kitten_woof
kitten_woof

Reputation: 35

Azure Yaml DevOps variable handling

So I have a YAML file where I am trying to do something like the following:

# top of the file, declare global variables
variables:
- name: MyName
  value: 'apple'
...

parameters:
- name: SwitchName
  type: boolean
  default: false
...


stages:
- stage: Build
  displayName: 'Build'
  ...

  jobs:
    variables:
    - ${{ if eq(parameters.SwitchName, true) }}:
      - name: MyName
        value: '$(MyName)_pie'
    ...

    steps:
      - task: PowerShell@1
         inputs:
           scriptType: inlineScript
           inlineScript: |
             Write-Output $(MyName)
...
#end of script

The goal is to control the pipeline to print apple in default run and apple_pie when the user selects that parameter in the UI to be true.

I understand that parameters are compile-time and variables are run-time. I know that to overwrite a global variable you can create a variable at a job level and change it as yo see fit. Sadly, the way how template works do not let me redeclare that variable properly and I end up with $(MyName)_pie. For some reason, Yaml fails to see that inside that template there is a runtime variable that is needed to be defined.

What are my options here to achieve desired behavior? Anything missing in my understanding here?

Upvotes: 0

Views: 856

Answers (1)

Kevin Lu-MSFT
Kevin Lu-MSFT

Reputation: 35099

You can change to use the ${{ variables.MyName }} to call the variable in root level.

For example:

variables:
- name: MyName
  value: 'apple'


parameters:
- name: SwitchName
  type: boolean
  default: false



stages:
- stage: Build
  displayName: 'Build'
  jobs:
   - job: A
     variables:
     - ${{ if eq(parameters.SwitchName, true) }}:
       - name: MyName
         value: '${{ variables.MyName }}_pie'
     steps:
       - script: "echo $(MyName)"

Result:

enter image description here

Since you used an if expression to reassign value, the variable will be assigned at compile time.

Refer to this doc: Runtime expression syntax

You can use the template expression format: ${{ variables.var }}

Upvotes: 1

Related Questions