Jenkinsfile 找不到命令 exec mv 命令

时间:2021-04-20 09:36:57

标签: find jenkins-pipeline exec

在 Jenkinsfile 中,我试图从 find 命令移动文件:

steps {
    sh "mkdir -p $WORKSPACE/rpmbuild/{SPECS,BUILD,SRPMS} $WORKSPACE/rpmbuild/RPMS/{noarch,x86_64} $WORKSPACE/rpmbuild/SOURCES/hello_world/"
    sh "mv $WORKSPACE/hello_world.spec $WORKSPACE/rpmbuild/SPECS/"
    script {
        FOLDER_NAME = sh(returnStdout: true, script: 'echo $WORKSPACE | xargs basename')
    }
    sh "find $WORKSPACE/ -maxdepth 1 ! -name rpmbuild ! -name ${FOLDER_NAME} -exec mv {} rpmbuild/SOURCES/hello_world/ \\;"
}

当我在 Jenkins 上运行构建时,我收到此错误消息:

[Pipeline] // script
[Pipeline] sh
+ find /home/jenkins/workspace/hello_world/ -maxdepth 1 '!' -name rpmbuild '!' -name hello_world
/home/jenkins/workspace/hello_world/html
/home/jenkins/workspace/hello_world/tests
/home/jenkins/workspace/hello_world/.git
/home/jenkins/workspace/hello_world/Jenkinsfile
/home/jenkins/workspace/hello_world/README.md
/home/jenkins/workspace/hello_world/CHANGELOG.txt
/home/jenkins/workspace/hello_world/.gitignore
/home/jenkins/workspace/hello_world/conf
+ -exec mv '{}' /home/jenkins/workspace/hello_world/rpmbuild/SOURCES/hello_world/ ';'
/home/jenkins/workspace/hello_world@tmp/durable-59cf8666/script.sh: line 2: -exec: command not found
[Pipeline] }

我很确定我没有以正确的方式做我想做的事情,但是如果有人可以帮助我解决我的问题或向我解释如何以更好的方式做到这一点,我将不胜感激.

1 个答案:

答案 0 :(得分:0)

我通过修剪和使用三重双引号成功地使我的管道正常工作:

script {
  // .trim() removes the EOL character at the end of the FOLDER_NAME variable.
  FOLDER_NAME = sh(returnStdout: true, script: 'echo $WORKSPACE | xargs basename').trim()
}
// May be overkill to use the triple double-quote but works for me, so I keep it as is...
sh """
  find $WORKSPACE/ -maxdepth 1 ! -name "$FOLDER_NAME" ! -name rpmbuild -exec mv {} $WORKSPACE/rpmbuild/SOURCES/hello_world/ \\;
"""