Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I execute Shell script in Jenkinsfile?

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.

like image 840
Kworks Avatar asked Jan 22 '14 07:01

Kworks


People also ask

What does sh do in Jenkinsfile?

On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline.


1 Answers

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.

like image 177
fedorqui 'SO stop harming' Avatar answered Sep 27 '22 18:09

fedorqui 'SO stop harming'