9
votes

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.

2
Why have you put ${{ inside ${{? Also note that format takes 0-based indices.jonrsharpe
Because it says under help.github.com/en/actions/… : "Contexts are a way to access information about workflow runs, runner environments, jobs, and steps. Contexts use the expression syntax. {{ <context> }}"stm
None of the examples on that page, including those using contexts, have more than one ${{. I'd guess you want ${{ format('{0}:{1}', env.PATH env.HOME) }}.jonrsharpe
Thanks for the information about the 0-based index, I missed that. But it doesn't help. With the syntax you proposed I get: "- 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 )"stm
Have you done research around that new error message, then?jonrsharpe

2 Answers

3
votes

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

1
votes

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}}