|
8 | 8 |
|
9 | 9 | # GitHub Action - Store variables between your jobs
|
10 | 10 |
|
| 11 | +## Overview |
| 12 | + |
| 13 | +This GitHub Action was originally created **in 2021** to allow you to **store variables** in a global store and then **read them in |
| 14 | +later jobs**—something that was not natively possible in GitHub Actions. It automatically adds read variables to |
| 15 | +your `${{ env }}` so that they become available for subsequent steps. |
| 16 | + |
| 17 | +## But… GitHub Actions native outputs make this library largely unnecessary! |
| 18 | + |
| 19 | +### What Are Native Outputs? |
| 20 | + |
| 21 | +GitHub Actions now provides native support for sharing data between jobs through the use of step outputs and job |
| 22 | +outputs. You can: |
| 23 | + |
| 24 | +- **Set a step output:** Write key/value pairs to `$GITHUB_OUTPUT` in a step. |
| 25 | +- **Define job outputs:** Map outputs from a step to a job-level output. |
| 26 | +- **Access job outputs:** In downstream jobs, access these outputs using the `needs` context. |
| 27 | + |
| 28 | +Similarly, the `$GITHUB_ENV` file allows you to persist environment variables across steps in the same job. |
| 29 | + |
| 30 | +Source: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/passing-information-between-jobs |
| 31 | + |
| 32 | +### Why Is This a Game Changer? |
| 33 | + |
| 34 | +Originally, this library provided a way to emulate global variable sharing: |
| 35 | + |
| 36 | +- **Before:** GitHub Actions did not support sharing environment variables between jobs. |
| 37 | +- **Now:** Native outputs let you write a variable in one job and read it in another without extra actions or artifacts. |
| 38 | + |
| 39 | +This native support simplifies your workflows, reduces dependencies, and improves reliability. |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## Converting a Use Case |
| 44 | + |
| 45 | +### Old Approach (Using `UnlyEd/github-action-store-variable`) - ⚠️ DEPRECATED |
| 46 | + |
| 47 | +Below is an example of how you might have stored and retrieved a variable with the library: |
| 48 | + |
| 49 | +```yaml |
| 50 | +jobs: |
| 51 | + compute-data: |
| 52 | + runs-on: ubuntu-22.04 |
| 53 | + steps: |
| 54 | + - name: Compute data |
| 55 | + run: | |
| 56 | + MY_VAR="Hello, World!" |
| 57 | + echo "MY_VAR=$MY_VAR" >> $GITHUB_ENV |
| 58 | +
|
| 59 | + - name: Store variable using the library |
| 60 | + uses: UnlyEd/github-action-store-variable@v2.1.0 |
| 61 | + with: |
| 62 | + variables: | |
| 63 | + MY_VAR=${{ env.MY_VAR }} |
| 64 | +
|
| 65 | + use-data: |
| 66 | + runs-on: ubuntu-22.04 |
| 67 | + needs: compute-data |
| 68 | + steps: |
| 69 | + - name: Retrieve variable using the library |
| 70 | + uses: UnlyEd/github-action-store-variable@v2.1.0 |
| 71 | + with: |
| 72 | + variables: | |
| 73 | + MY_VAR |
| 74 | + - name: Use variable |
| 75 | + run: echo "MY_VAR is $MY_VAR" |
| 76 | +``` |
| 77 | +
|
| 78 | +### New Approach (Using Native Outputs) |
| 79 | +Here’s how you can achieve the same result without an external action: |
| 80 | +
|
| 81 | +```yaml |
| 82 | +jobs: |
| 83 | + compute-data: |
| 84 | + runs-on: ubuntu-22.04 |
| 85 | + outputs: |
| 86 | + MY_VAR: ${{ steps.set-output.outputs.MY_VAR }} |
| 87 | + steps: |
| 88 | + - name: Compute data |
| 89 | + run: | |
| 90 | + MY_VAR="Hello, World!" |
| 91 | + echo "MY_VAR=$MY_VAR" >> $GITHUB_ENV |
| 92 | +
|
| 93 | + - name: Set step output |
| 94 | + id: set-output |
| 95 | + run: | |
| 96 | + # Export MY_VAR as a step output, so it can be mapped to the job output |
| 97 | + echo "MY_VAR=${MY_VAR}" >> $GITHUB_OUTPUT |
| 98 | +
|
| 99 | + use-data: |
| 100 | + runs-on: ubuntu-22.04 |
| 101 | + needs: compute-data |
| 102 | + steps: |
| 103 | + - name: Use variable from job outputs |
| 104 | + run: echo "MY_VAR is ${{ needs.compute-data.outputs.MY_VAR }}" |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +# Former documentation |
| 111 | + |
11 | 112 | ## Code snippet example (minimal example)
|
12 | 113 |
|
13 | 114 | ```yaml
|
|
0 commit comments