简体   繁体   English

通过 Jenkins 执行 Groovy 命令:“查找:缺少‘-exec’的参数”

[英]Executing Groovy command through Jenkins: "find: missing argument to `-exec'"

Im trying to copy the resulting war file from one build directory to another using Jenkins pipeline script(groovy).我正在尝试使用 Jenkins 管道脚本(groovy)将生成的 war 文件从一个构建目录复制到另一个目录。 I have tested the find/exec/cp command on the system itself as the jenkins user, from the same workspace as the script runs and it works fine (direct copy paste from console out).我已经作为 jenkins 用户在系统本身上测试了 find/exec/cp 命令,从与脚本运行相同的工作区,它工作正常(从控制台直接复制粘贴)。

String buildNumber = env.BUILD_NUMBER

def sout = new StringBuffer()
def serr = new StringBuffer()

//Create package directory in jenkins job folder
def packageDir = "${env.JENKINS_HOME}/jobs/Package_Deploy_Pipeline/builds/${buildNumber}/package/"
def command = "mkdir ${packageDir}"
def proc = command.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(3000)
println "out> $sout err> $serr"

command = "find ${env.JENKINS_HOME}/jobs/myJob/builds/lastSuccessfulBuild/archive/build/libs/ -name *.war -exec cp {} ${packageDir} \\;"
println command
proc = command.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(3000)
println "out> $sout err> $serr"

The error seen in Console Output is:在控制台输出中看到的错误是:

[Pipeline] echo
out>  err> 
[Pipeline] echo
find /var/lib/jenkins/jobs/myJob/builds/lastSuccessfulBuild/archive/build/libs/ -name *.war -exec cp {} /var/lib/jenkins/jobs/Package_Deploy_Pipeline/builds/23/package/ \;
[Pipeline] echo
out>  err> find: missing argument to `-exec'

Edit: I have also tried "*.war" , '*.war' , \\;编辑:我也试过"*.war" , '*.war' , \\; and ';'';'

带有声明式管道的Jenkins 只对我有用:

sh 'find . -ipath "*src/header.h" -exec rm {} ";"'

Groovy常规

The problem for the error you are experiencing might be related with \\;您遇到的错误的问题可能与\\; and the fact that you are escaping as for bash/sh command, when in fact you are not using bash/sh , but running find executable.以及您正在逃避bash/sh命令的事实,而实际上您没有使用bash/sh ,而是运行find可执行文件。

The same approach needs to be applied to -name parameter, where quoting should be dropped from search pattern.需要将相同的方法应用于-name参数,其中应从搜索模式中删除引用。

Example:例子:

command = "find src -name *.txt -exec cp {} dst/ ;"
println "[cmd] ${command}"
proc = command.execute()

def sout = new StringBuffer()
def serr = new StringBuffer()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "[cmd.out] ${sout}"
println "[cmd.err] ${serr}"

Jenkins詹金斯

If you are running this code from Jenkins and Pipeline job, probably most convenient way would be to use built-in sh step.如果您从 Jenkins 和 Pipeline 作业运行此代码,可能最方便的方法是使用内置的sh步骤。

sh: Shell Script sh:外壳脚本

This step allows a Jenkins server or slave running on Linux or a Unix-like machine to execute a shell script. Params: script: String - the script to execute sh "find src -name '*.txt' -exec cp {} dst/ \\;"

More information on the Pipeline DSL can be found in the Pipeline tutorial .更多关于流水线 DSL 的信息可以在流水线教程中找到。

Pure Groovy纯 Groovy

And after all why not replace find with just Groovy code:毕竟为什么不只用 Groovy 代码替换find

("src" as File).eachFileRecurse{
    if (it.name ==~ ".*\\.txt") {
        ...
    }
}

Isn't that more elegant?那不是更优雅吗?

I didn't resolve why the command was failing but i'm going to assume its groovys conversion to sh breaking when chaining commands.我没有解决命令失败的原因,但我将在链接命令时假设它的 groovys 转换为 sh 中断。 My solution was to break up the find and copy command.我的解决方案是分解查找和复制命令。

location = "find ${env.JENKINS_HOME}/jobs/myJob/builds/lastSuccessfulBuild/archive/build/libs/ -name *.war".execute().text
command = "cp $location ${packageDir}myJob.war"
proc = command.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(3000)
println "out> $sout err> $serr"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM