Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: 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?

like image 962
Bltucker Avatar asked Aug 14 '16 18:08

Bltucker


People also ask

How do I run a script in Jenkins pipeline?

Click New Item on your Jenkins home page, enter a name for your (pipeline) job, select Pipeline, and click OK. In the Script text area of the configuration screen, enter your pipeline syntax.

Can Jenkins run scripts?

Jenkins features a Groovy script console which allows one to run arbitrary Groovy scripts within the Jenkins controller runtime or in the runtime on agents.


1 Answers

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)

like image 69
Gergely Toth Avatar answered Nov 16 '22 00:11

Gergely Toth