Support saving environment variables between steps

With https://github.community/t5/GitHub-Actions/YAML-folded-multiline-strings-interpreted-as-multiple-commands/m-p/30876#M593 solved, the only thing standing in the way of reusing a lot of commands between Linux/MacOS and Windows when building C++ with CMake is an option to keep environment variables between steps (currently the environment is reset after each step).

For example, an option transfer-env that, when enabled, saves the environment after each step and restores it in the next step would allow me to remove a lot of duplicate code from reproc’s (https://github.com/DaanDeMeyer/reproc/blob/master/.github/workflows/main.yml) and doctest’s WIP (https://github.com/DaanDeMeyer/doctest/blob/github-actions/.github/workflows/main.yml) Github Actions configs.

I’ve seen that it’s possible to set global environment variables with exportVariable in https://github.com/actions/toolkit which makes me think that adding this is possible. If possible I’d try to implement it myself but I do not think the relevant code is open source.

10 Likes

You can echo out a string that will set an env for the rest of the job, this is exactly what the toolkit method will do.

echo ::set-env name=action_state::yellow

https://help.github.com/en/articles/development-tools-for-github-actions#set-an-environment-variable-set-env

16 Likes

Using set-env I was able to achieve the desired result.

On Windows, VS related environment variables are set by a third-party batch script, so I had to add them with set-env manually after calling the batch script. Instead of filtering the relevant environment variables, I just make every environment variable global using set-env. The script can be found here.

Thanks for creating that script.  I would be a little careful with that script as you are stomping on PATH rather an updating it with append-path.  In most cases it probably won’t be an issue but you never know. 

1 Like

I’ve been going round in cycles with this as I had my set-env like

::set-env VERSION=0.0.21-preview

Which looks like it’s working but it needs to be

::set-env name=VERSION::0.0.21-preview

1 Like

Link has moved to:

https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable

1 Like

How do I actually use these variables?

I’m setting a variable like this in a script:

echo "::set-env name=app_version::$version"

And trying to use it in my workflow:

    - name: Upload Artifacts
      uses: actions/upload-artifact@v2
      with:
        name: app-${{ app_version }}
        path: ${{ github.workspace }}/path-to-upload

But before my action runs I get this error:

Invalid workflow file
The workflow is not valid. .github/workflows/publish.yml (Line: 55, Col: 15): Unrecognized named-value: 'app_version'.

I have tried initialising it at both the job and workflow level like below but I still get the same error:

env:
   app_version: 0.0.0

The set-env command sets an environment variable, so you have to access it via the env context:

      with:
        name: app-${{ env.app_version }}

When inside a run command or some called program use whatever means the programming language offers to access environment variables, e.g. "${app_version}" in bash.