詹金斯和Python

时间:2015-07-16 14:20:08

标签: python jenkins

我正在尝试运行我在Jenkins上配置运行的Python作业,它运行良好,直到我想传递变量,我必须做什么才能通过命令shell通过Jenkins传递变量?我需要为作业添加什么以及我必须在脚本中做出哪些更改?

感谢,

埃雷兹

1 个答案:

答案 0 :(得分:2)

我每天都在使用Jenkins,并从Jenkins工作中运行Python脚本。以下是我如何使用参数调用Python脚本的一些示例:

python ./local_lib/bin/regression.py -u daily_regression
python ./local_lib/bin/run.py -t $Test_Name -b -c -no_compile -no_wlf
python ./local_lib/bin/run.py -t $Test_Name -b -c -no_compile -no_wlf -args="$sim_args"
python ./local_lib/bin/results.py  -r daily_regression -html  -o $WORK/results/daily_regression_results.html

这是你想要的吗?

您应该使用"此版本已参数化" Jenkins作业配置面板中的选项。我添加了一个名为" Test_Name"的字符串参数。然后我在"执行Shell"像这样的$ Test_Name

的文本区域

以下是如何在Python脚本中使用它。我们来看看Python脚本:

python ./local_lib/bin/run.py -t $Test_Name -b -c -no_compile -no_wlf

脚本run.py可能是:

import sys

print "Number of arguments", len(sys.argv)
for arg in sys.argv :
    print arg

# or

print 'second method'

for i in range(len(sys.argv)) :
    print sys.argv[i]

# take the test name given by the Jenkins parameter $Test_Name
testName = sys.argv[2] # arg 0 is script name, arg 1 == '-t', arg 2 == '<the provided test name in Jenkins Run Job interface as a string>', ...
print testName

输出是:

E:\>python run.py -t $Test_Name -b -c -no_compile -no_wlf
Number of arguments 7
run.py
-t
$Test_Name
-b
-c
-no_compile
-no_wlf
second method
jenkins.py
-t
$Test_Name
-b
-c
-no_compile
-no_wlf
$Test_Name

我现在无法在PC上访问Jenkins,因此在这种情况下,$ Test_Name按原样打印,但在Jenkins上,当启动Jenkins作业时,它将被文本框中提供的名称用户所取代