13

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.

8
  • 1
    Why have you put ${{ inside ${{? Also note that format takes 0-based indices. Commented Jan 19, 2020 at 11:56
  • 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> }}" Commented Jan 19, 2020 at 11:59
  • 2
    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) }}. Commented Jan 19, 2020 at 12:01
  • 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 )" Commented Jan 19, 2020 at 12:05
  • Have you done research around that new error message, then? Commented Jan 19, 2020 at 12:07

2 Answers 2

5

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}}
Sign up to request clarification or add additional context in comments.

Comments

3

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 Comment

It looks like it is being tracked internally to be implemented some day github.community/t/… .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.