Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional insertion in YAML templates with variables at build time Azure DevOps

Is there a way to use varaiables defined in the pipeline(dashboard) to use conditional insertion on a YAML template for steps:

I mean i have this steps:


- ${{ if eq(variables.['somevar'], '')}}:
    - task: Bash@3
      displayName: Var does not being declared
      inputs:
        targetType: 'inline'
        script: |
         echo "This is a step where the var 'somevar' does not being declared'
    - task: Bash@3
      displayName: Another Step
      inputs:
        targetType: 'inline'
        script: |
         echo "This is another step where the var 'somevar' does not being declared'

This should run when the variable is not being declared

enter image description here


- ${{ if ne(variables.['somevar'], '')}}:
    - task: Bash@3
      displayName: Var is being declared
      inputs:
        targetType: 'inline'
        script: |
         echo "This is a step where the var 'somevar' is being declared with some value'
    - task: Bash@3
      displayName: Another Step
      inputs:
        targetType: 'inline'
        script: |
         echo "This is another step where the var 'somevar' is being declared with some value'

This should run when the variable is declared

enter image description here


I know that it exist the Runtime parameters , but i don't want to use them every time that i run the pipeline (manually).I want that some pipelines run some steps, when i have declared the variable, and some other pipelines don't run some steps when the variable is not being declared.

I also know that it exist the condition inside every step like

condition: eq(variables.somevar, 'value')

But i want to use conditional insertion to run in some cases, many steps like in the examples above. Not in just one step

like image 826
Zahid Avatar asked Sep 16 '25 14:09

Zahid


1 Answers

I want that some pipelines run some steps, when i have declared the variable, and some other pipelines don't run some steps when the variable is not being declared.

Why not use the Runtime parameters? With the Runtime parameters used we can simply achieve your requirement.

Whatever, to achieve that by using the variables we can try to Specify condition for the Yaml templates:

Create Yaml templates :

# File: somevar-nondeclared.yml
steps:
- task: Bash@3
  displayName: Var does not being declared
  inputs:
     targetType: 'inline'
     script: |
      echo "This is a step where the var 'somevar' does not being declared"
- task: Bash@3
  displayName: Another Step
  inputs:
     targetType: 'inline'
     script: |
      echo "This is another step where the var 'somevar' does not being declared"

And

# File: somevar-declared.yml
steps:
- task: Bash@3
  displayName: Var is being declared
  inputs:
    targetType: 'inline'
    script: |
     echo "This is a step where the var 'somevar' is being declared with some value"
- task: Bash@3
  displayName: Another Step
  inputs:
    targetType: 'inline'
    script: |
     echo "This is another step where the var 'somevar' is being declared with some value"

And the azure-pipelines.yml for your reference :

# File: azure-pipelines.yml

trigger: none

jobs:
- job: Linux
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - template: somevar-nondeclared.yml 
  condition: eq(variables['somevar'], '')

- job: Windows
  pool:
    vmImage: 'windows-latest'
  steps:
  - template: somevar-declared.yml
  condition: ne(variables['somevar'], '')

Thus, It will run windows job if you declared the variable when run pipeline. Otherwise it will run the Linux job in this example.

enter image description here

like image 147
Andy Li-MSFT Avatar answered Sep 19 '25 06:09

Andy Li-MSFT