How to create a self-hosted agent for the azure build pipeline

In this article, we will discuss how to create a self-hosted agent for the build pipeline

Amr Younis
4 min readNov 1, 2020

What’re Azure Pipelines agents?
To build your code or deploy your software using Azure Pipelines, you need at least one agent. As you add more code and people, you’ll eventually need more.

When your pipeline runs, the system begins one or more jobs. An agent is a computing infrastructure with installed agent software that runs one job at a time and there are two types of agents Microsoft-hosted agent and self-hosted agent.

Microsoft-hosted agents
If your pipelines are in Azure Pipelines, then you’ve got a convenient option to run your jobs using a Microsoft-hosted agent. With Microsoft-hosted agents, maintenance and upgrades are taken care of for you. Each time you run a pipeline, you get a fresh virtual machine. The virtual machine is discarded after one use.
Azure Pipelines provides a pre-defined agent pool named Azure Pipelines with Microsoft-hosted agents.
For many teams, this is the simplest way to run your jobs. You can try it first and see if it works for your build or deployment. If not, you can use a self-hosted agent.
The Azure Pipelines agent pool offers several virtual machine images to choose from like (windows-latest OR windows-2019, vs2017-win2016, ubuntu-latest OR ubuntu-18.04, macOS-latest OR macOS-10.15)

Microsoft-hosted agent

Self-hosted agents
An agent that you set up and manage on your own to run jobs is a self-hosted agent. You can use self-hosted agents in Azure Pipelines or Team Foundation Server (TFS). Self-hosted agents give you more control to install dependent software needed for your builds and deployments. Also, machine-level caches and configuration persist from run to run, which can boost speed.
You can install the agent on Linux, macOS, or Windows machines. You can also install an agent on a Docker container.

Self-hosted agent

How to create a self-hosted agent?

  • Goto Project setting then click on Agent pools
Project Setting
  • Click on Add pool then fill the data as below but don’t forget to set your agent name then click create
  • Click on your created agent then click on the New agent button then Get the agent popup will appear then go to your virtual machine and open power-shell as administrator follow the below steps
  • Copy the following command and pasted on power-shell and press enter
    cd C:\ ; mkdir agent ; cd agent
  • Copy the create agent commend form Get the agent popup starting from Add-Type then press enter
  • After the agent created configure your agent using the following command .\config.md
  • After the configuring start, it’s will prompt three questions
    Enter server URL > https://dev.azure.com/your-organization-name then press enter for the second prompt click enter too it’s about authentication type PAT
    the final prompt about the token for creating token go to User Setting then click on Personal access token then click on New token and fill the scopes as below then press create and copy the token and go back to your VM power-shell and past the token and press enter finally use the following command to run the agent .\run.cmd after the command done the agent will be ready now use the pool name on your build pipeline and start the manual build to test that the agent is working fine.

Note that you can add any prerequisite installation on the build server like .Net Framework or .Net Core Run Time.

if you have any questions or face any issue leave a comment and I will reply to you once I saw your comment.

--

--