304

In GitHub Actions, I'd like to evaluate a bash expression and then assign it to an environment variable:

    - name: Tag image
      env:
        GITHUB_SHA_SHORT: ${{ $(echo $GITHUB_SHA | cut -c 1-6) }}
      ..do other things...

However, this naive attempt has failed. According to the docs this doesn't seem to be supported; a somewhat clean workaround would be fine.

2
  • 3
    Maybe set-env would work in a previous step. help.github.com/en/articles/… Commented Sep 17, 2019 at 7:23
  • Not related to your question, but to the example you gave: You should prefer git rev-parse --short $GITHUB_SHA over cut to create a short version of the rev, because it automatically uses the shortest length which doesn't yield collisions. So while a length of 6 may work fine in the beginning, that may not apply anymore when the number of objects in the repository grows. Commented Nov 19, 2024 at 18:41

4 Answers 4

494

The original answer to this question used the Actions runner function set-env. Due to a security vulnerability set-env is being deprecated and should no longer be used.

This is the new way to set environment variables.

name: my workflow
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set env
      run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV
    - name: Test
      run: echo $GITHUB_SHA_SHORT

Setting an environment variable echo "{name}={value}" >> $GITHUB_ENV

Creates or updates an environment variable for any actions running next in a job. The action that creates or updates the environment variable does not have access to the new value, but all subsequent actions in a job will have access. Environment variables are case-sensitive and you can include punctuation.

(From https://help.github.com/en/actions/reference/workflow-commands-for-github-actions#setting-an-environment-variable)

Example using the output to $GITHUB_ENV method:

    echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $GITHUB_ENV

This is an alternative way to reference the environment variable in workflows.

    - name: Test
      run: echo ${{ env.GITHUB_SHA_SHORT }}
Sign up to request clarification or add additional context in comments.

18 Comments

Removed the extra curly brackets (unnecessary as they become part of the value) and added link to doc. Thanks!
@evilSnobu Not sure if it fits your use case, but there is another similar method for passing values to later steps using set-output that I've detailed here. stackoverflow.com/questions/57819539/…
Just a quick note (since this caused me headache). If you are using a Windows/PowerShell environment, you have to use $env:GITHUB_ENV, i.e. something like run: echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> $env:GITHUB_ENV.
BEWARE of the fine print :) "The action that creates or updates the environment variable DOES NOT have access to the new value, but all subsequent actions in a job will have access."
But what about if I want to use this variable in multiple jobs? Is there any way to define a workflow level variable?
|
27
  - name: Set and Retrieve Github ENV variables
    shell: bash
    run: |

      # define variables      
      tests=16
      failures=2

      # set them as GitHub ENV variables
      echo "Tests=$tests" >> $GITHUB_ENV
      echo "Failures=$failures" >> $GITHUB_ENV

      # retrieve these GitHub ENV variables
      echo "${{ env.Failures }} out of ${{ env.Tests }} tests failed on CI"

Output

  2 out of 16 test failed on CI

7 Comments

Nice. Is this documented somewhere, could you add a reference? Does it persist for subsequent steps?
@evilSnobu No documentation, this what I'm using
This isn't working for me. The final output is ` out of tests failed on CI` without the env.Failures and env.Tests values.
What worked for me is adding quotes around $GITHUB_ENV when assigning the variable. That is, ... >> "$GITHUB_ENV". And I could only access the variable in a new step, not within the same step in which I assigned the variable. This is the GitHub docs page that I followed: docs.github.com/en/actions/using-workflows/…
You cannot access environment variables created this way in the same step, they become available in the next step and beyond.
|
3

As described in other answers the use of $GITHUB_ENV is limited to steps within a single job.

If you want to use a variable across multiple jobs there is a solution, but it's not concise.

The steps are:

  1. Define a job that does that actual commands, and saves the result as an output of a step, and that value as an output of the job.
  2. In each job where you need the value, reference the commands job outputs.

Here is an example job to save the output.

jobs:
  command-outputs:
    runs-on: ubuntu-latest
    outputs:
      GITHUB_SHA_SHORT: ${{ steps.commands.outputs.GITHUB_SHA_SHORT }}
    steps:
      - id: commands
        run: |
          echo "GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c 1-6)" >> "$GITHUB_OUTPUT"

Then use that value in another job:

  different-job:
    needs: [command-outputs]
    env:
      GITHUB_SHA_SHORT: ${{ needs.command-outputs.outputs.GITHUB_SHA_SHORT }}

    steps:
      - name: Tag image
        run: git tag v1.2.3 ${{ env.GITHUB_SHA_SHORT }} -m "Tagged"
      # do other things

If you only need the value in one step you can skip the setting of env and use the needs directly.

Comments

-7

The documentation https://docs.github.com/en/free-pro-team@latest/actions/reference/environment-variables#about-environment-variables describes 2 ways of defining environment-variables.

To set custom environment variables, you need to specify the variables in the workflow file. You can define environment variables for a step, job, or entire workflow using the jobs.<job_id>.steps[*].env, jobs.<job_id>.env, and env keywords.

steps:
  - name: Hello world
    run: echo Hello world $FIRST_NAME $middle_name $Last_Name!
    env:
      FIRST_NAME: Mona
      middle_name: The
      Last_Name: Octocat

You can also use the GITHUB_ENV environment file to set an environment variable that the following steps in a workflow can use. The environment file can be used directly by an action or as a shell command in a workflow file using the run keyword.

2 Comments

Ok, but can you evaluate a bash expression under env: FIRST_NAME:?
For me, it works perfectly, but I use env. var. from a python script. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.