简体   繁体   English

在Jenkins Groovy Pipeline中使用`Process`执行命令

[英]Execute Command using `Process` in Jenkins Groovy Pipeline

I am executing a cURL command using sh command with no issue. 我正在使用sh命令执行cURL命令,没有任何问题。

pulic uploadArtifct (String user, String password, String file, String 
  location) {
  def cred = "${user}:${password}"
  def cmd = "curl -v -u cred --upload-file ${file} ${location}"

  sh cmd

  }

However, when I try to execute the same cmd , using the Process object. 但是,当我尝试使用Process对象执行相同的cmd I get an error: 我收到一个错误:

public uploadArtifct (String user, String password, String file, String 
  location) {
  def cred = "${user}:${password}"
  def cmd = "curl -v -u cred --upload-file ${file} ${location}"
  try {
   def sout = new StringBuffer(), serr = new StringBuffer()
   def proc = cmd.execute()
   proc.consumeProcessOutput(sout, serr)
   proc.waitForOrKill(1000)
   println sout
  } catch (Exception e) {
    throw new RuntimeExceptipon("Cannot execute curl, exception: [${e.getClass().getName()} - '${e.getMessage()}']")
   }
  }

The error that I see is: 我看到的错误是:

java.lang.RuntimeException: Cannot execute curl, exception: [java.lang.RuntimeException - 'Error running; stdout='', stderr='curl: Can't open 'Folder/artifact/file.zip'!
curl: try 'curl --help' or 'curl --manual' for more information
'']

What is it about Process.execute() that does not work. 不起作用的Process.execute()有什么用。 Am I missing something? 我想念什么吗?

I ran into a similar issue half a year ago. 半年前,我遇到了类似的问题。 As i found out , the curl request that you run using the sh command is executed on the agent where the build is run 我发现,使用sh命令运行的curl请求是在运行构建的代理上执行的

sh cmd  //this runs on the agent and hence finds the ${file}

However the second piece of code 但是第二段代码

def proc = cmd.execute() . //This is run on the master and hence it cannot find the ${file}

When you use groovy classes, it is by design to be executed on the master node. 使用groovy类时,它是设计使在主节点上执行的。 This is because the groovy engine that Jenkins uses is on the master 这是因为Jenkins使用的groovy引擎在主机上

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

相关问题 Groovy 脚本在不使用管道的情况下在 jenkins 上执行多个不同的作业? - Groovy script to execute multiple different job on jenkins without using pipeline? 如何在 Jenkins 声明性管道中执行 Groovy 语句? - How to execute Groovy statements in a Jenkins declarative pipeline? 如何使用 groovy 在 jenkins windows slave 上执行 CMD 命令? - How to execute CMD command on jenkins windows slave using groovy? 管道 Jenkins:在 groovy 中使用 sh 运行 cp source destination 命令,源和目标在 groovy 中声明 - Pipeline Jenkins: run cp source destination command using sh in groovy with source and destination declared in groovy 从Jenkins访问带有密码凭证参数的用户名使用Groovy命令执行Groovy构建步骤? - Accessing a Username with Password Credential Parameter from a Jenkins Execute Groovy build step using Groovy command? 如何使用 groovy 在 Jenkins 管道中执行 SQL 查询(sql server 数据库)? - How to execute SQL query (sql server database) in Jenkins Pipeline using groovy? 在 Jenkins 中使用管道执行 pytest - execute pytest using pipeline in Jenkins 使用Jenkins Groovy管道脚本解析数据 - Parse Data Using Jenkins Groovy Pipeline Script 在Groovy和Jenkins管道构建中使用变量 - Using variables in Groovy and Jenkins pipeline build jenkins管道在循环中执行ssh命令 - jenkins pipeline execute ssh command in loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM