azure-devopsazure-pipelinesdevopsazure-pipelines-yaml

DevOps Yaml pass output variable to template as parameter


I am trying to use output variables to pass values between stages. I create an output variable and populate it.

In the second stage I use the variable inside a script and it works as expected.

However, when I try to pass this into the template it doesn't work. How do I pass this value into the template?

stages:
  - stage: AllocateEnvironment
    jobs:
      - job: AllocateEnvironment
        steps:
          - script: |
              environment="test"
              echo "##vso[task.setvariable variable=Environment;isOutput=true]$environment"

            name: AllocateEnvironment

  - stage: ScaleUpEnvironment
    dependsOn: AllocateEnvironment
    variables:
      Environment: $[stageDependencies.AllocateEnvironment.AllocateEnvironment.outputs['AllocateEnvironment.Environment']]
    jobs:
      - job: ScaleUpEnvironment
        displayName: 'Scale up environment'
        steps:
          - script: |
              echo "Environment: $(Environment)"
          - template: scaler.yaml
            parameters:
              environment: $[stageDependencies.AllocateEnvironment.AllocateEnvironment.outputs['AllocateEnvironment.Environment']]

Solution

  • To use the value of output variable on the steps in a different stage, you need to use the expression stageDependencies.STAGE.JOB.outputs['TASK.VARIABLE'] to map the output variable as a general variable at a job level. Then on the steps of this job, you can directly call the general variable using the expression $(VarName) to pass the value.

    For you case, there are two ways to pass the value of output variable to the step template. See below examples as reference.

    1. Directly call the general variable mapped for the output variable in the step template.

    stages:
    - stage: A
      displayName: 'Stage A'
      jobs:
      - job: A1
        displayName: 'Job A1'
        steps:
        - task: Bash@3
          displayName: 'Set Output Variable'
          name: setOutputVar
          inputs:
            targetType: inline
            script: echo "##vso[task.setvariable variable=outVar;isoutput=true]op1234"
    
    - stage: B
      displayName: 'Stage B'
      dependsOn: A
      jobs:
      - job: B1
        displayName: 'Job B1'
        variables:
          outVarFromA: $[ stageDependencies.A.A1.outputs['setOutputVar.outVar'] ]
        steps:
        - task: Bash@3
          displayName: 'Print outVarFromA'
          inputs:
            targetType: inline
            script: echo "outVarFromA = $(outVarFromA)"
        
        - template: Step-Templates/insert-step.yml
    
    steps:
    - task: Bash@3
      displayName: 'A template step'
      inputs:
        targetType: inline
        script: |
          echo "This is a template step."
          echo "outVarFromA = $(outVarFromA)"
    

    enter image description here

    2. Pass the value from the general variable to a parameter in the step template. Just like as you are doing.

    stages:
    - stage: A
      displayName: 'Stage A'
      jobs:
      - job: A1
        displayName: 'Job A1'
        steps:
        - task: Bash@3
          displayName: 'Set Output Variable'
          name: setOutputVar
          inputs:
            targetType: inline
            script: echo "##vso[task.setvariable variable=outVar;isoutput=true]op1234"
    
    - stage: B
      displayName: 'Stage B'
      dependsOn: A
      jobs:
      - job: B1
        displayName: 'Job B1'
        variables:
          outVarFromA: $[ stageDependencies.A.A1.outputs['setOutputVar.outVar'] ]
        steps:
        - task: Bash@3
          displayName: 'Print outVarFromA'
          inputs:
            targetType: inline
            script: echo "outVarFromA = $(outVarFromA)"
        
        - template: Step-Templates/insert-step.yml
          parameters:
            MyVar: $(outVarFromA)
    
    parameters:
    - name: MyVar
      type: string
    
    steps:
    - task: Bash@3
      displayName: 'A template step'
      inputs:
        targetType: inline
        script: |
          echo "This is a template step."
          echo "MyVar = ${{ parameters.MyVar }}"
    

    enter image description here