How can I execute Shell script in Jenkinsfile?

273,881

Solution 1

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.

Solution 2

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 main page of Jenkins select New Item.
  • Enter an item name like "my shell script job" and chose Freestyle project. Press OK.
  • On the configuration page, in the Build block click in the Add build step dropdown and select Execute shell.
  • 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.

Solution 3

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

enter image description here enter image description here

things to watch for /tips:

  • Environment variables, if your job is running at the same time then you need to worry about concurrency issues. One job may be setting the value of environment variables and the next may use the value or take some action based on that incorrectly.
  • Make sure all paths are fully qualified
  • Think about logging /var/log or somewhere so you would also have something to go to on the server (optional)
  • thing about space issue and permissions, running out of space and permission issues are very common in linux environment
  • Alerting and make sure your script/job fails the jenkin jobs when your script fails

Solution 4

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.

Share:
273,881
Kworks
Author by

Kworks

Technology Enthusiast.Loves Productivity

Updated on December 05, 2020

Comments

  • Kworks
    Kworks over 3 years

    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
    

    I am confused where I should keep the shell script file so that it is executed.