How to use Jenkins Environment variables in python script
30,362
That's what you need if I understand you correctly:
QUALIFIER="$(echo $BUILD_ID | sed "s/[-_]//g" | cut -c1-12)"
export QUALIFIER
python my_script.py
And in your Python script:
import os
qualifier = os.environ['QUALIFIER']
or without the shell part:
import os
import re
qualifier = re.sub(r'[-_]+', '', os.environ['BUILD_ID'])[0:12]
Author by
Asad S. Malik
Updated on November 20, 2020Comments
-
Asad S. Malik over 3 years
so I have a bash script in which I use the environment variables from Jenkins for example: QUALIFIER=
echo $BUILD_ID | sed "s/[-_]//g" | cut -c1-12
Essentially I'm taking the build id, along with job name to determine which script to call from my main script. I want to use python instead so I was wondering whether I can use these variables without the jenkins python api.
I hope the question makes sense. Thanks
-
Vahid Kharazi almost 11 yearscan you explain what you want?
-
Asad S. Malik almost 11 yearsI need to use the environment variables in Jenkins such as JOB_NAME to decide which script to call. I have a bash script which gets these variables but I wanted to do this in a python script. How would I get these variables in python?
-
Vahid Kharazi almost 11 years
-
-
Asad S. Malik almost 11 yearsBut would I need my bash script as well as the python one takes the 'QUALIFIER'? The $BUILD_ID makes the jenkins environment variable available to the bash script. Is there somehow I can make it available to the python script without having to use the bash one?
-
Tomasz Elendt almost 11 yearsYou can access
$BUILD_ID
exactly the same way (usingos.environ
) and then remove dashes and slice it in Python.