Bltucker
Bltucker

Reputation: 467

Execute a script from jenkins pipeline

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?

Upvotes: 10

Views: 38989

Answers (3)

Promise Preston
Promise Preston

Reputation: 29068

I had a similar concern when trying to execute a script in a Jenkins pipeline using a Jenkinsfile.

I was trying to run a script restart_rb.sh with sudo.

To run it I specified the present working directory ($PWD):

sh 'sudo sh $PWD/restart_rb.sh'

Upvotes: 0

Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27496

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'

Upvotes: 0

Gergely Toth
Gergely Toth

Reputation: 6976

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)

Upvotes: 11

Related Questions