Search code examples
githubgithub-actions

GitHub Action Set $GITHUB_ENV Not Saving


Per the documentation found here, I have tried setting a GitHub Action environment variable but noticed that the env variables don't seem to save. Has this been deprecated or is the documentation / my implementation just incorrect?

- name: Get Gradle VersionName
  working-directory : ${{ github.workspace }}/app
  run : |
    echo "Get Gradle VersionName"
    echo "action_state=yellow" >> $GITHUB_ENV
    echo "${{ env.action_state }}"
    grep 'versionName' build.gradle | awk '{print $2}'

enter image description here


Solution

  • This behaviour is expected since "the step that creates or updates the environment variable does not have access to the new value, but all subsequent steps in a job will have access." (documentation)

    To illustrate, consider the following definition of steps:

    steps:
       - name: Step 1
         run: |
            echo "action_state=yellow" >> $GITHUB_ENV
            echo "State is: '${{ env.action_state }}'" # No output since same step
       - name: Step 2
         run: |
            echo "State is: '${{ env.action_state }}'" # Output works
    

    Which results in this output:

    result