Search code examples
githubgithub-actionsworkflow

How to set or change a global variable in github action?


I have a github action:

name: Test

on:
 workflow_dispatch:

env:
  PREX: false
  

jobs:
  build:
    runs-on: windows-latest    
    steps:
    - uses: actions/checkout@v4
   
    - name: Say Hi
      run: |
        # Print the PREX environment variable
        echo "${{ env.PREX }}"

    - name: Say Hi2  
      if: contains(steps.get-version.outputs.version-suffix, 'beta')
      run: |
        echo "PREX=true" >> $GITHUB_ENV

    - name: Say Hi3
      run: |
        # Print the PREX environment variable
        echo "${{ env.PREX }}"

I want to change Prex variable value to true if my condition is true. But everytime i check it value is false. I expect a true value when i print it in Hi3 section, becasue i changed its value in Hi2 section. I can confirm that my if condition works fine (i can use exit 1 command and workflow will be canceled so if statement works fine but Prex value does not set/change) I removed other codes for simple code.


Solution

  • You are using Powershell on Windows. You need to use $env:GITHUB_ENV to reference it, or set shell: bash instead.

          run: |
            echo "PREX=true" >> $env:GITHUB_ENV
    

    or

          shell: bash
          run: |
            echo "PREX=true" >> $GITHUB_ENV
    

    or

      build:
        runs-on: ubuntu-latest