How to Create Self-Hosted Agent for Azure DevOps Pipelines

Azure DevOps Pipelines offer a powerful and flexible way to automate your software development workflows. You can create and configure self-hosted agents to enhance your flexibility and control over your build and deployment processes. This article will walk us through the steps to create a self-hosted agent for Azure DevOps Pipelines. You may also be interested in the following articles: how to run your first Pipelines using Azure DevOps and how to configure email notifications in Azure DevOps. See also How to Integrate SonarCloud with Azure DevOps Pipeline
Self-hosted agents allow you to run your builds on your infrastructure, providing customization and scalability as opposed to Microsoft-hosted agents.
Please see how to set up a self-hosted speed test server on Ubuntu Linux, and Veeam Agent Vulnerability: Fix Veeam Agent vulnerability for Microsoft Windows.
Prerequisites for Creating a Self-Hosted Agent
Azure DevOps Account: Ensure you have an active Azure DevOps account. If not, sign up for one at Azure DevOps.
Azure DevOps Organization: This is where your project is being held.
Azure DevOps Project: Create or have an existing project within your Azure DevOps account.
Create a Self-Hosted Agent for Azure DevOps Pipelines
To create a self-hosted agent, do the following: Please see how to Remove the Bing Chat Button from the Edge Sidebar, and how to migrate the Veeam Configuration Database to PostgreSQL Server.
Step 1: Create a Personal Access Token (PAT)
On your Azure DevOps account, locate the User Settings icon on the right side of the screen, ensuring that the Azure DevOps organization you want to create the token is highlighted. As shown below, we are creating the token for Org-AB’s Azure DevOps organization.

Next, click on PAT from the drop-down menu.

When the next screen opens up, click on “New Token.” Ensure that you select the personal access tokens under the Security tab.

Enter the name for the token and expiration date, custom-defined, and then click on “Show all scopes” to display the agent pools tab. Then select read and manage access right for the agent pool.

Finally, copy the token to a notepad editor for future use. Please see Veeam Backup Deployment options for Microsoft 365 Data.

Step 2: Create Agent Pool
To create an agent pool from Azure DevOps organization settings, scroll to Agent Pools under the Pipelines tab. Click on Add Pool.

From the add pool screen, select self-hosted, provide the agent pool name; the description is optional, and then click on Create. 
Keep the auto-provision this agent in all the projects within the organization checked if you want to use the same token across all projects


You should see the newly created agent pool in the list of agent pools now.

Step 3: Create a new agent
The next step is to create a new agent within the agent pool created in the previous step. To do so, double-click on the name of the agent pool and click on “New agent.”

From the above screen, under the Windows OS tab, click on download.
Note, we're selecting Windows because we want to create the agent for Windows OS. If you're using macOS or Linux OS, enure to select the appropriate OS.

Wait for the agent to finish downloading. We have already downloaded the agent, so I will cancel my download and use the already downloaded copy instead.
Step 4: Create an agent locally on your machine
To create an agent locally, launch PowerShell in administrative mode.

On the PowerShell screen, run:
PS C:\> mkdir agent ; cd agent
PS C:\agent> Add-Type -AssemblyName System.IO.Compression.FileSystem ; [System.IO.Compression.ZipFile]::ExtractToDirectory("$HOME\Downloads\vsts-agent-win-x64-3.232.3.zip", "$PWD")

The above command creates a directory called agent, navigates into the directory, and then extracts the agent files into the agent directory.
Step 5: Configure the agent
To configure the agent, run:
# This command configures the agent on your local machine
.\config.cmd

Let me explain a few things from the above screenshot.
The server URL is the URL for your Azure DevOps Organization. The personal access token is the one we created in step 1. The name of the agent pool is the one we created in step 2. We specify the name of the agent as "techdirectarchive_agent" the next steps in the configuration process involve, pressing the Enter key and Y and on the last step, we press Enter for N and our agent was successfully started.
The agent won’t run until we run it. It shows offline in the Azure DevOps organization.

Step 6: Run the Agent locally
To run the agent, type the command below and press the Enter key.
.\run.cmd

Checking the Azure DevOps organization now, the agent status is reporting “Online.”

One good thing about running a self-hosted agent is that you have the flexibility to start and stop it whenever you like. Which means you’re in control of everything.
Run Azure DevOps Pipelines using Self-Hosted Agent
After creating the self-hosted agent, let us use it to run pipelines for our application.
We will go through the following steps to get this done:
Step 1: Get the Application Sample Code from GitHub
The above GitHub repository is provided by Microsoft for demo purposes.

Step 2: Prepare the Azure DevOps Project
Here we will import the application codes from GitHub into the Azure Repo.

The below screenshot shows that the codes have been imported successfully:

Step 3: Create a Build Pipeline
The next step is to create a build pipeline. To do so, navigate to the Pipelines tab in your Azure DevOps project. Click on Create Pipeline.

On the next screen, select where your code is stored as an Azure Repos Git.

When asked to select a repository, click on the name of your Azure DevOps project.

In the configure your pipeline screen, select ASP.NET Core (.Net Framework).

Review the YAML (Yet Another Markup Language) file before you save and run your pipeline. In the YAML file, we are going to make some modifications by replacing the vmImage: ‘windows-latest’ with the self-hosted agent created previously.

Now replace the vmImage text with the below codes:
pool
  Name: my-agent-pool
demands:
- Agent.Name equals techdirectarchive_agent
Remember to replace the name of the agent pool and agent name with your own. After that, click Save and run.

This triggers the build automatically based on the pipeline configurations as soon as you add the commit message.

The next screen should report the status of your build pipeline as successful, and you should also get an email notification.

I hope you found this article useful on how to create a self-hosted agent for Azure DevOps pipelines. Please feel free to leave a comment below.