21
votes

I'd like to define and set environment variable between jobs inside my Github Actions Workflow. The workflow below is what I've tried but unfortunately the environment variable GIT_PR_SHA_SHORT and E2E_GIT_PR_SHA not working.

Is it possible?

name: Git Pull Request Workflow

on:
  workflow_dispatch:
  pull_request:
    branches:
      - master

env:
  GIT_PR_SHA: ${{github.event.pull_request.head.sha}}
  GIT_PR_SHA_SHORT: "${{ env.GIT_PR_SHA:0:10 }}"
  ENV_NAME: test
  E2E_GIT_PR_SHA: "${{ env.ENV_NAME }}-${{ env.GIT_PR_SHA_SHORT }}"

jobs:
  first-job:
    name: Build Docker Image
    runs-on: ubuntu-latest

    steps:
    - name: First Echo Step
      run: |
            echo "GIT_PR_SHA_SHORT = ${GIT_PR_SHA_SHORT}"
            echo "E2E_GIT_PR_SHA = ${E2E_GIT_PR_SHA}"

  second-job:
    name: Build Docker Image
    runs-on: ubuntu-latest

    steps:
    - name: Second Echo Step
      run: |
            echo "GIT_PR_SHA_SHORT = ${GIT_PR_SHA_SHORT}"
            echo "E2E_GIT_PR_SHA = ${E2E_GIT_PR_SHA}"
3

3 Answers

21
votes

You reference a workflow's environment variables with ${{ env.VARIABLE_NAME }} not ${VARIABLE_NAME}. The latter is bash syntax, but these are not shell environment variables, they're workflow environment variables. They're part of the workflow execution, not part of the shell's context.

To reference a workflow environment variable:

name: Git Pull Request Workflow

on:
  workflow_dispatch:
  pull_request:
    branches:
      - master

env:
  one: 1
  two: zwei
  three: tres

jobs:
  first-job:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "${{ env.one }}"
        echo "${{ env.two }}"
        echo "${{ env.three }}"

(I like to use lower-case for my workflow environment variables, and UPPER_CASE for my shell environment variables, so that it's more obvious to me which is which.)

Similarly, this won't work:

env:
  GIT_PR_SHA_SHORT: "${{ env.GIT_PR_SHA:0:10 }}"

This is mixing bash syntax :0:10 with the workflow syntax, but the workflow variables are not run through any shell. No virtual machine has been started when the workflow file is parsed, so there's no shell to run things though.

If you wanted to use bash expressions to manipulate the environment, you would need to create a step that runs bash to do that, and you would need to use the ::set-env or ::set-output syntax.

Then you can refer to a step's output using the ${{ steps... }} context.

Unfortunately, passing things between different jobs is trickier, since they run on different virtual machines. You'll need to set variables on the overall workflow itself. You'll need to first ::set-output so that it's visible to the job, then you can raise the visibility from the job to the workflow.

name: Demonstration
on:
  push:
    branches: [master]

jobs:
  first-job:
    runs-on: ubuntu-latest  
    steps:
    - id: identify
      run: |
        # use bash variable expression to get the substring
        export GIT_PR_SHA="${{ github.sha }}"
        export GIT_PR_SHA_SHORT="${GIT_PR_SHA:0:10}"

        echo "::set-output name=git_pr_sha::${GIT_PR_SHA}"
        echo "::set-output name=git_pr_sha_short::${GIT_PR_SHA_SHORT}"
    outputs:
      git_pr_sha: ${{ steps.identify.outputs.git_pr_sha }}
      git_pr_sha_short: ${{ steps.identify.outputs.git_pr_sha_short }}

  second-job:
    needs: first-job
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "${{ needs.first-job.outputs.git_pr_sha }}"
        echo "${{ needs.first-job.outputs.git_pr_sha_short }}"
4
votes

I'd like to add an extension to this since I've had similar difficulties finding how to compute & set environment variables for multi-step use.

Below is a basic example of how to push back to the github environment from within a step if processing is needed to compute an environment variable for later use. You can also update existing variables this same way, not just create new.

name: minimal variable example

on:
  push:

env:
  MAJOR:                        "1"
  MINOR:                        "0"
  PATCH:                        "1"

jobs:
  vars-example:
    runs-on: ubuntu-latest

    steps:
      - name: only available local variable
        run: LOCAL_VERSION=${MAJOR}.${MINOR}.${PATCH}

      - name: available across multiple steps
        run: echo "GLOBAL_VERSION=${MAJOR}.${MINOR}.${PATCH}" >> $GITHUB_ENV

      - name: Vars
        run: |
          echo LOCAL_VERSION = $LOCAL_VERSION
          echo GLOBAL_VERSION = $GLOBAL_VERSION

which results in Vars output of

  echo LOCAL_VERSION = $LOCAL_VERSION
  echo GLOBAL_VERSION = $GLOBAL_VERSION
  shell: /usr/bin/bash -e {0}
  env:
    MAJOR: 1
    MINOR: 0
    PATCH: 1
    GLOBAL_VERSION: 1.0.1
LOCAL_VERSION =
GLOBAL_VERSION = 1.0.1
1
votes

You can't use env in an expression under the env element. I don't see another way than using duplicate values instead of an expression.

The env context syntax allows you to use the value of an environment variable in your workflow file. You can use the env context in the value of any key in a step except for the id and uses keys. For more information on the step syntax, see "Workflow syntax for GitHub Actions".

https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#env-context