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
With the environ dictionary variable value of the environment variable can be set by passing the key in the dictionary and assigning the value to it. With setdefault a default value can be assigned to the environment variable. Bypassing the key and the default value in the setdefault method.
An easy way to obtain the Jenkins environment variables list from your local installation is to append env-vars. html to the server's URL. For a locally hosted Jenkins server, the URL would be: http://localhost:8080/env-vars.html.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With