How to execute shell script within Jenkins Pipeline on MacOS

Mayank Jain
4 min readMar 14, 2024

--

Hello Folks, In this blog we will learn how to execute the shell script within the Jenkins pipeline.

Benefits of using shell script:

There are several benefits to using shell scripts within Jenkins pipelines:

Code reusability:

Complex tasks can be encapsulated in a single script, which can then be called from multiple stages within the pipeline or even across different pipelines. This reduces duplication of effort and makes your pipeline code more concise and maintainable.

Improved organization:

Shell scripts can group related commands together, making the pipeline stages easier to understand and follow. This is especially helpful for complex pipelines with many steps.

Flexibility:

Shell scripts allow you to leverage the power and flexibility of the shell environment. You can use conditional logic, loops, and other scripting features to create dynamic pipelines that can adapt to different situations.

Easier maintenance:

If you need to modify the logic of a particular task, you can simply update the shell script instead of having to change the pipeline code itself. This can be a real time-saver, especially for complex pipelines.

Prerequisites:

Jenkins should be running.

Shell Script creation:

Create a file in your workspace with extension .sh. In my case I created a file name firstscript.sh in my learning folder.

Now add the below code in your file firstscript.sh

#!/bin/bash
echo "Hello Learning"

Jenkins Job creation:

Now login to your jenkins and create your first job by clicking Create a job

Once you clicked on create job a similar window will open as shown in below screenshot

In the required field you need to give the name to your job in my case I have gave firstjob, after that click on Pipeline and then click on ok. It will open the similar window as shown in below screenshot. This the the part where we configure our job.

Now scroll down on this page you will find the pipeline section where we are going to create our first pipeline script. Copy paste the code in the script section

pipeline{
agent any

stages{
stage('firstStage'){
steps{
sh '/Users/mayankjain/workspace/learning/firstscript.sh'
}
}
}
}

Note: Change the path of the script file as per your workspace.

Now click on Apply and Save. Our first job is created now. You will able to see similar screen as shown below.

Running the Job:

Now let’s run our first job by clicking on Build Now and we will see the logs of job by clicking on #1 which is our build number, then click on Console Output.

We will able to see the error is Permission denied.

This is very important point to note that whenever we are running a shell script via pipeline make your script have sufficient permission to execute.

Now let’s give the permission to the script by running the below command from terminal.

~ chmod 755 firstscript.sh

You can notice that after giving the permission the color of file got changes. You can check the permission of file by running the below command

~ ls -altr

Now we have given the sufficient permission. Let’s go back and re run our jenkins job. This time job will run successfully and execute the script. After running check the Console Output, in the output you will able to see that our script print the Hello Learning and the status of job is SUCCESS.

Congratulation you just learned how to execute the shell script via pipeline.

Learn how to install and configure Jenkins on MacOS here.

Learn how to set up the Puppet Master and Agent in a virtual box environment here.

Learn RHEL8 custom AMI creation using RHEL Image Builder here.

Learn how to remove the old Linux kernel from RHEL8/CentOS8 here.

Learn how to log commands of all users runs in Linux here.

--

--

Mayank Jain

Skilled DevOps Engineer with 7.5+ years of hands-on experience supporting, automating, & optimizing mission critical deployments in cloud.