How to pass environment variable received from GitHub actions
I think you are trying to read the wrong environment variable name. GitHub Actions adds INPUT_
to the name of the input variable. So try the following:
print(os.getenv('INPUT_TEST_VAR'))
From the documentation:
When you specify an input to an action in a workflow file or use a default input value, GitHub creates an environment variable for the input with the name INPUT_. The environment variable created converts input names to uppercase letters and replaces spaces with _ characters.
For example, if a workflow defined the numOctocats and octocatEyeColor inputs, the action code could read the values of the inputs using the INPUT_NUMOCTOCATS and INPUT_OCTOCATEYECOLOR environment variables.
https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs
Keep env vars secret by specifying them in Settings -> Secrets
in the repo, and then calling them in the workflow:
For example, consider a workflow that runs an R
script followed by a Python
script. First, in .github/workflows/my_job.yml
notice the MY_VAR
variable, which points to a stored secret with ${{ secrets.MY_VAR}}
. The rest is standard code (run on cron, specify Ubuntu OS and Docker image, define workflow steps).
on:
schedule:
- cron: '0 17 * * *'
jobs:
my_job:
name: my job
env:
MY_VAR: ${{ secrets.MY_VAR }}
runs-on: ubuntu-18.04
container:
image: docker.io/my_username/my_image:my_tag
steps:
- name: checkout_repo
uses: actions/checkout@v2
- name: run some code
run: bash ./src/run.sh
Next, in the scripts that compose your workflow, you can access the env var specified in the workflow file above as you would locally.
For example, in the repo, let's assume src/run.sh
calls an R
script followed by a Python
script.
In R
access the env var and store as an object:
my_var <- Sys.getenv("MY_VAR")
.
.
.
In Python
access the env var and store as an object:
import os
my_var = os.getenv("MY_VAR")
.
.
.
See the docs here.
A bit late but for the next one, you can also use the env
field :
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Test the GH action
uses: paramt/github-actions-playground@master
env:
test_var: "this is just a test"
which will be included during the creation of your docker and pass without the prefix INPUT_