title: Aure pipeline : use output variable in multi-stage parametrised
body: <p>I am facing an issue I could not resolve.</p>
  <p>regarding <a href="https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&amp;tabs=yaml%2Cbatch#use-outputs-in-a-different-stage" rel="nofollow noreferrer">this documentation</a> it seams to be possible to set a variable as output of a job in the first stage, then use it as input to set a job variable in second stage.</p>
  <p>The pbm I'm facing, is that my stages are parametrised, so their name are not constant.</p>
  <p>here my sample code :</p>
  <pre><code>name: mypipeline
  
  parameters:
    - name: Application
      displayName: Application to deploy the front end using front door
      type: string
    - name: stage_vars
      displayName: stage VARS
      type: object
      default:
      - stage_1
      - stage_2
      - none
  trigger: none
  
  stages:
    - stage: Build
      displayName: ${{ parameters.Application }} - Build.
      pool:
        name: Azure Pipelines
        vmImage: windows-2019
      jobs:
      - template: ../templates/job_BuildAndPublish.yml
        parameters:
          BuildId:  $(Build.BuildId)
          importTerratest: false
          importARM: true
  
    - ${{ each stageregion in parameters.stage_vars }}:
      - ${{ if ne(stageregion, 'none') }}:
          - template: ../templates/isolated_web/tf_plan_stage.yml
            parameters:
              BuildId: $(system.BuildId)
              Predecessors: 'Build'
  
          - template: ../templates/isolated_web/tf_apply_stage.yml
            parameters:
              Predecessors: '${{stageregion}}_prepare'
  </code></pre>
  <p>build template is not usefull, but the ones from tf_plan_stage and tf_apply_stage are below :</p>
  <p><strong>tf_plan_stage.yml :</strong></p>
  <pre><code>parameters:
    - name: BuildId
      type: string
      default: $(system.BuildId)
    - name: stage
      type: string
      default: Deploy_prepare
    - name: Predecessors
      type: string
      default: none
  stages:
    - stage: ${{ parameters.stage }}
      ? ${{ if and(ne(parameters.Predecessors, 'previous'), ne(parameters.Predecessors, 'none') ) }}
      : dependsOn: ${{parameters.Predecessors}}
      displayName: ${{ parameters.TF_VAR_STAGE }} Terraform plan &amp; publish artifact for ${{ parameters.TF_VAR_APPLICATION }}
      pool:
        name: Azure Pipelines
        vmImage: windows-2019
      jobs:
      - deployment: PreDeployTerraform
        displayName: ${{ parameters.TF_VAR_STAGE }} Terraform plan &amp; publish artifact     
        environment: PLAN_${{ parameters.TF_VAR_ENVIRONMENT }}
        timeoutInMinutes: 480
        strategy:
          runOnce:
            deploy:
              steps:
              - checkout: none
              # set output planAttempt output available for later Apply job
              - powershell: | 
                  echo &quot;jobAttempt is $(System.JobAttempt)&quot;
                  echo &quot;##vso[task.setvariable variable=planAttempt;isOutput=true;]$(System.JobAttempt)&quot;
                name: setVarJobAttempt
              - powershell: | 
                  echo &quot;variable setVarJobAttempt.planAttempt value is : $(setVarJobAttempt.planAttempt)&quot;
                name: getplanAttemptVar
  </code></pre>
  <p><strong>tf_apply_stage.yml :</strong></p>
  <pre><code>    parameters:
    - name: stage
      type: string
      default: Deploy_prepare
    - name: Predecessors
      type: string
      default: none
  stages:
    - stage: ${{ parameters.stage }}
      ? ${{ if and(ne(parameters.Predecessors, 'previous'), ne(parameters.Predecessors, 'none') ) }}
      : dependsOn: ${{parameters.Predecessors}}
      displayName: ${{ parameters.TF_VAR_STAGE }} download artifact &amp; Terraform apply changes to ${{ parameters.TF_VAR_APPLICATION }}
      pool:
        name: Azure Pipelines
        vmImage: windows-2019
      jobs:
      - deployment: DeployTerraform
        displayName: ${{ parameters.TF_VAR_STAGE }} download artifact &amp; Terraform apply changes
        variables:
          # this one fails in pipeline : &quot;[not recognise&quot;
          planJobAttempt: $[ stageDependencies.[parameters.Predecessors].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt'] ]
          # this one runs with no result in pipeline : planJobAttempt is the string &quot;stageDependencies['parameters.Predecessors'].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt']&quot;
          # planJobAttempt: $[ stageDependencies['parameters.Predecessors'].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt'] ]
        environment: ${{ parameters.TF_VAR_ENVIRONMENT }}
        timeoutInMinutes: 480
        strategy:
          runOnce:
            deploy:
              steps:
                - checkout: none
                
                - powershell: | 
                    echo &quot;jobAttempts outputs are :  $[ stageDependencies.[parameters.Predecessors].PreDeployTerraform.outputs ]&quot;
  
                - powershell: | 
                    echo &quot;jobAttempt is $[ stageDependencies['parameters.Predecessors'].PreDeployTerraform.outputs['setVarJobAttempt.planAttempt'] ]&quot;
  
                - powershell: | 
                    echo &quot;plan file for terraform is : $(pipeline_artifact_folder_download)/$(planJobAttempt)_${{ parameters.Predecessors }}/$(artefact_terraform_plan)_${{ parameters.TF_VAR_STAGE }}&quot;
  </code></pre>
  <p>I tried differents things to get my system.jobAttempts from tf_plan_stage stage to tf_apply_stage stage, but without any success, as the variable in the last stage (tf_apply) seams unable to find the value from a &quot;parametrised&quot; stage name.</p>
  <p>is there a way for that ?</p>
  <p>thank-you for any answers.</p>