Github Actions dynamic job environment variables

GitHub Actions jobs can dynamically set environment variables with job scope using a run: step to write the variable to an environment file.

Append to PATH: All job steps after the “run:” stanzas have the new PATH value “~/.local/bin” appended. Windows defaults to PowerShell.

- name: set Unix PATH
  if: runner.os != 'Windows'
  run: echo "${HOME}/.local/bin" >> $GITHUB_PATH

- name: set Windows PATH
  if: runner.os == 'Windows'
  run: echo "${HOME}/.local/bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

Any environment variable can be set in this way. Example: set environment variables “CMAKE_INSTALL_PREFIX” and “CMAKE_PREFIX_PATH” to “~/libs” for the following job steps:

- name: set Unix
  if: runner.os != 'Windows'
  run: |
    echo "CMAKE_INSTALL_PREFIX=~/libs" >> $GITHUB_ENV
    echo "CMAKE_PREFIX_PATH=~/libs" >> $GITHUB_ENV    

- name: set Windows
  if: runner.os == 'Windows'
  run: |
    echo "CMAKE_INSTALL_PREFIX=$HOME/libs" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
    echo "CMAKE_PREFIX_PATH=$HOME/libs" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append