Execute a script from jenkins pipeline

允我心安 提交于 2020-01-13 08:58:09

问题


I have a jenkins pipeline that builds a java artifact, copies it to a directory and then attempts to execute a external script.

I am using this syntax within the pipeline script to execute the external script

dir('/opt/script-directory') {
    sh './run.sh'
}

The script is just a simple docker build script, but the build will fail with this exception:

java.io.IOException: Failed to mkdirs: /opt/script-directory@tmp/durable-ae56483c

The error is confusing because the script does not create any directories. It is just building a docker image and placing the freshly built java artifact in that image.

If I create a different job in jenkins that executes the external script as its only build step and then call that job from my pipeline script using this syntax:

build 'docker test build'

everything works fine, the script executes within the other job and the pipeline continues as expected.

Is this the only way to execute a script that is external to the workspace?

What am I doing wrong with my attempt at executing the script from within the pipeline script?


回答1:


The issue is that the jenkins user (or whatever the user is that runs the Jenkins slave process) does not have write permission on /opt and the sh step wants to create the script-directory@tmp/durable-ae56483c sub-directory there.

Either remove the dir block and use the absolute path to the script:

sh '/opt/script-directory/run.sh'

or give write permission to jenkins user to folder /opt (not preferred for security reasons)




回答2:


Looks like a bug in Jenkins, durable directories are meant to store recovery information e.g. before executing an external script using sh. For now all you can do is make sure that /opt/script-directory has +r +w and +x set for jenkins user.

Another workaround would be not to change the current directory, just execute sh with it:

sh '/opt/script-directory/run.sh'


来源:https://stackoverflow.com/questions/38945443/execute-a-script-from-jenkins-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!