6

I want to use environment variables at the job level. Is there a way to do it?

env:
  stageEnv: UAT

jobs:
  name: Upload Build
  if: ${{ env.stageEnv == 'UAT' }}
  steps:
    ....

I get unrecognized named-value: 'env' error. Tried $stageEnv and ${{ env.stageEnv }}

Note: It works when I access within 'steps', but would like this to be accessible at 'jobs' level.

msg
  • 1,480
  • 3
  • 18
  • 27

1 Answers1

1

I'm afraid not, but you can do like this:

env:
  stageEnv: UAT

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest         
    outputs:
      stageEnv: ${{ steps.init.outputs.stageEnv }}
    
    steps:        
      - name: Make environment variables global 
        id: init
        run: |
          echo "stageEnv=${{ env.stageEnv }}" >> $GITHUB_OUTPUT

And use it in another job:

  upload:
    name: Upload build
    needs: build
    if: ${{ needs.build.outputs.stageEnv == 'UAT' }}

Note this is just an example, and I personally prefer environment variables uppercase and output variables lowercase

Baked Inhalf
  • 3,375
  • 1
  • 31
  • 45