I want to set an environment variable in the env:
section of a GitHub Action and make use of the Contexts and expression syntax for GitHub Actions. I tried this:
jobs:
build:
runs-on: ubuntu-latest
env:
MYVAR: ${{ format('{0}:{1}', ${{ env.PATH }}, ${{ env.HOME }} ) }}
steps:
- name: Check environment
run: echo $MYVAR
This results in the error message:
### ERRORED 10:45:52Z
- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 10, Col: 14): Unexpected symbol: '${{'. Located at position 19 within expression: format('{0}:{1}', ${{ env.PATH
This syntax:
env:
MYVAR: ${{ format('{0}:{1}', {{ env.PATH }}, {{ env.HOME }} ) }}
results in error:
### ERRORED 13:14:18Z
- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 10, Col: 14): Unexpected symbol: '{{'. Located at position 19 within expression: format('{0}:{1}', {{ env.PATH
and:
env:
MYVAR: ${{ format('{0}:{1}', env.PATH, env.HOME ) }}
results in error:
### ERRORED 13:16:12Z
- Your workflow file was invalid: The pipeline is not valid. .github/workflows/main.yml (Line: 10, Col: 14): Unrecognized named-value: 'env'. Located at position 19 within expression: format('{0}:{1}', env.PATH, env.HOME )
I'm aware of the solutions in How do i set an env var with a bash expression in GitHub Actions? and Github Actions, how to share a calculated value between job steps? for setting environment variables, but I would like to understand the expression syntax.
As @jonrsharpe pointed out, it is not possible to use the env
context in the value of a workflow environment variable. This was discussed here:
https://github.community/t5/GitHub-Actions/How-to-use-env-context/td-p/38951
At the start of a workflow, the env
context doesn't exist yet. This is why you get an error. Moreover, the first step in every job sees an empty env
context, so even if env
existed, the result of printing MYVAR
would have been just :
.
I came to the above conclusions from running some experiments.
env:
MYVAR: ${{ format('{0}:{1}', env.PATH, env.HOME) }}
The last syntax you used is the correct form, but because the env
context doesn't exist yet, the workflow fails to run.
To prove to yourself that the env
context is in fact empty at the first step, try the following job:
jobs
env-dump-context:
runs-on: ubuntu-latest
steps:
- run: echo env is: ${{ toJSON(env) }}
With that being said, it is still possible to what you need:
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: printf "MYVAR=${PATH}:${HOME}" | tee --append "$GITHUB_ENV"
- name: Check environment
run: echo ${{env.MYVAR}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With