Answers: <p>Refer : <a href="https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&amp;tabs=bash#set-an-output-variable-for-use-in-future-stages" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&amp;tabs=bash#set-an-output-variable-for-use-in-future-stages</a></p>
  <p>Here is a working example :</p>
  <pre><code>stages:
  - stage: A
    jobs:
    - job: A1
      steps:
       - bash: echo &quot;##vso[task.setvariable variable=myStageVal;isOutput=true]this is a stage output variable&quot;
         name: MyOutputVar
  - stage: B
    dependsOn: A
    jobs:
    - job: B1
      variables:
        myStageAVar: $[stageDependencies.A.A1.outputs['MyOutputVar.myStageVal']]
      steps:
        - bash: echo $(myStageAVar)
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2023-10-25T18:47:48.120Z
Answers: 2023-11-01T12:45:08.073Z
Answers: 2023-11-01T12:45:08.073Z
Answers: geeky_girl
Body: <p>I'm writing an Azure Pipeline where I use the <code>targetBranchName</code> of a pull request to define the test environment I'm going to use. Since <code>targetBranchName</code> has no value before execution, I created a &quot;prepare&quot; stage where I capture the value of the variable to use in further stages.</p>
  <pre><code>- stage: prepare
    jobs:
    - job: variables
      steps:
      - checkout: none
      - powershell: Write-Output &quot;##vso[task.setvariable variable=TARGET;isOutput=true;isReadOnly=true]$(system.pullRequest.targetBranchName)&quot; 
  </code></pre>
  <p>Then, in a later stage, I want to use the output variable <code>BRANCH_NAME</code> to find whether the stage should run and if so to load the appropriate group variables. I also would like to capture the value of the output variable in a job variable so that I can refer to it with a more &quot;convenient&quot; name.</p>
  <p>The stage condition seems to be working when tested with manually set values for <code>TARGET</code> in the <code>prepare</code> job, but I can't actually see the value of <code>TARGET</code>, so I added a &quot;print&quot; step and it shows no value for <code>TARGET</code>.</p>
  <p>I'm not shure about where is my mistake, if in how I refer to the output variable to copy its value (<code>stage</code> vs. <code>stageDependencies</code>) or if the syntax for recovering the value of a variable in <code>dependencies</code>/<code>stageDependencies</code> (<code>$()</code>, <code>$[]</code> or <code>${{}}</code>).</p>
  <pre><code>- stage: deploy_to_cint
    dependsOn: prepare
    condition: and(succeeded('prepare'), in(dependencies.prepare.outputs['variables.environment.TARGET'], 'env1', 'env2'))    
    variables:
    - name: TARGET
      value: $(dependencies.prepare.outputs['variables.environment.TARGET'])
    - group: 'EnvironmentCredentials-$(TARGET)'
    jobs:
    - job: debug
      steps:
      - bash: echo This is running for $(TARGET).
  </code></pre>
  
Tags: <azure-pipelines><yaml><variables>
Title: How do I refer in Azure Pipelines to an output variable defined in a previous stage for defining stage conditions and new variables?
UserDisplayName: lpacheco
ThreadText: # How do I refer in Azure Pipelines to an output variable defined in a previous stage for defining stage conditions and new variables?
  
  (Asked by: username_0 on 2023-03-24)
  
  I'm writing an Azure Pipeline where I use the `targetBranchName` of a pull request to define the test environment I'm going to use. Since `targetBranchName` has no value before execution, I created a "prepare" stage where I capture the value of the variable to use in further stages.
  
  ```
  - stage: prepare
    jobs:
    - job: variables
      steps:
      - checkout: none
      - powershell: Write-Output "##vso[task.setvariable variable=TARGET;isOutput=true;isReadOnly=true]$(system.pullRequest.targetBranchName)" 
  
  ```
  
  Then, in a later stage, I want to use the output variable `BRANCH_NAME` to find whether the stage should run and if so to load the appropriate group variables. I also would like to capture the value of the output variable in a job variable so that I can refer to it with a more "convenient" name.
  
  The stage condition seems to be working when tested with manually set values for `TARGET` in the `prepare` job, but I can't actually see the value of `TARGET`, so I added a "print" step and it shows no value for `TARGET`.
  
  I'm not shure about where is my mistake, if in how I refer to the output variable to copy its value (`stage` vs. `stageDependencies`) or if the syntax for recovering the value of a variable in `dependencies`/`stageDependencies` (`$()`, `$[]` or `${{}}`).
  
  ```
  - stage: deploy_to_cint
    dependsOn: prepare
    condition: and(succeeded('prepare'), in(dependencies.prepare.outputs['variables.environment.TARGET'], 'env1', 'env2'))    
    variables:
    - name: TARGET
      value: $(dependencies.prepare.outputs['variables.environment.TARGET'])
    - group: 'EnvironmentCredentials-$(TARGET)'
    jobs:
    - job: debug
      steps:
      - bash: echo This is running for $(TARGET).
  
  ```
  
  **Tags:** azure-pipelines, yaml, variables
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-10-25. Score: 0 | Accepted answer
  
  Refer : <https://learn.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#set-an-output-variable-for-use-in-future-stages>
  
  Here is a working example :
  
  ```
  stages:
  - stage: A
    jobs:
    - job: A1
      steps:
       - bash: echo "##vso[task.setvariable variable=myStageVal;isOutput=true]this is a stage output variable"
         name: MyOutputVar
  - stage: B
    dependsOn: A
    jobs:
    - job: B1
      variables:
        myStageAVar: $[stageDependencies.A.A1.outputs['MyOutputVar.myStageVal']]
      steps:
        - bash: echo $(myStageAVar)
  
  ```
  
  
  ---
  
  