Dirk G.
Dirk G.

Reputation: 11

Use query-time variables in pipeline YAML

I have set up a pipeline with variables users can enter using the UI like this:

UI for userinput of variable called 'forceRelease'

I now want to use this variable in the pipeline yaml inside an if-statement like this:

  jobs:
  - job: Demo
    steps:
    - ${{ if eq(variables['forceRelease'], 'true') }}:
    ...some more stuff...

This does'nt work. I've tried different approaches but could not find the right syntax. If I use the variable inside a condition, it works fine. Like this:

  jobs:
  - job: MAVEN_Build
    - task: Bash@3
      condition: eq(variables['forceRelease'], 'true')

I also tried to map the variable inside the variables block to a new pipeline variable like this:

variables:
 isReleaseBranch: ${{ startsWith(variables['build.sourcebranch'],'refs/heads/pipelines-playground') }}
 isForceRelease: $(forceRelease)

The first variable using 'build.sourcebranch' works fine. My approach using forceRelease doesnt work :(

Any ideas would be appreciated!

Cheers, Dirk

Upvotes: 1

Views: 858

Answers (1)

Glue Ops
Glue Ops

Reputation: 698

AFAIK this is working as intended. User set variables are not expanded during parsing of the template.

You can read more on pipeline processing here: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runs?view=azure-devops

You should instead use parameters.

parameters:
- name: "forceRelease"
  type: boolean
  default: "false"
- name: "someOtherParameter"
  type: string
  default: "someValue"


stages:
  - ${{ if eq(parameters['forceRelease'], true)}}:
    - stage: build
      jobs:
      - job: bash_job
        steps:
        - task: Bash@3
          inputs:
            targetType: 'inline'
            script: |
              # Write your commands here

And then when you run the pipeline you have the option to enable the parameter forceRelease or add someOtherParameter string.

enter image description here

Upvotes: 1

Related Questions