10

I define GENERATOR_PLATFORM as an empty environment variable, and then I want to set it to something for my Windows build. But, the variable never gets set:

env:
  GENERATOR_PLATFORM:

 steps:
    - name: windows-dependencies
      if: startsWith(matrix.os, 'windows')
      run: |
         $generator= "-DCMAKE_GENERATOR_PLATFORM=x64"
        echo "Generator: ${generator}"
        echo "GENERATOR_PLATFORM=$generator" >> $GITHUB_ENV

   - name: Configure CMake
      shell: bash
      working-directory: ${{github.workspace}}/build
      run: cmake $GITHUB_WORKSPACE $GENERATOR_PLATFORM
Booga Roo
  • 1,665
  • 1
  • 21
  • 30
Jacko
  • 12,665
  • 18
  • 75
  • 126
  • An important thing to note for anyone coming here because they can't get an environment variable to set in PowerShell: If you set the variable and then get it in the same step, it will not work. [Apparently,](https://github.com/orgs/community/discussions/25713#discussioncomment-3248870) the GITHUB_ENV file isn't updated in the same step. – gcode Mar 07 '23 at 00:05

2 Answers2

18

If you are using a Windows/PowerShell environment, you have to use $env:GITHUB_ENV instead of $GITHUB_ENV:

    echo "GENERATOR_PLATFORM=$generator" >> $env:GITHUB_ENV

This way, you can access your env var through $env:GENERATOR_PLATFORM, eg:

    run: echo $env:GENERATOR_PLATFORM
mklement0
  • 382,024
  • 64
  • 607
  • 775
soltex
  • 2,993
  • 1
  • 18
  • 29
3

To follow up on @soltex answer: The proposed solution only works if the encoding is set to utf-8. If your runner is using Windows PowerShell (i.e. not PowerShell v7+, which uses utf-8 by default), utf16-le is written to the environment file, which causes the variable to not being set.

The correct solution is this:

echo "GENERATOR_PLATFORM=$generator" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

From: https://github.com/actions/runner-images/issues/5251#issuecomment-1071030822

Further reading: Changing PowerShell's default output encoding to UTF-8

Marius
  • 41
  • 2