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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41931790/
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-20 01:54:08  来源:igfitidea点击:

How to execute local python scripts in Jenkins UI

pythonjenkinsjenkins-plugins

提问by PIZZA PIZZA

I am new to Jenkins, recently want to schedule a job to execute a local python script. I do not have a source control yet so I selected "None" in Source Code Management when creating the job in the Jenkins UI.

我是 Jenkins 的新手,最近想安排一个作业来执行本地 python 脚本。我还没有源代码控制,因此在 Jenkins UI 中创建作业时,我在源代码管理中选择了“无”。

I did some research about how to execute python scripts in Jenkins UI and I tried using Python Plugin to execute python scripts as build steps. But it failed. (But actually I don't want to use this Plugin since my script takes input arguments so I think I need to select something like "execute shell" in BUILD field -- I tried but also failed) Could anyone help me to find out how to properly run/call a local python script?

我做了一些关于如何在 Jenkins UI 中执行 python 脚本的研究,我尝试使用 Python Plugin 作为构建步骤来执行 python 脚本。但它失败了。(但实际上我不想使用这个插件,因为我的脚本需要输入参数,所以我想我需要在 BUILD 字段中选择“执行 shell”之类的东西——我试过但也失败了)谁能帮我找出如何正确运行/调用本地python脚本?

PS: I am also not clear about the Jenkins Workspace and how it works? Will appropriate if someone could clarify it for me.

PS:我也不清楚 Jenkins Workspace 以及它是如何工作的?如果有人可以为我澄清它会适当。

Here is the Console output I got after the fail build:

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

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

回答by Avinash

Create a Jenkins job and run your scripts as shell script from jenkins job. Like this

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

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

回答by dsaydon

instead of handle local script file on each server, you can actually copy all the python script into the "execute shell" under the Build section. it has to start with the relevant python shebang. For example:

而不是在每个服务器上处理本地脚本文件,您实际上可以将所有python脚本复制到Build部分下的“执行shell”中。它必须从相关的python shebang开始。例如:

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

you can also add parameters in the job and use environment variables in your python script. for example

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

parameter1 = os.environ['parameter1']

回答by Sysanin

Another way is creating pipelineand execute shcommand, which points to your python script. You also can pass parameters via Jenkins UI as dsaydonmentioned in his answer.

另一种方法是创建pipeline和执行sh命令,该命令指向您的 Python 脚本。您也可以通过 Jenkins UI 传递参数,如他的回答中提到的dsaydon

shcommand can be as follow (is like you run in command line):

sh命令可以如下(就像你在命令行中运行一样):

sh 'python.exe myscript.py'

Example pipeline step with creating new virtual environment and run script after installing of all requirements

示例管道步骤,在安装所有要求后创建新的虚拟环境并运行脚本

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

means multiline shell command (if you really need it)

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

You also can pass variables from your pipeline (groovy-script) into shcommand and, consequently, to your python script as arguments. Use this way '''+argument_value+'''(with three quotes and plus around variable name)

您还可以将管道(groovy-script)中的变量传递给sh命令,从而作为参数传递给您的 Python 脚本。使用这种方式'''+argument_value+'''(在变量名周围加上三个引号)

Example: your python script accepts optional argument pathand you want to execute it with specific value which you would like to input in your Jenkins UI. Then your shell-command in groovy script should be as follow:

示例:您的 python 脚本接受可选参数,path并且您希望使用您想在 Jenkins UI 中输入的特定值来执行它。那么你在 groovy 脚本中的 shell-command 应该如下:

// 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+'''"' 

回答by Kaustub

To execute a Python script under BUILD option- select execute windows batch command - type these cammands.

要在BUILD option- 选择执行 Windows 批处理命令下执行 Python 脚本- 键入这些命令。

I am passing the pythonpath because jenkins was not able to access the environmental variables because of access issues.

我正在传递 pythonpath 因为 jenkins 由于访问问题而无法访问环境变量。

set PYTHONPATH=%PYTHONPATH%;C:\Users\ksaha029\AppData\Local\Programs\Python\Python3
python C:\Users\ksaha029\Documents\Python_scripts\first.py

回答by S D

On Mac I just moved the script.py to /Users/Shared/Jenkins/Home/workspace/your_project_nameand with chmod 777 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.pyI could fix the problem. Also, I did not need to use : #!/bin/shor #!/usr/bin/env python. Just inside jenkins build I used:

在 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 build 里面:

python3 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.py

python3 /Users/Shared/Jenkins/Home/workspace/your_project_name/script.py

I should mention that, for one day I was trying tu solve this problem, and I have read all the froum related questions. no one really could help :/ .

我应该提到的是,有一天我试图解决这个问题,我已经阅读了所有与论坛相关的问题。没有人真的能帮上忙:/。

回答by user13552212

Simplest implementation is checking the inject environment variables into the build process box. Then define two variables one for the python another for the script.
For example PYTHONPATH = C:/python37/python.exe TEST1SCRIPT = C:/USERS/USERNAME/Documents/test1.pyExecuting the windows batch command.
%PYTHONPATH% %TEST1SCRIPT%This way you can run a number of scripts inside one or multiple execute windows batch command segments. There are also way to customize. You can create a wrapper that would run the scripts under Jenkins, this way, script result output could be formatted, if emailing results of the whole test suite.

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