【问题标题】:How to execute local python scripts in Jenkins UI如何在 Jenkins UI 中执行本地 python 脚本
【发布时间】:2017-01-30 08:46:22
【问题描述】:

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

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

PS:我也不清楚 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

【问题讨论】:

    标签: python jenkins jenkins-plugins


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢,但我试过了。这是我所做的:我在 BUILD 中选择了 Execute Shell 并在命令中有这个:python D:/xxx/xxx/xxxx/script.py 但是当我单击 build 时仍然失败
    • 是的,但它太长了。我会在问题中添加它。
    • 我认为这与 Jenkins 工作区有关,我正在尝试将我的脚本移动到工作区,看看它是如何进行的。
    • [downloader] $ sh -xe C:\windows\TEMP\hudson3430410121213277597.sh 系统找不到指定的文件 FATAL: command execution failed check path of .sh
    • 如果您将 Windows 命令指定为“执行 shell”而不是“执行 Windows 批处理命令”,则会发生这种情况。
    【解决方案2】:

    您实际上可以将所有 python 脚本复制到 Build 部分下的“执行 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+'''"
              '''
      }
      

      在哪里

      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中做到这一点?
      • 这是说尽管它在我的机器上却找不到 python
      【解决方案4】:

      要在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
      

      【讨论】:

        【解决方案5】:

        在 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。就在我使用的詹金斯构建中:

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

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 2021-03-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-03-23
            • 2021-09-05
            • 2019-04-18
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多