简体   繁体   中英

Set an environment variable from a python script

I am trying to automate build, test, and deployment via CI/CD. I have a python script designed to query my git remote repository, select the most recent tag in semantic versioning format (xxx) and increment it depending on the type of change.

I would like to set my environment variable (GIT_NEW_VERSION) so that this can be used within my Makefile and the generated binary will have the version available. The problem with this is that the python script is run in a sub-process that doesn't have access to the parent process variables. So I can modify the variable only for the current process and any processes created after but not the process that called the python script.

I could call make from the python script but that is not ideal for error management and logging with my CI tool.

bash:

LD_LIBRARY_PATH=my_path
sqsub -np $1 /path/to/executable

Similar, in Python:

import os
import subprocess
import sys

os.environ['LD_LIBRARY_PATH'] = "my_path" # visible in this process + all children
subprocess.check_call(['sqsub', '-np', sys.argv[1], '/path/to/executable'],
                      env=dict(os.environ, SQSUB_VAR="visible in this subprocess"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM