Conditional Variables in Azure DevOps Pipelines

Creating Azure DevOps Pipelines, have you used a condition to determine which variables to use? If not, I will detail in this blog post how you can do this!

What is a condition?

Conditions or statements that are used to determine an outcome; used widely in programming.

Some examples of conditions:-

  • If today is Monday then true – if not, false!
  • If branch is main, then run task
  • If the sky is blue, echo “hello”

All various examples of conditions!

Now, lets look at using conditional variables with your Azure DevOps Pipelines!

Conditional Variables in Azure DevOps Pipelines

Are they useful? Very! I have built alot of pipelines & do tend to use quite a few types of conditions, in this blog post I will detail conditional variables, but will add more blog posts later with other ways to use conditions – they can used in jobs, steps, stages etc too!

In my example, I am going to use a parameter to decide on which teamName I want to be displayed in my Bash@3 task, lets step it out

The parameter “environment”, will be asked at run-time

parameters:
  - name: environment
    displayName: Which Team to deploy?
    type: string
    default: 'developer'
    values:
    - developer
    - preproduction
    - production

Now my variables, which are conditional depending on the parameter input, depending on the value of parameter.environment will decide which of the three teams: alpha, beta, charlie will be set to the variable.teamName

variables:
  - name: teamName
    ${{ if eq( parameters['environment'], 'developer') }}: 
      value: "alpha"
    ${{ if eq( parameters['environment'], 'preproduction' ) }}: 
      value: "beta"
    ${{ if notIn( parameters['environment'], 'developer', 'preproduction') }}: 
      value: "charlie"

Notice the last value (lines 7-8)?

I have used notIn, rather than a third iteration of if – I tend to do this; to have a catch-all!

Now as mentioned, the variable is conditional – depending on the parameter.environment name, the stage will echo the required variable

stages:
- stage: TeamToDeploy
  jobs:
  - job: Example
    steps:
      - task: Bash@3
        displayName: TeamDeployed
        inputs:
          targetType: inline
          script: |
            echo ${{variables.teamName}}

Lets run the pipeline, as mentioned above – The parameter “environment”, will be asked at run-time

Reviewing the task output in Azure DevOps

We can see the variable it uses is beta, the conditional for parameter.environment preproduction!

    ${{ if eq( parameters['environment'], 'preproduction' ) }}: 
      value: "beta"

Awesome! Hopefully this blog post has given you an insight into using Conditional Variables in Azure DevOps Pipelines

Full pipeline used below and can also be found in GitHub

pool:
  vmImage: ubuntu-18.04

parameters:
  - name: environment
    displayName: Which Team to deploy?
    type: string
    default: 'developer'
    values:
    - developer
    - preproduction
    - production

variables:
  - name: teamName
    ${{ if eq( parameters['environment'], 'developer') }}: 
      value: "alpha"
    ${{ if eq( parameters['environment'], 'preproduction' ) }}: 
      value: "beta"
    ${{ if notIn( parameters['environment'], 'developer', 'preproduction') }}: 
      value: "charlie"

stages:
- stage: TeamToDeploy
  jobs:
  - job: Example
    steps:
      - task: Bash@3
        displayName: TeamDeployed
        inputs:
          targetType: inline
          script: |
            echo ${{variables.teamName}}

2 comments

Leave a Reply