Search code examples
findjenkins-pipelineexec

Jenkinsfile find command exec mv command not found


In a Jenkinsfile, I'm trying to move file from the find command:

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/ \\;"
}

When I run a build on Jenkins, I get this error message:

[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] }

I'm pretty sure I'm not doing what I would like to do the right way, but If someone can either help me to solve my issue or explain to me how to do this a better way, I would appreciate it.


Solution

  • I succeed to make my pipeline to work without error by trimming and using the triple double-quote:

    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/ \\;
    """