Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run bash command on jenkins pipeline

Inside a groovy script (for a jenkins pipeline): How can I run a bash command instead of a sh command?

I have tried the following:

Call "#!/bin/bash" inside the sh call:

stage('Setting the variables values') {     steps {          sh '''             #!/bin/bash             echo "hello world"          '''     } } 

Replace the sh call with a bash call:

stage('Setting the variables values') {     steps {          bash '''             #!/bin/bash             echo "hello world"          '''     } } 

Additional Info:

My command is more complex than a echo hello world.

like image 797
Yago Azedias Avatar asked Jun 02 '17 13:06

Yago Azedias


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 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.


1 Answers

The Groovy script you provided is formatting the first line as a blank line in the resultant script. The shebang, telling the script to run with /bin/bash instead of /bin/sh, needs to be on the first line of the file or it will be ignored.

So instead, you should format your Groovy like this:

stage('Setting the variables values') {     steps {          sh '''#!/bin/bash                  echo "hello world"           '''     } } 

And it will execute with /bin/bash.

like image 180
Jake Avatar answered Sep 23 '22 17:09

Jake