2

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

fmode
  • 23
  • 5

1 Answers1

2

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)
RoyWang-MSFT
  • 245
  • 1
  • 3
  • Hi RoyWang, thank you for your proposal. I need to check it with our setup, but this sounds like a workable solution. Nevertheless, it will leave us with a lot of "zombie"-Steps in the Pipeline which is not really nice. I was under the impression that a stage which is dependent on another one gets evaluated once the first one is done. But looks like pipelines are very limited in dynamic evaluation. – fmode Nov 28 '22 at 09:43
  • Hi @RoyWang , I think your proposed solution does not work in our use case. The parameter is used to control the execution of a bundle of tasks added as templates within the whole pipeline. several Tasks A - Conditional Tasks B - several Tasks C. Tasks C can only be executed if A was successful. When B is omitted, execute C but if B was run only execute C when B was successful. Conditions cannot be used for template calls and put the condition into the template is not an option because it's not always needed, only in a special case. This would end up in a huge overload of parameters. – fmode Nov 28 '22 at 14:26
  • You managed to solve a completely different problem I was having. I could not make the condition work on stage level - I was trying with `stageDependencies.*` syntax as you have for setting up the variable there. Changing that to your proposal here (using `dependencies.*`) finally worked. For all its beauty, I am baffled by the variation and complexity of syntax Microsoft has introduced to their pipeline YML specification. – Nikola Malešević Jul 21 '23 at 17:44