How to use system environment variable in cache actions path?

I am trying to access the Windows environment variable ProgramFiles to pass it to the GitHub Cache Action: https://github.com/evandrocoan/TestGithubActions/actions/runs/63543924/workflow#L37

name: CI
on: [push, pull_request]

jobs:
  tests:
    name: Test ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        include:
          - os: ubuntu-latest
            SEP: /
            PIP_WHEELS_DIR: ~/.cache/pip
            CARGO_INDEX_DIR: ~/.cargo/git
            CARGO_REGISTRY_DIR: ~/.cargo/registry

          - os: macos-latest
            SEP: /
            PIP_WHEELS_DIR: ~/Library/Caches/pip
            CARGO_INDEX_DIR: ~/.cargo/git
            CARGO_REGISTRY_DIR: ~/.cargo/registry

          - os: windows-latest
            SEP: \
            PIP_WHEELS_DIR: ~\AppData\Local\pip\Cache
            CARGO_INDEX_DIR: C:\Rust\.cargo\git
            CARGO_REGISTRY_DIR: C:\Rust\.cargo\registry

    steps:
      - uses: actions/checkout@v2

      - name: Cache git
        if: matrix.os == 'windows-latest'
        uses: actions/cache@v1
        with:
          path: ${{ env.ProgramFiles }}${{ matrix.SEP }}Git
          key: ${{ runner.os }}-git-${{ hashFiles('**/main.yml') }}-1-

      - run: echo Print something.
      - run: echo On ubuntu-latest
        if: matrix.os == 'ubuntu-latest'

But when it runs, the variable is empty: https://github.com/evandrocoan/TestGithubActions/runs/535518431?check_suite_focus=true#step:9:2

C:\windows\System32\tar.exe -cz -f d:\a\_temp\7e3aad1e-aade-4599-af14-17c2f935a571\cache.tgz -C d:\Git .

How to access system-defined variables?

Hi @evandrocoan , 

In default, system environment variable is not included in env context, you could use set-env command to set the value of system ProgramFiles to an job env. And then you it with  ${{ env.ProgramFilesPath }}  syntax. 

Please see my example:

name: CI

on:
  push:
     paths:
       - '**/main.yml'

jobs:
  run_tests:
    runs-on: windows-latest
    steps:
      # env context is empty at the beginning
      - name: Dump env context
        env:
          ENV_CONTEXT: ${{ toJson(env) }}
        run: write-host "$env:ENV_CONTEXT1"
      # set system ProgramFiles value to env ProgramFilesPath
      - name: set env ProgramFiles
        shell: cmd
        run: |
           echo %programfiles%
           echo ::set-env name=ProgramFilesPath::%programfiles%
      # check env context again, now include ProgramFilesPath
      - name: Dump env context 2
        env:
          ENV_CONTEXT1: ${{ toJson(env) }}
        run: write-host "$env:ENV_CONTEXT1"
      # Now you can use env.ProgramFilesPath
      - name: Cache git
        uses: actions/cache@v1
        with:
          path: ${{ **env.ProgramFilesPath** }}\Git
          key: ${{ runner.os }}-git-${{ hashFiles('**/main.yml') }}-1-
1 Like