How to set environment variables in GitHub actions using python
Asked Answered
F

4

15

I want to execute a python script to set some environment variables in GitHub actions. I want to use those environment variables later in my GitHub actions steps. My python script looks like:

new_ver = get_version_from_commit(commit_msg)
if new_ver:
    if new_ver == "false":
        os.environ["SHOULD_PUSH"] = "0"
        print("Not pushing the image to k8s")
        exit(0)
    else:
        new_tag = app_name + ":" + str(new_ver)
        os.environ["DOCKER_IMAGE_TAG"] = new_tag
        os.environ["SHOULD_PUSH"] = "1"
        print("New tag: " + new_tag)
        exit(0)

Part of my GitHub actions file, after the execution of the above python script looks like:

- name: Print env var
  run: echo ${{ env.DOCKER_IMAGE_TAG }}
- name: Build and push
  id: docker_build
  uses: docker/build-push-action@v2
  with:
     push: true
     tags: ${{ secrets.DOCKER_REGISTRY }}/${{ env.DOCKER_IMAGE_TAG }}

But using os.environ won't expose the environment variable outside of the python process. How can I fix this ?

File answered 26/11, 2021 at 10:57 Comment(0)
P
26

You cannot set environment variables directly. Instead, you need to write your environment variables into a file, whose name you can get via $GITHUB_ENV.

In a simple workflow step, you can append it to the file like so (from the docs):

echo "{name}={value}" >> $GITHUB_ENV

In python, you can do it like so:

import os

env_file = os.getenv('GITHUB_ENV')

with open(env_file, "a") as myfile:
    myfile.write("MY_VAR=MY_VALUE")

Given this python script, you can set and use your new environment variable like the following:

- run: python write-env.py
- run: echo ${{ env.MY_VAR }}
Patriarchate answered 26/11, 2021 at 11:24 Comment(7)
why do you need write into a file "a"? how does it write for file "a"?Scantling
"a" is not the filename, but the mode. It means open the file in append mode.Patriarchate
ah, yeah! That is true, I see the code now, thank youScantling
Does this work today?Alisonalissa
Note: myfile.write(...) does not append a newline, whereas print(..., file=myfile) does. So print is the correct equivalent to echo "..." >> $GITHUB_ENV.Demineralize
Don't forget to add '\n' (myfile.write("MY_VAR=MY_VALUE\n")) if intending to write multiple environment varables.Palstave
For me did not work as of todayPathos
K
4

I asked me how to set two or more environment variables. You have te seperate these variables with a linebreak. Here is an example:

import os

env_file = os.getenv('GITHUB_ENV')

with open(env_file, "a") as myfile:
    myfile.write("MY_VAR1=MY_VALUE1\n")
    myfile.write("MY_VAR2=MY_VALUE2")
Katleen answered 13/5, 2022 at 6:29 Comment(0)
D
0

Another, maybe more straightforward, way is to print a value in your Python code and "catch" it on the bash level, after which you can append it to GITHUB_OUTPUT variable -

script.py

def main():
    print("SOME VALUE")

if __name__ == "__main__":
    main()

workflow.yml

- id: set-variable
  run: |
    YOUR_VARIABLE=$(python script.py)
    echo "YOUR_VARIABLE=${YOUR_VARIABLE}" >> $GITHUB_OUTPUT
- name: Use your variable
  run: |
    echo "Your variable value is ${{ steps.set-variable.outputs.YOUR_VARIABLE }}"
Drumlin answered 2/6, 2023 at 15:23 Comment(0)
P
0

For some reason, accepted answer did not work for me. The following did work. Basically I am running an "echo ...." command from the python script.

var_name='DUMMY__ARG'
var_value="New_dummy_arg_value"
echo_statement="echo "+"\""+var_name+"="+var_value+"\""+" >> $GITHUB_ENV"
print("sending the following to os.system")
print(echo_statement)
os.system(echo_statement)

In the workflow, both of the following worked to access the variable in my case: ${{ env.DUMMY__ARG}} or $DUMMY__ARG

Note that "the step that creates or updates the environment variable does not have access to the new value, but all subsequent steps in a job will have access." from the documentation: GitHub Action Set $GITHUB_ENV Not Saving

Pathos answered 25/7 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.