Azure DevOps - Configure Self Hosted Agent For Azure Pipelines

In this article, we will learn how to create and configure a Self-Hosted Agent in Azure DevOps (ADO).
 
Before, we dive-in, let’s first understand what an Agent is?
 

What is an Agent?

 
Agent is a server with the appropriate Operating System and the tools installed and configured.
 
Azure DevOps supports the following two types of Agents
  • Microsoft Hosted Agent – These agents are managed by Microsoft
  • Self-Hosted Agents – These are created and managed by the Customer.
Most of the times, using the Microsoft hosted Agents should be good. However, below are the scenarios in which you should look at configuring Self-Hosted Agents
  1. If 10GB free space in the Virtual Machine (Agent) is not sufficient for your build needs.
  2. When you want a Virtual Machine, whose capacity is greater than of Standard DS2V2
  3. When you would like to use a Software that is not available in the Microsoft hosted Build Agents
In this article, we will focus on how to configure the Self Hosted Agents.
 
Prerequisites
  1. A Valid Azure DevOps account. You can create one at dev.azure.com
  2. Create an ADO Organization
    1. Create a Project.
  3. A Server that you would like to make it as an Agent (I have created a Windows Virtual Machine in Azure)
Navigate to the Organization Settings and click on Agent Pools menu item as shown in the below screen.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
In the Agent Pools listing page, click on the Default Agent Pool as shown in the below screenshot. Feel free to create a new Agent Pool if you want.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
You will be taken to the Agent Pool Summary page where you need to click on Agents tab and click on New Agent button as shown in the below screenshot.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
Clicking on the New Agent button opens a popup which shows information on how to configure the agent in various Operating Systems and the Architectures (see left hand side) as shown below.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
In this article, I’m going to configure a Windows based Virtual Machine as an ADO agent. Before, we start configuring the agent, we need to create a Personal Access Token which is an important step for authenticating the Virtual Machine by the ADO server.
 

Configure PAT in Azure DevOps


Azure DevOps - Configure Self Hosted Agent For Azure Pipelines

Configure PAT in Azure DevOps

  1. Navigate to User Settings (in the top right-hand side) and click on Personal access token
  2. Personal access token screen shows the list of all the PATs. Click on New token button.
  3. You will see a page where you configure the details of the new PAT.
  4. Once you provide the details, click on Create button to create the PAT. Once the PAT is generated a success message is shown and you can copy the PAT by clicking on the Copy button.

Configure Agent

 
Below is the list of steps that we need to perform in order to setup a Self-hosted agent.
  1. Create a Virtual machine and ensure it has the below prerequisites.
    1. Windows 2008 R2 SP1 or higher
    1. Install PowerShell 3.0 or higher
    1. .NET Framework 4.6.2 or higher
  2. Download the Agent by clicking on the Download button in the Get the Agent popup shown earlier and extract the zip file to any folder as shown below.
  3. Run the config.cmd file which prompts you several questions.
    • Enter the Server Url: https://dev.azure.com/{your-organization}
    • Enter the authentication type (press enter for PAT) – Press enter for this step.
    • Enter personal access token. – Paste the PAT generated earlier
    • Enter Agent Pool – Press Enter if you are creating an agent in the default Agent pool, otherwise, provide the name of the Agent Pool
    • Enter Agent Name – Provide the Agent Name (In my case, I provided CustomAgent as the Agent name
    • Enter run agent as Service – Press Y for this in order to install & Run the Agent as a service in the Virtual Machine.
That’s it. We have successfully configured the Agent. If you navigate to the Agent list in ADO, you should be able to view the agent with the name CustomAgent as shown below.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
Note
Initially, the status of the Agent might be offline. It should automatically turn into Online after a few minutes.
 
Let’s attach this new Agent Pool to a Pipeline and see how it works. Go-ahead and create a new pipeline with an Empty Job and use the Default as the Agent Pool as shown below.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
Note
You will choose the Agent Pool in the Pipeline definition. ADO will automatically pick the available agent for executing the pipeline.
 
So, Let’s run the pipeline now and click on Initialize Job step where you can view the Agent in which the pipeline has run as shown below. In my case the agent’s name was Custom Agent.
Azure DevOps - Configure Self Hosted Agent For Azure Pipelines
That’s it. We have learnt how to configure an Agent in Azure DevOps. If you have any questions, feel free to comment below. Happy Learning.