9

I have got an Azure Pipeline already running. Now I want to execute a sequence of steps only if a certain condition becomes true during runtime.

Example

steps:
  - template: steps_checkout.yml
  # some more steps here

  - bash: |
    if [ some condition ]; then 
      echo "##vso[task.setVariable variable=rebuild_lib]false"
      echo "Did set rebuild_lib to false"
     fi

  - if eq( variables.rebuild_lib, true) ):
    - template: steps_lib_build.yml

The line if eq( variables.rebuild_lib, true) ) does not work, because it is not a correct condition syntax. I could use

${{ if eq( parameters.something, true ) }}

but that needs to be known at runtime. Accodring to https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops an expression can also be $[ if eq(variables.rebuild_lib), true] to be evaluated at runtime, but using this, I get

Unexpected value '$[ if eq( variables.rebuild_lib, true) ) ]'

Seems like the yml cannot be modified at runtime this way.

So how do I decide to use a template at runtime?

I could imagine giving the variable as parameter to the next template adapter.yml. This template adapter.yml gets then the variable as parameter and could use the ${{}} expression and again use the next template steps_lib_build.yml ... but creating templates only for that seems somehow ... workaroundish.

Also using something like

- template: steps_lib_build.yml
  condition: ...

does not work.

Is there a good way to do this?

maze
  • 789
  • 1
  • 7
  • 31
  • How about the issue? If you have any update, please let me know. And my answer will help you, could you please accept it as an answer, so it could help other community members who get the same issues and we could archive this thread, thanks. – Felix Mar 12 '21 at 01:48

2 Answers2

4

In your current yaml, you are trying to use the Parameters to select a template. But according to the doc: Parameters to select a template at runtime, this is only used at runtime. So here, we can use the job conditions and output variables to help us separate the second yaml template.

Here is a demo yaml example to help you:

trigger: none

pool:
  vmImage: ubuntu-latest

jobs:
  - job: A
    steps:
    - task: Bash@3
      name: ProduceVar  # because we're going to depend on it, we need to name the step
      inputs:
        targetType: 'inline'
        script: 'echo "##vso[task.setVariable variable=rebuild_lib;isOutput=true]false"'
  - job: B
    condition: and(succeeded(), eq(dependencies.A.outputs['ProduceVar.rebuild_lib'], 'true'))
    dependsOn: A
    steps:
      # - script: echo Hello B
      - template: start.yaml

Please Note: We should use the task Bash to help us set the output variable, because we're going to depend on the task name.

Georgi Koemdzhiev
  • 11,421
  • 18
  • 62
  • 126
Felix
  • 1,104
  • 3
  • 6
  • Thank you for your answer. In my case I need to do the same with a template containing a large number of steps but not a job. My question was if there is a way to run the whole template ... or don't. Hence, your answer is technically correct but does not solve my problem :-).. So upvote from my side but not checked as solved. – maze Mar 13 '21 at 18:11
  • Wouldn't organizing your steps into jobs solve it? And gives you overview of what's going on in the pipeline (saying this without seeing your code ;-) ) – promicro Dec 07 '22 at 10:45
0

I struggled myself with this issue. From my findings, it seems that condition is not possible because it is evaluated at compile-time, whereas the variable is set at runtime.

This is the reason your dynamic variable is not available.

To find more, check Adam's article: https://adamtheautomator.com/azure-devops-variables.

The only way to make it work is to have a condition at the task/template level.

  parameters: $(rebuildLib)  # or ${{ variables.rebuildLib }}

And inside the template (steps_lib_build.yml):

- task: ...
  condition: ne('${{ parameters.rebuildLib }}', '')
Andy
  • 35
  • 3
  • not sure this would even work. ${{ parameters.rebuildLib }} will be evaluated at COMPILE time which won't have the value set – Jason Aug 29 '23 at 21:20