Conditional formatting of a template parameter in Azure devops pipeline

Conditional formatting of a template parameter in Azure devops pipeline

I have a pipeline that calls a template with a parameter called Args:

trigger: none

schedules:
  - cron: "0 * * * *"
    displayName: Every hour
    branches:
      include:
        - main

jobs:
- job: myJob
  steps:
    - template: .pipelines/templates/template.yml
      parameters:
        Args: "-c $(configKey)"
  strategy:
    matrix:
      item1:
        configKey: X
        zoneRedundancy: true
      item2:
        configKey: Y

I want to make a change so that if in the matrix, zoneRedundancy is set and is set to true, then args is set to "-c $(configKey) --redundancyEnabled" instead of "-c $(configKey)"

I tried

     - ${{ if eq(variables['zoneRedundancy'], 'true') }}:
      - template: ../../.pipelines/templates/template.yml
        parameters:
          Args: "-c $(configKey) --redundancyEnabled"

    - ${{ if ne(variables['zoneRedundancy'], 'true') }}:
      - template: ../../.pipelines/templates/template.yml
        parameters:
          Args: "-c $(configKey)"

but it didn't work

Answer

You need to handle the conditional logic inside the steps of the job as you're working with a matrix strategy. And you should evaluate the condition based on the zoneRedundancy value for each matrix item.

Try:

trigger: none

schedules:
  - cron: "0 * * * *"
    displayName: Every hour
    branches:
      include:
        - main

jobs:
- job: myJob
  steps:
    - ${{ each item in parameters.matrix }}:
        - template: .pipelines/templates/template.yml
          parameters:
            Args: |
              -c $(item.configKey) ${{ if eq(item.zoneRedundancy, true) }} then --redundancyEnabled else '' 
  strategy:
    matrix:
      item1:
        configKey: X
        zoneRedundancy: true
      item2:
        configKey: Y
        zoneRedundancy: false

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles