Skip to content

Instantly share code, notes, and snippets.

@emmanuelnk
Created September 20, 2023 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuelnk/13f418d1c1a7eb08ee72ef60a71ca041 to your computer and use it in GitHub Desktop.
Save emmanuelnk/13f418d1c1a7eb08ee72ef60a71ca041 to your computer and use it in GitHub Desktop.
Extract Job or step output JSON object to GITHUB_ENV in Github Actions

If you have a job or step that has many outputs that you would like to set as envs (in the shell), do this:

name:

workflow_dispatch:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Set some outputs via GITHUB_OUTPUT
        id: set_outputs
        run: |
          echo "VAR_1=some_value_1" >> $GITHUB_OUTPUT
          echo "VAR_2=some_value_2" >> $GITHUB_OUTPUT
          echo "VAR_3=some_value_3" >> $GITHUB_OUTPUT
          echo "VAR_4=some_value_4" >> $GITHUB_OUTPUT
          echo "VAR_5=some_value_5" >> $GITHUB_OUTPUT

      - name: Echo all outputs to GITHUB_ENV
        run: >
          echo '${{ toJSON(steps.set_outputs.outputs) }}' 
          | jq -r 'to_entries[] 
          | "\(.key)=\(.value)"' 
          >> $GITHUB_ENV

      - name: Verify vars in GITHUB_ENV
        run: |
          # Now should log 'some_value_1'
          echo $VAR_1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment