Nedudi
Nedudi

Reputation: 5949

Is there a way to use custom variables in Azure Pipelines conditions

I am trying to do something like this

variables:
  ${{ if eq(variables['abc'], 'dev') }}:
    someOtherVariable: '123'

with a variable defined via UI here:

enter image description here

It doesn't work. someOtherVariable is not defined after this.
Is there a way to use this variable in conditions? What should be the syntax?

thanks

Upvotes: 2

Views: 1438

Answers (1)

jessehouwing
jessehouwing

Reputation: 114641

The syntax is correct but doesn't seem to work for conditions that rely on variables within the block where variables are defined for custom variables.

It is one of the (many) quirks of the Azure Pipelines YAML processing pipeline. Some conditions, variables, templates, syntax is only available at specific stages of the YAML processing and it depends on whether you are in a pipeline, template, or decorator.

Simplest solution is to use a script step to set the variable and optionally make that step conditional:

    ${{ if eq(variables['condition'], 'true') }}:
      script: echo '##vso[task.setvariable variable=someOtherVariable]123'

or rely on one of my tasks to do that on your behalf:

- task: SetVariable@1
  inputs:
    name: 'someOtherVariable'
    value: '123'
  condition: eq(variables['condition'], 'true')

or:

${{ if eq(variables['condition'], 'true') }}:
- task: SetVariable@1
  inputs:
    name: 'someOtherVariable'
    value: '123' 

Upvotes: 3

Related Questions