Photo by Paul

Dynamically select your build Agent using Azure DevOps Server

Run your project on a specific agent on demand!

Posted by Damien Aicheh on 01/13/2022 · 4 mins

When running your pipeline on Azure DevOps Server for your projects you sometimes need to target a specific agent without changing the configuration of your demands.

This can be useful in these cases:

  • Test if your project can build with the next sdk version
  • Run you project to a specific Agent because the default one is stop or broken
  • Test a new agent configuration for your project

When you use Azure DevOps Server you are responsible of managing your pool of agents.

In this tutorial we will discover how to define your default demands to target all the agents compatibles and in the same time, be able to select a specific agent if needed.

The pipeline

A pipeline has a structure like this:

  | --- stage 1
      | --- job 1
          | --- task 1
          | --- task X
      | --- job Y
  | --- stage Z

Each pipeline can have multiple stages which can contains multiple jobs and each jobs has his own steps represented by the tasks.

For instance, a stage can have the responsability to contain all build jobs for your project, and each job will build your application for a specific environment (dev, staging, prod…). A task is a simple command that execute a script.

Define the demands

The demands allow Azure DevOps to select the correct agent for you. You can define them using the demands keyword.

Down below we ask for an agent with the a version of XCode set to 13.0:

- XCODE_VERSION -equals 13.0

The XCODE_VERSION is called a capacity. This is manually defined in your agent. With this demand, your pipeline will run only with an agent running this version of Xcode.

Select a specific agent

Now we know how to select an agent using the demands, let’s add the ability to choose it by his name:

  agentName: 'Any'

- stage: Build
    name: Default # The name of your agent pool
    ${{ if eq(parameters.agentName, 'Any') }}:
      - XCODE_VERSION -equals 13.0
      # Other demands here
    ${{ if ne(parameters.agentName, 'Any') }}:
      - Agent.Name -equals ${{ parameters.agentName }}
  - job:
      # All steps for your job here..

As you can see above we define the agentName to a default value: Any. When you will run your pipeline if you don’t change this value it will automatically run it using the demands you specified, here the Xcode version.

However, if you want to run your pipeline on a specific agent, you just have to enter the name of this agent before starting the pipeline.

Final Touch

It’s now easy to switch between different agents and configurations on your Azure DevOps Server pipelines. From my experience, this functionality will save you a lot of time in your projects!

Happy coding!

Do not hesitate to follow me on to not miss my next tutorial!