Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an env var with a bash expression in GitHub Actions?

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.

like image 585
evilSnobu Avatar asked Sep 17 '19 06:09

evilSnobu


People also ask

How do I set an environment variable in GitHub Actions?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

How do I set environment variables in Git bash?

You must add and edit the “. bashrc” file in the home directory to export or change the environment variable. Then, to make the changes take effect, source the file. The variable (in our case, 'CD') would then become active.

How do I add an environment variable to GitHub?

(After the setting, you can use environment variables like this in your project.) To add a secret to your repository, go to your repository's Setting > Secrets , click on Add a new secret .

How do I set an environment variable in shell?

To set an environment variable everytime, use the export command in the . bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script. If you execute the script it will not work.


1 Answers

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 }} 
like image 132
peterevans Avatar answered Sep 18 '22 08:09

peterevans