Photo by Timothy Dykes

Manage your demands dynamically with Azure DevOps

Specify your demands easily

Posted by Damien Aicheh on 12/10/2020 · 5 mins

While developing your projects and using Azure DevOps for your pipelines, you probably noticed that testing the compatibility of your project with latest version of tools and softwares could be tricky. You should start with editing your YAML pipelines and changing the demands to target the latest version you need. In this tutorial, we are going to see how to declare your demands so it can be changed easily without updating your YAML pipeline.

What are the demands?

Demands are useful to define a list of capabilities the build agent should have to run your pipelines. For instance, make sure:

  • a specific SDK is installed
  • the project will be built with a specific version of tools

This approach is used densely when you manage your own pull of agents. Each agent has its capabilities and each one of your projects probably needs its own conditions to be able to run correctly.

Let’s have an example; a project needs to compile with the version of XCode (the official iOS IDE) 11.0 and another project needs the version 12.0. You will need to pass this configuration to your demands so Azure DevOps will find the right agents:

In the case you want to have the version 11.0 of the software you will declare your demands like this:

demands:
- XCODE_VERSION -equals 11.0

And the other one need the version 12.0 so you declare your demands as below:

demands:
- XCODE_VERSION -equals 12.0

Assuming XCODE_VERSION is a key defined in advance in the agent capabilities which returns the version of the software.

Now, imagine we have a pipeline like this:

trigger: none

pool:
    name: Default
    demands:
    - XCODE_VERSION -equals 11.0

jobs:
- job:
  displayName: 'Job 1'
  steps:
  - task: Bash@3
    inputs:
      targetType: 'inline' 
      script: |
        echo "XCode version..."
        /usr/bin/xcodebuild -version

If for any reason you need to build your project with a different version of XCode, you will not be able to do it without updating your YAML template which will probably begin a Pull Request process to review your new YAML and wait for reviewers.

So how to avoid this and keep your pipeline flexible?

Define custom properties

Azure DevOps has the ability to customize the run menu; Hence, we will specify on top of our YAML template a parameter for the XCode version called xcodeVersion and a default value of 11.0

parameters:
- name: xcodeVersion
  displayName: 'XCode version'
  type: string
  default: '11.0'

Because demands need to be known at the beginning of the pipeline, declaring variables is needed to be used for demands entries.


variables:
- name: xcodeVersionVariable
  value: '${{ parameters.xcodeVersion }}'

Thus, the previous pipeline will finally looks like this:


trigger: none

parameters:
- name: xcodeVersion
  displayName: 'XCode version'
  type: string
  default: '11.0'

variables:
- name: xcodeVersionVariable
  value: '${{ parameters.xcodeVersion }}'

pool:
    name: Default
    demands:
    - XCODE_VERSION -equals $(xcodeVersionVariable) # Demands selected dynamically

jobs:
- job:
  displayName: 'Job 1'
  steps:
  - task: Bash@3
    inputs:
      targetType: 'inline' 
      script: |
        echo "XCode version..."
        /usr/bin/xcodebuild -version

The job will be running on an agent running XCode 11.0 by default, and if you want to test this same job with the version 12.0 or latest it is now easily. You just have to type the version you want in the run menu:

Run menu

Final touch

By reaching this step, you have the ability to change the demands as you need without updating your YAML pipelines. That guarantees a flexible pipeline and progressive updates.

Happy coding!

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