Answers: <p>No hacks are needed; you can use “Output Variables,” which lets you define variables in one job and then use them in other jobs within the same pipeline.</p>
  <p>Here's a quick example:</p>
  <ol>
  <li>In the job where you want to create the variable, use this command: <code>##vso[task.setvariable]</code>. For example:</li>
  </ol>
  <pre class="lang-yaml prettyprint-override"><code>jobs:
  - job: JobA
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: |
        echo &quot;##vso[task.setvariable variable=myOutputVar;isOutput=true]somevalue&quot;
  </code></pre>
  <p>In this case, ﻿JobA is creating an output variable called ﻿myOutputVar and giving it the value ﻿somevalue.
  2. Now, in the job where you want to use that variable, you can reference it by using this syntax: ﻿$(.).</p>
  <pre class="lang-yaml prettyprint-override"><code>jobs:
  - job: JobB
    dependsOn: JobA
    pool:
    vmImage: 'windows-latest'
    variables:
    myVar: $[ dependencies.JobA.outputs['myOutputVar'] ]
    steps:
    - script: |
      echo $(myVar)
  </code></pre>
  <p>In this example, ﻿JobB says, &quot;I need to run after ﻿JobA finishes because I want to use that variable it created.&quot; It grabs the value of ﻿myOutputVar using the syntax ﻿ [ dependencies.JobA.outputs['myOutputVar'] ] and assigns it to a variable called ﻿myVar. Then, you can use ﻿myVar in any of the steps in ﻿JobB.
  Using output variables is a legit non-hack way to pass variables between jobs in different locations within the same pipeline. no hacks required.
  Just make sure to use the ﻿dependsOn keyword to define the order of your jobs, so the variables are ready to go when you need them.</p>
  
Answers: CC BY-SA 4.0
Answers: 2024-04-26T12:41:43.190Z
Answers: Thanks for your answer. I finally came across some something similar to your answer. It appears to be only supported in yaml; which is not the end of the world as devops are actively converting all things to yaml.
Answers: Ross Bush
Answers: CC BY-SA 4.0
Answers: 2024-09-06T10:39:25.487Z
Answers: There are two issues in this answer. 1. the script in JobA has no name to prefix the variables 2. The format of the yaml
Answers: koalabruder
Answers: CC BY-SA 4.0
Answers: 2024-04-26T11:24:08.613Z
Answers: 2024-04-26T11:24:08.613Z
Answers: Thomas Vincent
Answers: <ol>
  <li>The script (step) where you set the variable needs a name to reference (see <code>deployVars</code>).</li>
  <li>When you set the variable you need to use <code>isOutput=true</code></li>
  </ol>
  <pre class="lang-yaml prettyprint-override"><code>- job: JobA
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - checkout: none
    - script: |
        echo &quot;##vso[task.setvariable variable=namespace;isOutput=true]mynamespace&quot;
        echo &quot;##vso[task.setvariable variable=ingressHost;isOutput=true]myingressHost&quot;
      # name defines the prefix for the variables.
      name: deployVars
    - script: |
        echo &quot;$(deployVars.namespace)&quot;
        echo &quot;$(deployVars.ingressHost)&quot;
  
  - job: JobB
    dependsOn: JobA
    pool:
      vmImage: 'ubuntu-latest'
    variables:
      # By using the prefix &quot;deployVars.&quot; you can use the variables in JobB like in JobA
      deployVars.namespace: $[ dependencies.JobA.outputs['deployVars.namespace'] ]
      deployVars.ingressHost: $[ dependencies.JobA.outputs['deployVars.ingressHost'] ]
    steps:
    - checkout: none
    - script: |
        echo &quot;$(deployVars.namespace)&quot;
        echo &quot;$(deployVars.ingressHost)&quot;
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-09-06T10:35:46.153Z
Answers: 2024-09-06T10:35:46.153Z
Answers: koalabruder
Body: <p>Before I start using this hack that I found from an article in 2020, I would like to ask if this is a supported feature or is there an easy solution that I have overlooked.</p>
  <p>PROBLEM: In a single pipeline, I need to get a variable from a task running on a build machine in one location and pass that over to a task running in a job in another location. Is this even possible? See image below:</p>
  <p>Pipeline Variable Definition
  <a href="https://i.sstatic.net/TM0LRcjJ.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/TM0LRcjJ.png" alt="enter image description here" /></a></p>
  <p>Pipeline Tasks
  <a href="https://i.sstatic.net/Z46dwhpm.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Z46dwhpm.png" alt="enter image description here" /></a></p>
  
Tags: <azure-devops><azure-pipelines>
Title: Pass a Pipeline Variable Between Jobs
UserDisplayName: Ross Bush
ThreadText: # Pass a Pipeline Variable Between Jobs
  
  (Asked by: username_0 on 2024-04-25)
  
  Before I start using this hack that I found from an article in 2020, I would like to ask if this is a supported feature or is there an easy solution that I have overlooked.
  
  PROBLEM: In a single pipeline, I need to get a variable from a task running on a build machine in one location and pass that over to a task running in a job in another location. Is this even possible? See image below:
  
  Pipeline Variable Definition
  [![enter image description here](https://i.sstatic.net/TM0LRcjJ.png)](https://i.sstatic.net/TM0LRcjJ.png)
  
  Pipeline Tasks
  [![enter image description here](https://i.sstatic.net/Z46dwhpm.png)](https://i.sstatic.net/Z46dwhpm.png)
  
  **Tags:** azure-devops, azure-pipelines
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2024-04-26. Score: 1 | Accepted answer
  
  No hacks are needed; you can use “Output Variables,” which lets you define variables in one job and then use them in other jobs within the same pipeline.
  
  Here's a quick example:
  
  1. In the job where you want to create the variable, use this command: `##vso[task.setvariable]`. For example:
  
  ```yaml
  jobs:
  - job: JobA
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - script: |
        echo "##vso[task.setvariable variable=myOutputVar;isOutput=true]somevalue"
  
  ```
  
  In this case, ﻿JobA is creating an output variable called ﻿myOutputVar and giving it the value ﻿somevalue.
  2. Now, in the job where you want to use that variable, you can reference it by using this syntax: ﻿$(.).
  
  ```yaml
  jobs:
  - job: JobB
    dependsOn: JobA
    pool:
    vmImage: 'windows-latest'
    variables:
    myVar: $[ dependencies.JobA.outputs['myOutputVar'] ]
    steps:
    - script: |
      echo $(myVar)
  
  ```
  
  In this example, ﻿JobB says, "I need to run after ﻿JobA finishes because I want to use that variable it created." It grabs the value of ﻿myOutputVar using the syntax ﻿ [ dependencies.JobA.outputs['myOutputVar'] ] and assigns it to a variable called ﻿myVar. Then, you can use ﻿myVar in any of the steps in ﻿JobB.
  Using output variables is a legit non-hack way to pass variables between jobs in different locations within the same pipeline. no hacks required.
  Just make sure to use the ﻿dependsOn keyword to define the order of your jobs, so the variables are ready to go when you need them.
  
  > **Comments:**
  > **username_0**: Thanks for your answer. I finally came across some something similar to your answer. It appears to be only supported in yaml; which is not the end of the world as devops are actively converting all things to yaml.  
  > **username_2**: There are two issues in this answer. 1. the script in JobA has no name to prefix the variables 2. The format of the yaml  
  
  ---
  
  ### Answer by username_2 on 2024-09-06. Score: 0
  
  1. The script (step) where you set the variable needs a name to reference (see `deployVars`).
  2. When you set the variable you need to use `isOutput=true`
  
  ```yaml
  - job: JobA
    pool:
      vmImage: 'ubuntu-latest'
    steps:
    - checkout: none
    - script: |
        echo "##vso[task.setvariable variable=namespace;isOutput=true]mynamespace"
        echo "##vso[task.setvariable variable=ingressHost;isOutput=true]myingressHost"
      # name defines the prefix for the variables.
      name: deployVars
    - script: |
        echo "$(deployVars.namespace)"
        echo "$(deployVars.ingressHost)"
  
  - job: JobB
    dependsOn: JobA
    pool:
      vmImage: 'ubuntu-latest'
    variables:
      # By using the prefix "deployVars." you can use the variables in JobB like in JobA
      deployVars.namespace: $[ dependencies.JobA.outputs['deployVars.namespace'] ]
      deployVars.ingressHost: $[ dependencies.JobA.outputs['deployVars.ingressHost'] ]
    steps:
    - checkout: none
    - script: |
        echo "$(deployVars.namespace)"
        echo "$(deployVars.ingressHost)"
  
  ```
  
  
  ---
  
  