简体   繁体   中英

How to use Jenkins Environment variables in python script

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 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

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]

import os

os.environ.get("variable_name")

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