Github Actions dynamic environment variables

GitHub Actions jobs can have a run: step that dynamically sets environment variables for following steps in that job.

Dynamic job environment variables (such as appending to PATH) are done by writing to environment files.

Append to PATH

To add “~/.local/bin” to PATH with job scope, do like the following examples (distinct behavior for Windows).

The steps after the “run:” stanzas have the new value for the environment variable.

Linux / macOS:

- run: echo "${HOME}/.local/bin" >> $GITHUB_PATH

Windows defaults to PowerShell, so the syntax is distinct from Unix shells:

- run: echo "${HOME}/.local/bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Set arbitrary environment variable

Writing to environment files can set any environment variable. For example, to set environment variable “CMAKE_INSTALL_PREFIX” and “CMAKE_PREFIX_PATH” to “~/libs” for the following job steps:

Linux / macOS:

- run: echo "CMAKE_INSTALL_PREFIX=~/libs" >> $GITHUB_ENV

- run: echo "CMAKE_PREFIX_PATH=~/libs" >> $GITHUB_ENV

Windows:

- run: echo "CMAKE_INSTALL_PREFIX=$HOME/libs" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

- run: echo "CMAKE_PREFIX_PATH=$HOME/libs" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

Note: since jobs don’t normally interact without additional options, advanced techniques would be needed to make a dynamic workflow environment variable.