0

I am trying to use a variable that is set in a script using a logging command in an if statement in an Azure pipeline as shown below, however, it takes the initial value.

parameters:
- name: aParameter
  type: string
  default: dothis
  values:
  - dothis
  - dothat

variables:
- name: aVariable
  value: 0
  
schedules:
- cron:
...

steps:
- bash: |
    updateVariable=$(<an expression>)
    echo "##vso[task.setvariable variable=aVariable]$updateVariable"
  displayName: Set the variable value

- bash: echo $(aVariable) # It actually prints the updated value

- ${{ if or(eq(parameters.aParameter, 'dothis'), and(eq(variables['Build.Reason'], 'Schedule'), lt(variables['aVariable'], 30))) }}:
  - template: templates/do-this.yaml

- ${{ if or(eq(parameters.aParameter, 'dothat'), and(eq(variables['Build.Reason'], 'Schedule'), ge(variables['aVariable'], 30))) }}:
  - template: templates/do-that.yaml

Do you know if it is possible to achieve this behavior?

1 Answers1

1

Do you know if it is possible to achieve this behavior?

I am afraid it is impossible to achieve this behavior at this moment.

That because the template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts in a pipeline.

But set variable aVariable in a script using a logging command need to run the task during the pipeline runs.

That the reason why it takes the initial value instead of the updated value.

You could check the document the Understand variable syntax for some more details:

In a pipeline, template expression variables (${{ variables.var }}) get processed at compile time, before runtime starts. Macro syntax variables ($(var)) get processed during runtime before a task runs.

Leo Liu
  • 71,098
  • 10
  • 114
  • 135