如何在python脚本中使用Jenkins环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17071584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:21:08  来源:igfitidea点击:

How to use Jenkins Environment variables in python script

pythonbashjenkinsenvironment-variables

提问by Asad S. Malik

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

所以我有一个 bash 脚本,我在其中使用了 Jenkins 的环境变量,例如: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.

本质上,我使用构建 ID 和作业名称来确定从我的主脚本调用哪个脚本。我想改用 python,所以我想知道是否可以在没有 jenkins python api 的情况下使用这些变量。

I hope the question makes sense. Thanks

我希望这个问题是有道理的。谢谢

采纳答案by Tomasz Elendt

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:

在你的 Python 脚本中:

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]