Azure DevOps Pipelines: Build Agents Explained

Welcome to this tutorial on understanding and utilizing build agents within Azure DevOps Pipelines. Build agents are the backbone of your CI/CD processes, responsible for executing your build, test, and deployment jobs.

What are Build Agents?

A build agent is a piece of software installed on a machine (either self-hosted or a Microsoft-hosted virtual machine) that connects to Azure DevOps Services or Azure DevOps Server. When a pipeline is triggered, Azure DevOps dispatches tasks to available agents that meet the pipeline's requirements.

Types of Build Agents

1. Microsoft-Hosted Agents

These are agents managed by Microsoft and are readily available to use. They are hosted on Azure and offer a quick and easy way to get started with pipelines. You don't need to worry about maintaining or updating them.

  • Pros: No setup or maintenance required, always up-to-date, scalable.
  • Cons: Limited customization, potential for queue times if demand is high, may not be suitable for all scenarios (e.g., needing specific software or network access).

Microsoft-hosted agents have specific capabilities associated with their operating system (e.g., vsts-agent-win-x64 for Windows, vsts-agent-linux-x64 for Linux, vsts-agent-mac-x64 for macOS).

2. Self-Hosted Agents

You can install and manage your own agents on your own infrastructure (e.g., on-premises servers, Azure VMs, Docker containers). This gives you complete control over the environment, allowing you to install custom software, configure specific network settings, and manage hardware resources.

  • Pros: Full control over the environment, install any software, optimized for specific needs, no queue times (if enough agents are available), can access private resources.
  • Cons: Requires setup, maintenance, and scaling effort.

Agent Pools

Agent pools are logical groupings of agents. You can create different agent pools for different purposes or teams. When defining your pipeline jobs, you specify which agent pool the job should run on. This helps in managing and allocating your build resources efficiently.

Commonly used agent pools include:

  • Azure Pipelines (Microsoft-hosted): Default pool for Microsoft-hosted agents.
  • Your Own Pools: Custom pools you create for self-hosted agents.

Configuring a Self-Hosted Agent

Setting up a self-hosted agent involves a few key steps:

  1. Install Node.js: The agent software is built on Node.js.
  2. Download Agent Software: Obtain the agent software from Azure DevOps.
  3. Configure the Agent: Run the configuration script, providing your Azure DevOps organization URL and a personal access token (PAT) for authentication. You'll also specify the agent pool.
  4. Run the Agent: Start the agent service (either interactively or as a background service).

Important Security Note:

When creating a PAT for your self-hosted agent, grant only the necessary permissions. For build agents, typically 'Agent Pools (read & manage)' and 'Build (read & execute)' are sufficient.

Using Agents in Pipelines (YAML Example)

In your Azure Pipelines YAML file, you specify the agent pool using the pool keyword:


trigger:
- main

pool:
  name: Azure Pipelines # Use Microsoft-hosted agents
  vmImage: ubuntu-latest # Specify the VM image

# Or for a self-hosted agent pool:
# pool:
#   name: MySelfHostedAgentPool

jobs:
- job: Build
  displayName: 'Build the application'
  steps:
  - script: echo Building...
    displayName: 'Run a one-line script'

  - script: |
      echo Add other tasks to build, test, and deploy your project.
      echo Example: dotnet build
    displayName: 'Multi-line script'
                

Agent Capabilities

Agents can have capabilities, which are essentially software or environment variables detected on the agent. Your pipeline tasks can depend on these capabilities. For example, a task might require a specific version of Node.js or a particular SDK to be present. You can see the detected capabilities of an agent in the Agent Pools section of Azure DevOps.

Troubleshooting Common Issues

  • Agent Offline: Ensure the agent service is running and has a stable network connection to Azure DevOps.
  • Job Rejection: Verify that the agent pool and agent capabilities match the requirements specified in your pipeline job.
  • Permissions: Ensure the PAT used for agent configuration has the correct scopes.

By understanding and effectively managing build agents, you can optimize your CI/CD workflows, ensure reliable builds, and accelerate your software delivery.