Nick's Blog

Azure DevOps pipeline – Pass variable from sub-template to the main pipeline

I was working on an Azure DevOps pipeline. I have a requirement to run some logic in the part of pipeline and decide what need to be run afterwards.

The scenario is I have a mono repo include all my manifests for multiple clusters. But they are all sharing 90% of the code. So I bring up all of them into one pipeline and use git to figure out which environment file has been changed, therefore decide to deploy into which environment.

I figured that when you set a variable by using the following, let’s say to make it universal then update the variable when running the pipeline. It didn’t work as expected.

<pre>
variables:
  DEV: 'bla'
- task: Bash@3
  displayName: Test1
  inputs:
    targetType: 'inline'
    script: |
      ${{ variables.NSPDEV }}="123" # This will not work since you dont have permission to modify the variable.
</pre>

Then I found there is a way to use a task to assign the variable and make it as output and use the output as the condition to trigger the pipeline.

echo "##vso[task.setvariable variable=DEV;isOutput=true]deploy"

You just need to add the following condition to the task/job that you want to trigger and it should just work

    condition: eq(dependencies.getEnvironment.outputs['gitdiff.DEV'], 'deploy')