If, elseif or else in Azure DevOps Pipelines

Writing Azure DevOps Pipelines YAML, have you thought about including some conditional expressions? In this blog post, I am going to show how you can use If, elseif or else expressions to assist in your pipeline creation

Probably the most common expression you may be using is determining if a stage or job can run. There is lots of expressions available in Azure DevOps to assist you.

Azure DevOps Pipeline If, elseif or else expression examples

In this blog post, I will show example usage of these expressions in:

  • Determining which variable to use
  • Determining which task to run
  • Determining which stage to run

if, elseif or else expressions to determine which variable to use

Lets have a look at using these conditional expressions as a way to determine which variable to use depending on the parameter selected.

I have 1 parameter environment with three different options: develop, preproduction and production

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

Next, is the variable colour that has potential three different outputs, depending on the environment selected. Notice the use of if , elseif and else?

variables:
  - name: colour
    ${{ if eq( parameters['environment'], 'develop') }}:
      value: "red"
    ${{ elseif eq( parameters['environment'], 'preproduction' ) }}:
      value: "yellow"
    ${{ else }}:
      value: "purple"

The stage used to test, is a simple echo return of both parameter & variable values, with full pipeline example:

parameters:
  - name: environment
    displayName: Which Environment to deploy?
    type: string
    default: 'develop'
    values:
    - develop
    - preproduction
    - production
 
variables:
  - name: colour
    ${{ if eq( parameters['environment'], 'develop') }}:
      value: "red"
    ${{ elseif eq( parameters['environment'], 'preproduction' ) }}:
      value: "yellow"
    ${{ else }}:
      value: "purple"

stages:
- stage: EnvironmentToDeploy
  jobs:
  - job: colour
    steps:
      - task: Bash@3
        displayName: EnvironmentSelected
        inputs:
          targetType: inline
          script: |
            echo "The environment selected was ${{parameters.environment}} and colour ${{variables.colour}}"

Selecting the production environment parameter I get the below output:

if, elseif or else expressions to determine which task to run

Moving on, we can use the same expression to determine which task to run within our stage while using the same parameters as previous.

Looking at the pipeline, notice the stage and tasks? The task is determined on the parameters value:

parameters:
  - name: environment
    displayName: Which Environment to deploy?
    type: string
    default: 'develop'
    values:
    - develop
    - preproduction
    - production
 
stages:
- stage: TaskSelected
  jobs:
  - job: colour
    steps:
      - ${{ if eq( parameters['environment'], 'develop') }}:
        - task: Bash@3
          displayName: task1
          inputs:
            targetType: inline
            script: |
              echo "task1 is used as environment: ${{parameters.environment}} was selected"

      - ${{ elseif eq( parameters['environment'], 'preproduction') }}:
        - task: Bash@3
          displayName: task2
          inputs:
            targetType: inline
            script: |
              echo "task2 is used as environment: ${{parameters.environment}} was selected"

      - ${{ else }}:
        - task: Bash@3
          displayName: task3
          inputs:
            targetType: inline
            script: |
              echo "task3 is used as environment: ${{parameters.environment}} was selected"

Output of pipeline when parameter environment preproduction has been selected:

if, elseif or else expressions to determine which stage to run

To finish, lets look at the same setup as previous examples – but now, stages!

3 stages, the stage to run is dependent on which parameter is selected:

parameters:
  - name: environment
    displayName: Which Environment to deploy?
    type: string
    default: 'develop'
    values:
    - develop
    - preproduction
    - production
 
stages:
- ${{ if eq( parameters['environment'], 'develop') }}:
  - stage: stage1
    jobs:
    - job: stage1job
      steps:
        - task: Bash@3
          displayName: stageselected
          inputs:
            targetType: inline
            script: |
              echo "The stage selected was stage1 as environment: ${{parameters.environment}} was selected"

- ${{ elseif eq( parameters['environment'], 'develop') }}:
  - stage: stage2
    jobs:
    - job: stage2job
      steps:
        - task: Bash@3
          displayName: stageselected
          inputs:
            targetType: inline
            script: |
              echo "The stage selected was stage2 as environment: ${{parameters.environment}} was selected"

- ${{ else }}:
  - stage: stage3
    jobs:
    - job: stage3job
      steps:
        - task: Bash@3
          displayName: stageselected
          inputs:
            targetType: inline
            script: |
              echo "The stage selected was stage3 as environment: ${{parameters.environment}} was selected"

Selecting parameter environment production, will run stage: stage3

Awesome, i’ve shown 3 examples of how If, elseif or else expressions can and will assist you in your pipeline creation. There is so many ways & use cases for these sort of expressions.

Try various ways, to see how it can recreate your pipeline thoughts – they are great! As always, any queries etc – do give me a shout, always love to hear feedback and seeing different Azure DevOps pipelines being created!

GitHub Repo here – containing all examples used

2 comments

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s