How to Deploy a Self-Hosted Agent in Azure Pipeline

GABRIEL OKOM
5 min readJan 14, 2024

--

Step by Step guide in creating your own self-hosted build agent for Azure CICD pipeline using Azure VM.

Rather than using the recommended agents given to us by Microsoft itself, sometimes there by a need to have more control over these agents such as the kind of image it uses, the version of the image needed, scalability, security etc. We may then need to have our own customized agents that gives us more control in order to meet up with our application needs.

The decision to use self-hosted agents in Azure DevOps is driven by the desire for greater control and customization over the build and deployment environment, ensuring that it aligns perfectly with the unique needs of your application. A self-hosted agent can be on Windows, Linux. MacOS and Docker container.

For this project we will be deploying our self-hosted agent in an Azure VM.

Step 1: Configure a Self-Hosted Agent

Step 2: Create an Azure VM

Step 3: Create a Personal Access Token (PAT)

Step 4: Log into the Azure VM and Configure the Self-Hosted Agent

Step 1: Configure a Self-Hosted Agent

Make sure you already created a new project for this purpose; in this case mine is Self-hosted-project-demo , you can give it any name you deem fit.

  • Go to Azure DevOps dashboard — https://dev.azure.com/
  • Select your project dashboard: This is the new project you just created
  • Go to your project settings: Scroll down to the bottom of the left menu to find it
  • From the menu on the left Click on Agent pools: This is a pool of agents that you set on your own to run jobs.
  • Create a new Agent pool name by clicking on Add Pool > for “pool to link” select New > for “Pool type” select Self hosted > give it a name Ubuntu-Agent-Pool you can add a description > Make sure to checkGrant access permission to all pipelines > Finally, click on Create
  • Click on the newly created poolUbuntu-Agent-Pool > Click New Agent > Click on Linux > You can download or Copy the configuration file (For this project I will be using the copied configuration file)
Copy the configuration

This is my copied configuration link.

https://vstsagentpackage.azureedge.net/agent/3.232.1/vsts-agent-linux-x64-3.232.1.tar.gz

Login to your Azure VM now. Well, if you haven’t created your Azure VM yet like me, lets do that now.

Step 2: Create an Azure VM

It is pretty straight forward on how to create an Azure VM

. Go to portal.azure.com> search for Virtual Machines > Click on Create.

. Create a Resource Group if you don’t already have one > Insert the VM name UbuntuagentVM > for “Image” use Ubuntu:20 > I will be leaving everything else as default. > Download the private key > Click Create

Make sure SSH inbound port 22 is open (usually open by default for linux machines), we need this to be able to access the VM. The downloaded private key helps us to have an SSH access into the machine.

Downloaded private key

Step 3: Create a Personal Access Token (PAT)

A PAT, or Personal Access Token, is a string of digits used to provide secure access to Azure DevOps for a hosted or self-hosted agent. It plays a crucial role in registering and authenticating the agent with Azure DevOps, ensuring secure communication and authorization for various tasks within the DevOps environment.

Create a Personal Access Token for authentication to Azure DevOps

Step 4: Log into the Azure VM and Configure the Self-Hosted Agent

In this section, we will be log into the Azure VM we created earlier through SSH. After the login, we will proceed by configuring for the Self-Hosted Agent using the configuration files genereted when creating the agent pool.

Here is a snippet of the codes and steps used in configuring for the self-hosted ubuntu agent.

  • Go into the directory where you downloaded the unique ./ubuntuagentVM_key.pem . In my own case I have downloaded it inside my ./downloads folder. This is essential because you need the key to gain access into your Azure VM.
cd ./downloads
  • Login into the Azure VM created earlier and copy its IP
ssh -i ubuntuagentVM_key.pem azureuser@40.124.173.167
  • Create the Agent
mkdir myagent && cd myagent
  • Download the unique agent configuration link
wget https://vstsagentpackage.azureedge.net/agent/2.214.1/vsts-agent-linux-x64-2.214.1.tar.gz
  • Extract the files and Configure the Agent
tar zxvf vsts-agent-linux-x64-2.214.1.tar.gz
  • List the files in the directory after extracting.
ls -al
  • Run the below command
./config.sh
  • Accept the Team Explorer Everywhere license agreement now?
    Type Y and enter
  • Enter server URL > https://dev.azure.com/yourorganization
  • Enter authentication type (press enter for PAT) > PAT
  • Copy and paste personal access token generated earlier in step 3
  • Enter Agent pool > From the agent pool created earlier copy and paste the name Ubuntu-Agent-Pool and hit enter
  • Enter Agent name > Give it a name myBuildAgent_1 and hit enter
  • Enter work folder > Give some name or leave as default by hitting enter

Congrats, you have successfully configured a self-hosted agent

  • Configure the Agent to run as a Service
sudo ./svc.sh install &
  • Execute now to run as a service
./runsvc.sh &
  • The agent should now be listening for pods

This confirms that Build agent is successfully configured in Azure DevOps and is available to run builds

  • You can also check the status of build Agent > Click on the ubuntu-agent-pool > Click on Agents
Confirm the status of your agent is showing online

Congrats !! We have come to the end of this project.

If you’d like to know how to deploy an APP using the Self-Hosted Agent. Click Here

#docker #selfhosted #container #python #cicd #GabrielOkom #kubernetes #dockerimage #appservice #webapp #devops #devopsprojects

--

--

GABRIEL OKOM

MSc Cyber Security and Computer Forensics | Certified DevOps Engineer