In Azure DevOps, agents are the workers that run your build and release jobs. There are two types of agents: Microsoft-hosted agents and self-hosted agents. Here’s a detailed explanation with examples and steps:
Microsoft-hosted Agents
Why: Microsoft-hosted agents are provided by Azure DevOps and run on Microsoft’s infrastructure. They are convenient because they require no setup and are always up-to-date with the latest tools and updates.
Steps:
- Create a New Pipeline:
- Go to your Azure DevOps project.
- Navigate to Pipelines > Pipelines and click on “New pipeline.”
- Select the Code Repository:
- Choose where your code is stored (e.g., Azure Repos, GitHub).
- Configure the Pipeline:
- Select a template or start from scratch.
- Define the pipeline steps in a YAML file or use the visual designer.
- Use Microsoft-hosted Agents:
- By default, the pipeline uses Microsoft-hosted agents.
- Example YAML:yamlCopy code
pool: vmImage: 'ubuntu-latest' steps: - script: echo Hello, world! displayName: 'Run a one-line script'
- Run the Pipeline:
- Save and run the pipeline.
- The pipeline will use a Microsoft-hosted agent to execute the steps.
Example: You want to compile a .NET application. The pipeline YAML might look like this:
trigger:
branches:
include:
- main
pool:
vmImage: 'windows-latest'
steps:
- task: UseDotNet@2
inputs:
packageType: 'sdk'
version: '5.x'
- script: dotnet build
displayName: 'Build the application'
- When you run this pipeline, it uses a Microsoft-hosted agent with the
windows-latest
image to build your .NET application.
Self-hosted Agents
Why: Self-hosted agents are managed by you on your own infrastructure. This is useful if you need specific software configurations, more control over the build environment, or if you want to avoid the limitations of Microsoft-hosted agents (e.g., build time limits).
Steps:
- Set Up a Self-hosted Agent:
- Go to your Azure DevOps project.
- Navigate to Project Settings > Agent pools.
- Click on “New agent” to download the agent package.
- Install the Agent:
- Extract the downloaded package on your server.
- Run the configuration script and provide the necessary information (server URL, agent pool, personal access token).
- Register the Agent:
- The agent will register itself with Azure DevOps and appear in the agent pool.
- Configure the Pipeline to Use the Self-hosted Agent:
- Modify your pipeline YAML to use your self-hosted agent pool.
- Example YAML:
pool:
name: 'Self-hosted'
steps:
- script: echo Hello, self-hosted world!
displayName: 'Run a one-line script'
- Run the Pipeline:
- Save and run the pipeline.
- The pipeline will use your self-hosted agent to execute the steps.
Example: You need to run a build on a machine with specific hardware or software. After setting up the self-hosted agent, your pipeline YAML might look like this:
trigger:
branches:
include:
- main
pool:
name: 'Self-hosted'
steps:
- script: echo Running on a self-hosted agent
displayName: 'Message'
- script: ./build.sh
displayName: 'Build the application'
- When you run this pipeline, it uses your self-hosted agent to execute the build script
build.sh
.
Live Environment and Flow
- Microsoft-hosted Agent Flow:
- Developer commits code to the repository.
- Pipeline triggers automatically.
- Microsoft-hosted agent picks up the job, sets up the environment, runs the build/test/deploy steps.
- Results are reported back to Azure DevOps.
- Self-hosted Agent Flow:
- Developer commits code to the repository.
- Pipeline triggers automatically.
- Self-hosted agent picks up the job from the configured server, runs the build/test/deploy steps using the custom environment.
- Results are reported back to Azure DevOps.