Answers: <h1>Using <code>$GITHUB_OUTPUT</code></h1>
  <ol>
  <li><strong>Write to <code>GITHUB_OUTPUT$</code>:</strong> This is the only supported method to <a href="https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/passing-information-between-jobs" rel="nofollow noreferrer">pass data between jobs</a>
  <pre class="lang-yaml prettyprint-override"><code>myenvprep:
    runs-on: ubuntu-latest
    steps:
      - name: Prepare Environment
        id: myenvprep
        run: |
          echo &quot;mykey=myvalue&quot; &gt;&gt; &quot;$GITHUB_OUTPUT&quot;
  </code></pre>
  </li>
  <li><strong>Export the output variable by mapping the output variable to the job's <code>outputs:</code>:</strong> This makes the key <code>mykey</code> available to be stored in an output variable; but, you must still export that output variable. To do that you need to add something like this to the job.
  <pre class="lang-yaml prettyprint-override"><code>myenvprep:
    runs-on: ubuntu-latest
    outputs:
      myoutput: ${{ steps.myenvprep.outputs.mykey }}
    steps:
      - name: Prepare Environment
        id: myenvprep
        run: |
          echo &quot;mykey=myvalue&quot; &gt;&gt; &quot;$GITHUB_OUTPUT&quot;
  </code></pre>
  </li>
  <li><strong>Map the output variable to the job's <code>env:</code></strong> now you can reference the <code>output</code> in the <code>env:</code> declaration for subsequent jobs,
  <pre class="lang-yaml prettyprint-override"><code>otherjob:
    runs-on: ubuntu-latest
    needs: myenvprep
    steps:
      - name: Retrieve Env
        env:
          MYKEY: ${{ needs.myenvprep.outputs.myoutput }}
        run: |
          echo $MYKEY
  </code></pre>
  </li>
  </ol>
  <h2>Distinct Names for Everything</h2>
  <p>So an MVE of this looks like this, but the names are only kept distinct for demonstration. In production, you'd likely just use <code>env</code> for all these distinct names (demonstrated below)</p>
  <pre class="lang-yaml prettyprint-override"><code>on: push
  
  jobs:
    myenvprep:
      runs-on: ubuntu-latest
      outputs:
        myoutput: ${{ steps.myenvprep.outputs.mykey }}
      steps:
        - name: Prepare Environment
          id: myenvprep
          run: |
            echo &quot;mykey=myvalue&quot; &gt;&gt; &quot;$GITHUB_OUTPUT&quot;
    otherjob:
      runs-on: ubuntu-latest
      needs: myenvprep
      steps:
        - name: Retrieve Env
          env:
            MYKEY: ${{ needs.myenvprep.outputs.myoutput }}
          run: |
            echo $MYKEY
  </code></pre>
  <h1>Collapsing Names for Simplicity</h1>
  <p>That the names were kept distinct is just to illustrate the concept. This is how it should look in production,</p>
  <pre class="lang-yaml prettyprint-override"><code>on: push
  
  jobs:
    env:
      name: Generate Environment
      runs-on: ubuntu-latest
      outputs:
        mykey: ${{ steps.env.outputs.mykey }}
      steps:
        - name: Prepare And Save Environment
          id: env
          run: |
            echo &quot;mykey=myvalue&quot; &gt;&gt; &quot;$GITHUB_OUTPUT&quot;
    build:
      name: Do Buildy Things
      runs-on: ubuntu-latest
      needs: env
      steps:
        - name: Retreive Env
          env:
            MYKEY: ${{ needs.env.outputs.mykey }}
          run: |
            echo $MYKEY
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2024-11-25T02:44:46.167Z
Answers: 2024-11-25T02:51:07.407Z
Answers: 2024-11-25T02:51:07.407Z
Answers: Evan Carroll
Body: <p>This question is a <a href="https://devops.stackexchange.com/q/19897/18965">follow-up to this question</a>, where I learned that writes to <code>GITHUB_ENV</code> do not persist across jobs. If <code>$GITHUB_ENV</code> will only affect subsequent steps  in the same job, then how do we work around this? I want a dependent job to makes changes to <code>$GITHUB_ENV</code> which subsequent jobs will inherit.</p>
  
Tags: <github-actions><environment-variables>
Title: How do you make the environment persist across jobs in GitHub Actions?
UserDisplayName: Evan Carroll
ThreadText: # How do you make the environment persist across jobs in GitHub Actions?
  
  (Asked by: username_0 on 2024-11-25)
  
  This question is a [follow-up to this question](https://devops.stackexchange.com/q/19897/18965), where I learned that writes to `GITHUB_ENV` do not persist across jobs. If `$GITHUB_ENV` will only affect subsequent steps in the same job, then how do we work around this? I want a dependent job to makes changes to `$GITHUB_ENV` which subsequent jobs will inherit.
  
  **Tags:** github-actions, environment-variables
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2024-11-25. Score: 0
  
  # Using `$GITHUB_OUTPUT`
  
  1. **Write to `GITHUB_OUTPUT$`:** This is the only supported method to [pass data between jobs](https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/passing-information-between-jobs)
  
     ```yaml
     myenvprep:
       runs-on: ubuntu-latest
       steps:
         - name: Prepare Environment
           id: myenvprep
           run: |
             echo "mykey=myvalue" >> "$GITHUB_OUTPUT"
  
     ```
  2. **Export the output variable by mapping the output variable to the job's `outputs:`:** This makes the key `mykey` available to be stored in an output variable; but, you must still export that output variable. To do that you need to add something like this to the job.
  
     ```yaml
     myenvprep:
       runs-on: ubuntu-latest
       outputs:
         myoutput: ${{ steps.myenvprep.outputs.mykey }}
       steps:
         - name: Prepare Environment
           id: myenvprep
           run: |
             echo "mykey=myvalue" >> "$GITHUB_OUTPUT"
  
     ```
  3. **Map the output variable to the job's `env:`** now you can reference the `output` in the `env:` declaration for subsequent jobs,
  
     ```yaml
     otherjob:
       runs-on: ubuntu-latest
       needs: myenvprep
       steps:
         - name: Retrieve Env
           env:
             MYKEY: ${{ needs.myenvprep.outputs.myoutput }}
           run: |
             echo $MYKEY
  
     ```
  
  ## Distinct Names for Everything
  
  So an MVE of this looks like this, but the names are only kept distinct for demonstration. In production, you'd likely just use `env` for all these distinct names (demonstrated below)
  
  ```yaml
  on: push
  
  jobs:
    myenvprep:
      runs-on: ubuntu-latest
      outputs:
        myoutput: ${{ steps.myenvprep.outputs.mykey }}
      steps:
        - name: Prepare Environment
          id: myenvprep
          run: |
            echo "mykey=myvalue" >> "$GITHUB_OUTPUT"
    otherjob:
      runs-on: ubuntu-latest
      needs: myenvprep
      steps:
        - name: Retrieve Env
          env:
            MYKEY: ${{ needs.myenvprep.outputs.myoutput }}
          run: |
            echo $MYKEY
  
  ```
  
  # Collapsing Names for Simplicity
  
  That the names were kept distinct is just to illustrate the concept. This is how it should look in production,
  
  ```yaml
  on: push
  
  jobs:
    env:
      name: Generate Environment
      runs-on: ubuntu-latest
      outputs:
        mykey: ${{ steps.env.outputs.mykey }}
      steps:
        - name: Prepare And Save Environment
          id: env
          run: |
            echo "mykey=myvalue" >> "$GITHUB_OUTPUT"
    build:
      name: Do Buildy Things
      runs-on: ubuntu-latest
      needs: env
      steps:
        - name: Retreive Env
          env:
            MYKEY: ${{ needs.env.outputs.mykey }}
          run: |
            echo $MYKEY
  
  ```
  
  
  ---
  
  