Append PATH in GitHub Actions
GitHub Actions environment variables have distinct scopes:
- Workflow
- Job
- Step
It’s trivial to set static environment variables in each of these scopes. Setting dynamic environment variables can be done for job and step scopes in a straightforward manner. Since jobs don’t normally interact without additional options, advanced techniques would be needed to make a dynamic workflow environment variable.
Workflow
Set static workflow environment variables in GitHub Actions by using env:
at the top level of a “.github/workflows/ci.yml” file like:
name: ci
env:
CMAKE_GENERATOR: Ninja
CC: gcc
Job
Static job environment variables are set like:
jobs:
windows:
runs-on: windows-latest
env:
CMAKE_GENERATOR: Ninja
CC: gcc
Dynamic job environment variables (such as appending to PATH) are done by writing to environment files. For example, 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
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
Step
The syntax to set a step environment variable is quite similar to above:
- run: cmake -B build
env:
CMAKE_GENERATOR: Ninja
CC: gcc