[英]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.