azure-devopsazure-pipelinesazure-yaml-pipelines

use stageDependencies in if-statements in Azure DevOps yaml


I have a pretty complex setup of my Pipelines in Azure DevOps for various reasons but I'm kind of stuck in a special scenario now. Let me explain a bit.

There is a Stage_A with Job_A setting a Variable_A. Now there is a Stage_B with Job_B, need to use the Variable_A from Stage_A.Job_A.

The variable in Job_A is set by this:

echo ##vso[task.setvariable variable=Variable_A;isOutput=true]$value

Now, Job_B in Stage_B can access the variable in a condition with

variables: 
          Variable_A_FromStageA: $[stageDependencies.Stage_A.Job_A.outputs['task_A.Variable_A']]

I can also do an echo on the variable by using

echo $(Variable_A_FromStageA)

the Question is now, how can I use this in an if-statement? I tried different approaches:

- ${{ if eq($(Variable_A_FromStageA), 'True') }}:

- ${{ if eq(variables.Variable_A_FromStageA, 'True') }}:

- ${{ if eq(variables['Variable_A_FromStageA'], 'True') }}:

- ${{ if eq(stageDependencies.Stage_A.Job_A.outputs['task_A.Variable_A'], "True") }}:

Nothing actually works. Either the system complains about syntax issues or it doesn't evaluate it correctly. I don't really know how to use the information in my if statement in the yaml file. The documentation is not really clear about it. It only mentions the usage of a stage dependency in a condition and that's it. Hope anyone can help me here!

Cheers, Frank


Solution

  • use stageDependencies in if-statements in Azure DevOps yaml

    If you mean you want to use conditional insertion to use the variables output from the logging command, then answer is NO.

    The reason is the conditional insertion needs compile time value(you must provide them before pipeline run.), but the variable that the logging command output is runtime. Conditional Insertion will be unable to get it.

    The right way is to use "condition" instead of "Conditional Insertion". Using condition can achieve your situation.

    I write a demo for you as below:

    trigger:
    - none
    pool:
      vmImage: ubuntu-latest
    stages:
    - stage: A
      jobs:
      - job: A1
        steps:
         - bash: echo "##vso[task.setvariable variable=shouldrun;isOutput=true]true"
         # or on Windows:
         # - script: echo ##vso[task.setvariable variable=shouldrun;isOutput=true]true
           name: printvar
    - stage: B
      condition: and(succeeded(), eq(dependencies.A.outputs['A1.printvar.shouldrun'], 'true'))
      variables:
          myStageAVar: $[stageDependencies.A.A1.outputs['printvar.shouldrun']]
      dependsOn: A
      jobs:
      - job: B1
        steps:
        - script: echo $(myStageAVar)