Environment variable GITHUB_WORKSPACE unusuable when setting environment variables

I’m doing like so:

- run: echo $GITHUB_WORKSPACE
    - run: echo $npm_config_cache
      env:
        npm_config_cache: ${{ env.GITHUB_WORKSPACE }}/frontend_cache

However rather than the expected output I get:

Run echo $GITHUB_WORKSPACE
/home/runner/work/paulimg/paulimg
Run echo $npm_config_cache
/frontend_cache

So the issue is that when defining environment variables the GITHUB_WORKSPACE environment varialbe is not yet set.  To make things worse the workflow dir isn’t available in any of the other contexts!

3 Likes

“GITHUB_WORKSPACE” is not in the ‘env’ context by default, if you export ‘env’ context ({{ toJson(env) }}), you will find it’s empty, that’s why it cannot get the value of ‘{{ env. GITHUB_WORKSPACE }}’.

envcontext.png

Please use ‘set-env’ to set environment variables, code sample as below:

- name: Set env
        run: echo ::set-env name=workspace::$GITHUB_WORKSPACE
      - name: Dump GitHub context
        env:
          GITHUB_CONTEXT: ${{ toJson(env) }}
        run: echo "$GITHUB_CONTEXT"
      - run: echo $npm_config_cache
        env:
          npm_config_cache: ${{ env.workspace }}/frontend_cache

fixenv.png

Or you can directly use ‘${{ github.workspace }}’ instead, code as below:

- run: echo $npm_config_cache
        env:
          npm_config_cache: ${{ github.workspace }}/frontend_cache
7 Likes

Is this documented anywhere? If not, then quirks like this is exactly why one should avoid a highly immature offering like GH Actions.

https://help.github.com/en/actions/configuring-and-managing-workflows/using-environment-variables#default-environment-variables

list of action variables