macos ${{ env.TMPDIR }} notworking in github actions

Viewed 2401

I have a github action which tries to use ${{ env.TMPDIR }} and that doesnt appear to expand to the TMPDIR environment variable.

 - name: DEBUG Workaround GitHub-Actions-MacOS Issue with env.TMPDIR
    run: |
      echo "TMPDIR=$TMPDIR"
      echo "TMPDIR using ENV.TMPDIR=${{ env.TMPDIR }}"

this produces:

TMPDIR=/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/
TMPDIR using ENV.TMPDIR=

EXAMPLE: DEBUG Workaround GitHub-Actions-MacOS Issue with env.TMPDIR

1 Answers

TMPDIR is a shell environment variable provided by bash. You're referring to it correctly with $TMPDIR, which is how you reference an environment variable in bash. It will probably resolve to something like /tmp.

However, you cannot set a shell environment variable in one step of your GitHub Actions workflow, and then use it in another. That's because you'll get a new instance of a shell, and it will have a new environment. This will not work:

- run: FOO="hello"
- run: echo $FOO

However, you can use a GitHub Actions environment variable to persist information in the GitHub Actions environment, instead of in the shell environment. These are two different things, and there's no implicit mixing. That is to say $TMPDIR is not ${{env.TMPDIR}}. These are two different environments.

If you want to persist an environment variable between steps, you will need to set it explicitly, and then you can use it. (Your workflow is not setting the TMPDIR anywhere.)

(I would also encourage you to use a different name, to avoid confusion with the TMPDIR environment variable set by your shell.)

You can set a GitHub Actions environment variable by echoing a name=value pair to a specific file provided to you in a shell environment variable.

In other words:

- run: echo "FOO=hello" >> $GITHUB_ENV
- run: echo ${{ env.FOO }}

Will persist the GitHub Actions environment variable FOO with the value hello. The second run step will print it.

If you want to create, persist and use a temporary directory, you should use the mktemp command. For example, to create a temp directory in one step and set its path to the TEMP_DIR environment variable, write a file into the temp dir in the next step, print the temp directory name in a third step, and show the contents in a final step:

- run: echo "TEMP_DIR=$(mktemp -d)" >> $GITHUB_ENV
- run: echo "hi" >> ${{ env.TEMP_DIR}}/temp_file
- run: echo ${{env.TEMP_DIR}}
- run: ls -Flas ${{ env.TEMP_DIR }}

If you really want to use TMPDIR (and again, I don't think that you should) then you'll need to set it as a GitHub Actions environment variable.

- run: echo $TMPDIR # works
- run: echo ${{ env.TMPDIR }} # does not work
- run: echo "TMPDIR=${TMPDIR}" >> $GITHUB_ENV # sets it into the GitHub Actions environment
- run: echo ${{ env.TMPDIR }} # works
Related