1

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

Dirk G.
  • 11
  • 2

1 Answers1

1

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

Glue Ops
  • 658
  • 3
  • 16
  • Thank you! Yes, parameters is an option but in my case it is a template pipeline which is used by multiple pipelines. I would then have to define the parameter additionally in every pipeline using the template and pass the parameter to it. This looked to me as too much overhead. – Dirk G. Mar 25 '22 at 11:29
  • Unfortunately no way to conditionally parse pipelines using variables. What is the issue with using conditions? They can be set per stage, job or step and you can achieve the same functionality toggle using variables. – Glue Ops Mar 25 '22 at 11:40
  • Just to clarify last comment. some variables are available for use during template parsing. e.g. Build.SourceBranch. So you can conditionally parse pipelines using those. But none of the user defined variables can be used. More information here: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/runs?view=azure-devops – Glue Ops Mar 25 '22 at 11:45
  • Thank you. Yes, I know the Build.Sourcebranch but I want to combine this with an additional flag and for readability reasons since the combination of the branchname and the flag is needed on multiple places in the pipeline. So in my case: only if it's a release-Branch AND a release is forced, some things should happen. But may be you are right and I should structure the pipeline a bit different to use conditions on a job level or so. – Dirk G. Mar 25 '22 at 12:29
  • Good luck and would appreciate marking the answer if you believe this helped answer your question. – Glue Ops Mar 26 '22 at 13:56