Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access GitHub Action environment variables within a Bash script run by the Action?

I'm not able to access environment variables defined at the top-level of a GitHub Action configuration file from within a script run by the action.

For example, given the following config file:

name: x-pull-request
on: pull_request
env:
  FOO: bar
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: does a thing
        run: ./scripts/do-a-thing.sh

... and the following script:

X=${FOO:default}
echo "X: $X" # X: default

The FOO environment variable defined in the config file is not available to the script and the default value is being used.

So, how can I access the environment variable from a Bash script run by the build step? Am I missing a prefix or something? (I know values defined in the input hash require you to use an INPUT_ prefix when referencing them.)

like image 835
pdoherty926 Avatar asked Jan 22 '20 19:01

pdoherty926


People also ask

Which command can you use to view environment variables in bash?

The most used command to displays the environment variables is printenv .

How do I run an environment variable in bash?

In order to set a permanent environment variable in Bash, you have to use the export command and add it either to your “. bashrc” file (if this variable is only for you) or to the /etc/environment file if you want all users to have this environment variable.

How do I run a bash script in GitHub Actions?

Run the bash script from an action using run: bash ${GITHUB_WORKSPACE}/scripts/example.sh my-folder-name and use $1 to use the argument you passed. This makes it easy to re-use a script from multiple actions.


Video Answer


2 Answers

You can use env at any level, since the root action, also in job and step.

I wrote a test action and a test script to validate it. Take a look.

The action file:

name: Env tests
on: push

env:
  FOO_ROOT: bar on root

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      FOO_JOB: bar on job
    steps:
      - uses: actions/checkout@v1
      - name: Test envs
        run: ./env-test.sh
        env:
          FOO_STEP: bar on step

The script file:

#!/usr/bin/env bash

echo "FOO_ROOT: $FOO_ROOT"
echo "FOO_JOB: $FOO_JOB"
echo "FOO_STEP: $FOO_STEP"
echo " "
printenv

The results:

FOO_ROOT: bar on root
FOO_JOB: bar on job
FOO_STEP: bar on step

LEIN_HOME=/usr/local/lib/lein
M2_HOME=/usr/share/apache-maven-3.6.3
...

Check mine results here.

In fact, I don't know why it didn't work on you side, because, it must work.

like image 154
Tiago Gouvêa Avatar answered Oct 24 '22 05:10

Tiago Gouvêa


If some one else is searching for a solution:

You have to explicity pass the environment variables to the bash script. Otherwise they are not available.

name: x-pull-request
on: pull_request
env:
  FOO: bar
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: does a thing
        run: ./scripts/do-a-thing.sh $FOO

And in the Script you have to map the parameter to a variable.

X=$1
echo "X: $X" # X: default

Hope this helps.

like image 30
Mehtrick Avatar answered Oct 24 '22 05:10

Mehtrick