如何在Jenkins UI中执行本地Python脚本

13

我刚接触Jenkins,最近想要安排一个工作来执行本地的Python脚本。由于我还没有源代码控制,因此在创建Jenkins UI中的工作时选择了“无”。

我对如何在Jenkins UI中执行Python脚本进行了一些研究,并尝试使用Python插件将Python脚本作为构建步骤执行。但失败了。(实际上,我不想使用此插件,因为我的脚本需要输入参数,所以我认为我需要在BUILD字段中选择类似于“execute shell”的选项--我尝试过,但也失败了)有谁能帮我找出如何正确运行/调用本地的Python脚本吗?

附注:我也不清楚Jenkins工作空间以及它是如何工作的?如果有人能为我澄清这个问题那就太好了。

以下是我在构建失败后得到的控制台输出:

Started by user Yiming Chen
[EnvInject] - Loading node environment variables.
Building in workspace D:\Application\Jenkins\workspace\downloader
[downloader] $ sh -xe C:\windows\TEMP\hudson3430410121213277597.sh
The system cannot find the file specified
FATAL: command execution failed
java.io.IOException: Cannot run program "sh" (in directory     "D:\Application\Jenkins\workspace\downloader"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at hudson.Proc$LocalProc.<init>(Proc.java:245)
at hudson.Proc$LocalProc.<init>(Proc.java:214)
at hudson.Launcher$LocalLauncher.launch(Launcher.java:846)
at hudson.Launcher$ProcStarter.start(Launcher.java:384)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:108)
at hudson.tasks.CommandInterpreter.perform(CommandInterpreter.java:65)
at hudson.tasks.BuildStepMonitor$1.perform(BuildStepMonitor.java:20)
at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:779)
at hudson.model.Build$BuildExecution.build(Build.java:205)
at hudson.model.Build$BuildExecution.doRun(Build.java:162)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:534)
at hudson.model.Run.execute(Run.java:1728)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:43)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:404)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 16 more
Build step 'Execute shell' marked build as failure
Finished: FAILURE
6个回答

20

创建一个 Jenkins 作业,并从 Jenkins 作业中将您的脚本作为 shell 脚本运行。就像这样:

#!/bin/sh
python <absolute_path_of_python_script>.py

谢谢,但我已经尝试过了。这是我做的:我在BUILD中选择了Execute Shell,并在命令中输入了python D:/xxx/xxx/xxxx/script.py,但当我点击构建时仍然失败了。 - PIZZA PIZZA
1
是的,但它太长了。我会在问题中添加它。 - PIZZA PIZZA
我认为这与Jenkins工作区有关,我正在尝试将我的脚本移动到工作区并查看其效果。 - PIZZA PIZZA
[downloader] $ sh -xe C:\windows\TEMP\hudson3430410121213277597.sh 系统找不到指定的文件 致命错误:命令执行失败 检查.sh文件的路径 - Avinash
1
如果您将Windows命令指定为“执行shell”而不是“执行Windows批处理命令”,则会发生这种情况。 - Avinash
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/134348/discussion-between-austin-chen-and-avi。 - PIZZA PIZZA

13

不必在每台服务器上处理本地脚本文件,实际上可以将所有的Python脚本复制到“构建”部分下的“执行Shell”中。

它必须以相关的Python shebang开头。例如:

#!/usr/bin/env python
your script...

您还可以在作业中添加参数,并在Python脚本中使用环境变量。例如:

parameter1 = os.environ['parameter1']

3
另一种方法是创建pipeline并执行sh命令,该命令指向您的Python脚本。 您还可以通过Jenkins UI传递参数,就像dsaydon在他的答案中提到的那样。 sh命令可以如下所示(类似于在命令行中运行):
sh 'python.exe myscript.py'

示例流水线步骤:创建新的虚拟环境并在安装所有依赖后运行脚本

stage('Running python script'){
sh      '''
        echo "executing python script"
        "'''+python_exec_path+'''" -m venv "'''+venv+'''" && "'''+venv+'''\\Scripts\\python.exe" -m pip install --upgrade pip && "'''+venv+'''\\Scripts\\pip" install -r "'''+pathToScript+'''\\requirements.txt" && "'''+venv+'''\\Scripts\\python.exe" "'''+pathToScript+'''\\my_script.py" --path "'''+PathFromJenkinsUI+'''"
        '''
}

where

sh ''' 
   your command here
   ''' 

表示多行shell命令(如果你真的需要的话)

您还可以从管道(Groovy脚本)中将变量传递到sh命令,并因此将其作为参数传递给您的Python脚本。使用这种方式:'''+argument_value+'''(在变量名周围使用三个引号和加号)

例如:您的Python脚本接受可选参数path,您想要使用特定值执行它,该值应输入在Jenkins UI中。然后,您在Groovy脚本中的shell命令应如下所示:

// getting parameter from UI into `pathValue` variable of pipeline script
// and executing shell command with passed `pathValue` variable into it.

pathValue = getProperty('pathValue') 
sh '"\\pathTo\\python.exe" "my\\script.py" --path "'''+pathValue+'''"' 

如何在 @NonCPS 中实现该操作? - hadoop
这句话的意思是尽管我在计算机上已经安装了Python,但系统仍然无法找到它。 - Andrew

2
选择“BUILD”选项以执行Python脚本- 选择执行Windows批处理命令 - 输入以下命令。我传递了Python路径,因为Jenkins由于访问问题无法访问环境变量。
set PYTHONPATH=%PYTHONPATH%;C:\Users\ksaha029\AppData\Local\Programs\Python\Python3
python C:\Users\ksaha029\Documents\Python_scripts\first.py

0
在 Mac 上,我只需将 script.py 移动到 /Users/Shared/Jenkins/Home/workspace/your_project_name,并使用 chmod 777 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.py 命令即可解决问题。此外,我不需要使用 #!/bin/sh 或 #!/usr/bin/env python。在 Jenkins 构建中,我只需使用以下命令:python3 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.py。
值得一提的是,我花了一天时间尝试解决这个问题,并阅读了所有相关论坛的问题,但没有人真正能够帮助我 :/。

-1

最简单的实现方法是勾选将注入环境变量到构建过程框中。然后定义两个变量,一个用于Python,另一个用于脚本。
例如:PYTHONPATH = C:/python37/python.exe TEST1SCRIPT = C:/USERS/USERNAME/Documents/test1.py 执行Windows批处理命令。
%PYTHONPATH% %TEST1SCRIPT% 这样,您可以在一个或多个执行Windows批处理命令段内运行多个脚本。还有一些自定义的方法。您可以创建一个包装器,在Jenkins下运行脚本,这样,如果要将整个测试套件的结果发送电子邮件,则可以格式化脚本结果输出。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接