2

How to assign ENV variable to another step ENV block?

I've tried every combination and it doesn't work.

  1. Var is set in one step
- name: Set ENV var
  run: echo "CUSTOM_VAR=IT WORKS" >> $GITHUB_ENV
  1. Var is used in another step via bash script, run block
- name: Use ENV var
  run: echo $CUSTOM_VAR # OK
  1. Var is used in another step via env block - passed to external action
- name: Use ENV var in external action - NOTHING WORKS
  uses: 'some/nodejs/action@main'
  env:
     USE_CUSTOM_VAR: ${{ env.CUSTOM_VAR }} # NOT OK, prints ''
  1. External action is just console.log(process.env)
  • CUSTOM_VAR is available, but USE_CUSTOM_VAR is not.
console.log('CUSTOM_VAR: ', process.env.CUSTOM_VAR); // OK
console.log('USE_CUSTOM_VAR: ', process.env.USE_CUSTOM_VAR); // NOT OK, empty

Example:

      - name: Set ENV var
        run: |
          echo "CUSTOM_VAR=IT WORKS" >> $GITHUB_ENV

      - name: Use ENV var via `env` block
        run: |
          echo $CUSTOM_VAR # OK
          echo ${{ env.CUSTOM_VAR }} # OK
          echo $USE_CUSTOM_VAR # NOT OK, prints ''
          echo ${{ env.USE_CUSTOM_VAR }} # NOT OK, prints ''
          echo $USE_CUSTOM_VAR_2 # NOT OK, prints '$CUSTOM_VAR'
          echo ${{ env.USE_CUSTOM_VAR_2 }} # OK
          echo $USE_CUSTOM_VAR_3 # NOT OK, prints '$GLOBAL_WORKFLOW_ENV'
          echo ${{ env.USE_CUSTOM_VAR_3 }} # OK
        env:
          USE_CUSTOM_VAR: ${{ env.CUSTOM_VAR }}
          USE_CUSTOM_VAR_2: $CUSTOM_VAR
          USE_CUSTOM_VAR_3: $GLOBAL_WORKFLOW_ENV

      - name: Use ENV var via `env` block in external action
        uses: 'some/nodejs/action@main'
        env:
          USE_CUSTOM_VAR: ${{ env.CUSTOM_VAR }} # NOT OK, prints ''
          USE_CUSTOM_VAR_2: $CUSTOM_VAR # NOT OK, prints '$CUSTOM_VAR'
          USE_CUSTOM_VAR_3: $GLOBAL_WORKFLOW_ENV # OK

I've also tried with instead of env.

It is strange that I can echo it in run script, but I can't pass it to external action.

lima_fil
  • 1,744
  • 27
  • 34
  • Are you on Linux or Windows? In Windows the `-run:` blocks are executed by PowerShell and the syntax to read an environment variable is `$env:CUSTOM_VAR` and you need to redirect the command to set a variable to `$env:GITHUB_ENV`, not `$GITHUB_ENV`. See: https://jessehouwing.net/github-actions-workflow-commands-in-cmd/ – jessehouwing Jun 29 '23 at 11:08

2 Answers2

1

There are a few things that can be at fault here:

Script Handler

  1. The default script engine that executes the script.

In GitHub actions the default script interpreter is dependent on 2 things. The Operating system of the Runner. On a Windows runner, the default script interpreter is PowerShell Core (if available) or PowerShell 5. On MacOS and Linux the default is Bash.

  1. The overridden default script engine that executes the script.

A workflow can define a different default for the run: section.

defaults:
  run:
    shell: bash

And a run: section can set the shell explicitly:

- run: echo yay!
  shell: pwsh

In both these cases the syntax to set a variable and the syntax to read an evironmnent variable will be different.

Setting and reading a variable in Bash:

  - run: echo "CUSTOM_VAR=IT WORKS" >> $GITHUB_ENV
  - run: echo $CUSTOM_VAR # OK

Setting and reading a variable in pwsh:

  - run: echo "CUSTOM_VAR=IT WORKS" >> $env:GITHUB_ENV
  - run: echo $env:CUSTOM_VAR

How you pass the value into the other step.

There are multiple ways to assign a value to another step. Some happen when the workflow is evaluated and the value is "inlined". In some cases the value is passed to the environment.

The ${{ env.CUSTOM_VAR }} syntax is evaluated before the step actually executes. The values are "inlined". In the case of a run: section, the value is literally pasted into the script text before it's passed to the interpreter.

The $CUSTOM_VAR syntax is not evaluated by GitHub Actions. The script is sent to the script interpreter as-is. And Bash or PowerShell then evaluates the script. $CUSTOM_VAR is then either taken from the environment (Bash) or from a local variable (PowerShell).

Jobs reset the env

Environment variables are not passed from one job to the next. If your value is set in one job, you need to declare an output variable in order to pass it to another job.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
0

As noted in "Using contexts to access environment variable values", it depends on the context used.

The env context contains environment variables that have been set in a workflow, job, or step.

Try one of the other contexts, after checking their context availability.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250