GitHub Actions Workflow — Python and Environment Variables

os.environ()

Environment variables could be easily set in a Python script by using the object os.environ() as below:

What is GITHUB_ENV?

GITHUB_ENV is a file that contains all the environment variables for a workflow runner machine. To be more specific, it contains the file path of the runner machine that sets the environment variables directed via workflow commands. An environment variable set in a workflow step could be made available to any subsequent steps of the workflow job by simply writing the variable to this file.

hello.pyimport osenv_file = os.getenv('GITHUB_ENV') # Get the path of the runner file# write to the file
with open(env_file, "a") as env_file:
env_file.write("ENV_VAR=1")
workflow.yamljobs:
execute:
runs-on: ubuntu-20.04
defaults:
run:
working-directory: .

step:
- name: execute hello.py
run: |
output=$(python hello.py)
- name: check env variable
run: |
if [ $ENV_VAR ==1 ]
then
echo "Env variable set"
else
echo "Env variable not set"
exit 1
fi

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinod Chelladurai

Vinod Chelladurai

Principal Data Engineer @ SumUp • M.Sc in Data and Knowledge Engineering • Author of a research publication • Tech Youtuber - Data Engineering Minds • Pianist