ENV vars are missing from actions context

TL:DR One of ENV vars is not dumping in github actions workflow, and I could not figure why.

Currently, I’m working on integrating with our scripts GH Actions, and one task is to hand over environment variables to our deployment scripts so that Ansible can use it
​I made a short test, where I dump all the “env” context. For test purposes, I created the workflow, job, and step variables. Interestingly, when I dump the env context, the step variable is not included in the output.

GHA workflow example:

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
    branches: [ master ]
    branches: [ master ]
# this is a workflow variable
  WORKFLOW_VAR: "workflow variable"
  ANOTHER_WF_VAR: "another environment variable available to all jobs and steps in the workflow"
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
  # This workflow contains a single job called "build"
    # The type of runner that the job will run on
    runs-on: build
    # this is a job variable
      JOB_VAR: "job variable"
      ANOTHER_JOB_VAR: "another environment variable that is available only to all steps in build job"    
    # Steps represent a sequence of tasks that will be executed as part of the job
    # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
    - uses: actions/checkout@v2
    # Runs a single command using the runners shell
    - name: Run a one-line script
      # this is a step variable
        STEP_VAR: "step variable"
        ANOTHER_STEP_VAR: "another environment variable that is available only to the step job"
      run: echo Hello, $STEP_VAR!
    # Runs a set of commands using the runners shell
    - name: Run a multi-line script
      run: |
        echo Add other actions to build,
        echo test, and deploy your project.
        echo $ANOTHER_JOB_VAR
        uname -a
    - name: Dump env context
        STEP_VAR_FOR_ENV_DUMP: "this is a step var for the env context dump step"
        ENV_CONTEXT: ${{ toJson(env) }}
      run: echo "$ENV_CONTEXT"
    - name: Dump GitHub context
        STEP_VAR_FOR_DUMP: "step var only for the GitHub context dump step"
        GITHUB_CONTEXT: ${{ toJson(github) }}
      run: echo "$GITHUB_CONTEXT"

Run results:

Dump env context step, should contain STEP_VAR_FOR_ENV_DUMP, but it does not.
Any ideas why?

I believe it’s because the new environment variable defined in the step is not yet in env context at time of execution toJson(env).

You can try echo "$STEP_VAR_FOR_ENV_DUMP" instead and I bet that will work.

Hi @Troush,

Glad to see you in Github Community Forum!

The step level env only works in that step where it defines, it will not be added into GITHUB_CONTEXT.
But you can find it in ‘env’ setting, and use $varname to invoke it.

To add env to Github Context in step, please use “set-env” command:

    - name: add env var to github context
      run: |
          echo "::set-env name=action_state::yellow"
    - name: Dump env context for check
        ENV_CONTEXT: ${{ toJson(env) }}
      run: |
         echo "$ENV_CONTEXT"


1 Like