Reputation: 793
I am keeping a shell script file named urltest.sh
in /var/lib/jenkins
and executing the file from jenkins
build.
When I execute the build, It fails.
The Environment Variables are -
HOME - /var/lib/jenkins ;
JENKINS_HOME - /var/lib/jenkins
The console output comes as:
Started by user anonymous
Building in workspace /var/lib/jenkins/workspace/AutoScript
[AutoScript] $ /bin/sh -xe /tmp/hudson2777728063740604479.sh
+ sh urltest.sh
sh: 0: Can't open urltest.sh
Build step 'Execute shell' marked build as failure
Finished: FAILURE
Where should I keep the shell script file so that it is executed?
Upvotes: 40
Views: 349278
Reputation: 290275
Based on the number of views this question has, it looks like a lot of people are visiting this to see how to set up a job that executes a shell script.
These are the steps to execute a shell script in Jenkins:
In the textarea you can either paste a script or indicate how to run an existing script. So you can either say:
#!/bin/bash
echo "hello, today is $(date)" > /tmp/jenkins_test
or just
/path/to/your/script.sh
Click Save.
Now the newly created job should appear in the main page of Jenkins, together with the other ones. Open it and select Build now to see if it works. Once it has finished pick that specific build from the build history and read the Console output to see if everything happened as desired.
You can get more details in the document Create a Jenkins shell script job in GitHub.
Upvotes: 63
Reputation: 22392
Previous answers are correct but here is one more way of doing this and some tips:
Option #1 Go to you Jenkins job and search for "add build step" and then just copy and paste your script there
Option #2 Go to Jenkins and do the same again "add build step" but this time put the fully qualified path for your script in there example : ./usr/somewhere/helloWorld.sh
things to watch for /tips:
Upvotes: 8
Reputation: 1138
If you see your error message it says
Building in workspace /var/lib/jenkins/workspace/AutoScript
and as per your comments you have put urltest.sh in
/var/lib/jenkins
Hence Jenkins is not able to find the file. In your build step do this thing, it will work
cd # which will point to /var/lib/jenkins
./urltest.sh # it will run your script
If it still fails try to chown the file as jenkin user may not have file permission, but I think if you do above step you will be able to run.
Upvotes: 24
Reputation: 1973
There's the Managed Script Plugin which provides an easy way of managing user scripts. It also adds a build step action which allows you to select which user script to execute.
Upvotes: 7