繁体   English   中英

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

[英]How to execute local python scripts in Jenkins UI

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

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

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

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

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

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

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

如果你在 ubuntu 上。 由于指定了命令 python3 而不是 python。

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

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

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

parameter1 = os.environ['parameter1']

另一种方法是创建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+'''"
        '''
}

在哪里

sh ''' 
   your command here
   ''' 

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

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

示例:您的 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+'''"' 

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

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

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

在 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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM