【问题标题】:cURL command executes in groovy script, but not when imported into a Jenkins PipelinecURL 命令在 groovy 脚本中执行,但在导入 Jenkins 流水线时不执行
【发布时间】:2018-07-27 15:04:45
【问题描述】:

我正在尝试将执行 cURL 命令的 groovy 脚本导入到我的 Jenkins 管道中。在管道外部(例如从 shell)执行 groovy 脚本时,cURL 命令可以正确执行,但是在管道内部时,我的管道执行时没有错误,但 cURL 命令似乎没有执行。

test.groovy:

  class GroovyClass{

   def executeCurl() {

//define curl command and execute
    def proc = "curl fakecommand".execute()

// cURL uses error output stream for progress output.
    Thread.start { System.err << proc.err } 
// Wait until cURL process finished
    proc.waitFor()
 }


}

return new GroovyClass();

Jenkins 文件:

if (run) {
   def groovyClass= load 'test.groovy'
   groovyClass.executeCurl()
 }  

【问题讨论】:

    标签: curl groovy jenkins-pipeline jenkins-groovy


    【解决方案1】:

    在 Jenkinsfile 中,您只能使用实现可序列化接口的类的对象。

    如果不是这种情况,有时您会收到一条错误消息,有时它是偶然发生的,有时似乎什么也没发生。

    如果您想使用不可序列化类型的对象,您需要将其封装在一个带有@NonCPS 注释的方法中。除此之外,在创建进程时,这意味着您需要禁用沙盒模式。

    但这不是你想要的。因为这会在不应该执行任何此类任务的享元执行器中的主节点上执行 curl。

    要在节点上的管道中执行某些操作,您必须使用 batsh 步骤。

    一定要检查Jenkins Pipeline tutorial 和步骤参考和sn-p 生成器。文件访问的工作方式也不同。

    您当然也可以使用batsh 调用groovy 二进制文件以从管道内运行脚本

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      • 2014-11-17
      • 1970-01-01
      • 2021-12-29
      • 2016-10-18
      相关资源
      最近更新 更多