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

2024-04-20 04:28:20 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我做了一些关于如何在Jenkins UI中执行python脚本的研究,并尝试使用python插件作为构建步骤来执行python脚本。但失败了。(实际上,我不想使用这个插件,因为我的脚本接受输入参数,所以我认为我需要在构建字段中选择类似“execute shell”的内容——我尝试了,但也失败了)有谁能帮助我找到如何正确运行/调用本地python脚本的方法吗?

注:我也不清楚詹金斯的工作空间和它是如何工作的?如果有人能为我澄清的话。

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

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

Tags: runbuild脚本langexecutemodelprocjava
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+'''"
        '''
}

其中

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

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

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

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

parameter1 = os.environ['parameter1']

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

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

相关问题 更多 >