Print variable set in $GITHUB_ENV

How do I get the value of an environment variable set in $GITHUB_ENV?

For eg -
echo "CC=clang" >> $GITHUB_ENV

Now I want to do -
$CC --version
but $CC does not exist.

1 Like

Could you post the context from your workflow file? That should work, assuming the second command is in another run step in the same job.

You might want to set it as output so that you can explicitly reference and set it as env instead. But would need to see your workflow to better understand the problem. Do you use PowerShell by any chance?

This is my config file - (Note where I try $CC --version)

No. Bash.

I see the problem there: The $CC --version is part of the same steps the set write CC to GITHUB_ENV. That’s not going to work, you can only use it in later steps. So, remove that command from the “setup compiler” steps, and add one like this after them:

    - name: Check compiler version
      run: '$CC --version'
1 Like

If you only need an environment variable within the current step, then there is no need to involve GITHUB_ENV at all. But I guess some of the commands in later steps take CC and CXX into account?

To make $CC --version work using a single step, you could also set it as regular variable for immediate access:

   - name: Setup compiler on Linux
      run: |
        sudo apt-get update
        if [ "${{ matrix.compiler }}" = "gcc" ]; then
          echo "CC=gcc" >> $GITHUB_ENV
          echo "CXX=g++" >> $GITHUB_ENV
          sudo apt-get install -y clang
          echo "CC=clang" >> $GITHUB_ENV
          echo "CXX=clang++" >> $GITHUB_ENV
        $CC --version
      if: runner.os == 'Linux'
      shell: bash
1 Like

Here is how to do it in powershell 5.1 or newer


      - name: Increment version
        shell: pwsh
        run: |
          $version = [version]((git describe --tags --abbrev=0) -replace 'v','')
          $increment = [version]::New($version.Major,$version.Minor,$version.Build+1)
          "REVISION=$increment" | Add-Content -Path $ENV:GITHUB_ENV

I wouldn’t recommend "REVISION=$increment" | Add-Content -Path $ENV:GITHUB_ENV in PowerShell (Core) below version 6, because of the inconsistent handling of character encoding (and sometimes addition of a byte-order mark):

I suppose in PowerShell Core (pwsh) as available in GitHub hosted runners (version 7.x) it’s fine, because it defaults to BOM-less UTF-8 for everything as far as I know. That would mean this should work just fine:

echo "foo" >> $Env:GITHUB_ENV

Nonetheless, the official recommendation is:

echo "foo" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append

It doesn’t say whether this is for the legacy PowerShell only, but it might.