Published on

GitHub Actions: Using the Temporary Directory

Authors

When working with GitHub Actions, you might need to store files in a temporary directory.

For such case, GitHub Actions provides temp directory. There are two ways to access this directory:

VariableTypeUsage
runner.tempContext variableUse in YAML contexts
RUNNER_TEMPEnvironment variableUse in shell commands

Both variables return the path to the same temporary directory and

Using ${{ runner.temp }}

Use this in your workflow YAML file where expressions are evaluated:

name: Temp Directory Demo
on:
  push:
    branches: [main]

jobs:
  example-job:
    runs-on: ubuntu-latest
    steps:
      - name: Create and write to a file in temp directory
        run: |
          echo "Hello from the temp directory!" > ${{ runner.temp }}/hello.txt
          cat ${{ runner.temp }}/hello.txt

      - name: Use the file from temp directory
        run: |
          echo "Contents of the file:"
          cat ${{ runner.temp }}/hello.txt

Using $RUNNER_TEMP

The RUNNER_TEMP environment variable is available within shell commands in the run: section of a step:

    name: Temp Directory Demo
    on:
    push:
        branches: [main]

    jobs:
    example-job:
        runs-on: ubuntu-latest
        steps:
        - name: Use RUNNER_TEMP
            run: |
            echo "Hello from RUNNER_TEMP" > $RUNNER_TEMP/file.txt
            cat $RUNNER_TEMP/file.txt

Important Notes:

  • The temporary directory is job-specific and not shared between jobs
  • The directory is cleared at the start and end of each job

Reference

For more details, check the official documentation on GitHub Actions environment variables.

Happy writing files! 🎉