DEV Community

Cover image for CI/CD using Jenkins
Abayomi Ogunnusi
Abayomi Ogunnusi

Posted on

CI/CD using Jenkins

Hello there, I hope you're staying in shape. We'll take a look at a Continuous Development and Continuous Integration (CI/CD) tool today.

Jenkins is an open source automation server which can be used to automate all sorts of tasks related to building, testing, and delivering or deploying software. cite.


Get your laptops and let's automate some tasks

automation


Prerequisite

🎯 Jenkins click ⬇️
🎯 A bit of scripting knowledge
🎯 Java installed on your machine


Agenda

🎯 Jenkins setup
🎯 Creating a job
🎯 Scheduling jobs using Cron expression
🎯 Remote triggers
🎯 Webhook and Poll SCM
🎯 Switching Users


Jenkins setup

The default port for jenkins after following the installation guide is 8080. Visit http://localhost:8080 in any browser. I've included the installation guide in the resources section below.
Image description


⏭️ After that, type in the username and password you created during the setup process. This is what your dashboard should look like:

Image description


Creating jobs

Let us now create our first job.
Image description

Give it a name, choose free style project and click OK

Image description


You are redirected to this page.

Image description

I chose the window shell because I have Jenkins installed on a Windows machine.
Image description


⏭️ Following that, we will build our job.
Image description


📝 Green denotes success.
Image description


Let's look at the results.

Image description


Result

Image description


Job scheduling

Now that we've established how to make a new item, let's looking at automating this task using Cron.

Cron is the baked in task scheduler - run things at fixed times, repeat them and so on

Now let's schedule our build using Cron Expression . Locate the configure icon and click on it.
Image description


⏭️ Select the build periodically option

Image description

Fig 1.8

Image description
In the describe image Fig 1.8, here we trigger a build at an interval of 1 minute.

⚠️ Remember to leave a space between the 2 adjacent stars.

A bit about cron/scheduler: Each stars represent time i.e first star (seconds), second star (minutes) and so on.

Image description

Read more about cron expression here


⏭️ Let's check our build history to see if the cron scheduler actually works.

Image description


Remote triggers

We have other options of building our files. Let's untick the build periodically and let's choose build triggers.
Image description

⏭️ The firebuild is the token. And we enter our remote trigger in the form of url. Click apply and save.
The URL should look like:
http://localhost:8080/job/myfirstjob/build?token=firebuild

⏭️ I have cleared the build history for clarity's sake.
Image description

💡 Let us now run the URL above in our browser; we now have a new build trigger.
Image description

So, as long as you have the remote trigger link, you can build from anywhere, using any browser.


Parameterized Job:

A build parameter allows us to pass data into our Jenkins jobs. Using build parameters, we can pass any data we want: git branch name, secret credentials, hostnames and ports, and so on.

Click the configure icon
Image description

⏭️ Choose the parameterized option
Image description

⏭️ Add parameter, choose string parameter
Image description

Image description

⏭️ If you observe closely you will see build with parameter
Image description

⏭️ Click on the build
Image description


Webhook and Poll SCM

A Webhook is a mechanism to automatically trigger the build of a Jenkins project in response to a commit pushed to a Git repository.

while

Poll SCM periodically polls the SCM to check whether changes were made (i.e. new commits) and builds the project if new commits where pushed since the last build.

To use web-hook, we must first connect to our source code manager, git.
Image description

Image description
Depending on the repository, change the git branch to main or master.
⚠️ For private repositories, credentials will be required.


⏭️ We use the poll with cron schedule
Image description


GitHub web hook

📝 Go to configure and choose GitHub web-hook
Image description

📝 Go to your the repository on GitHub, locate and click web-hook under the settings section.
Image description

📝 Click on add
Image description

Add the Jenkins URL to the payload
Image description

⚠️ Using localhost with the above will not work, to make our localhost public ip we need ngrok to forward it.

Download it and install. Expose Jenkins port which is 8080 to get the URL
Image description

Image description

When we visit the URL, we get:

Image description

⏭️ Next, we paste the forwarded URL from ngrok to the github webhook
Image description
Click on Add webhook, and refresh. On refresh we get a green ✅
Image description

⏭️ Now, lets login into Jenkins using the forwarded URL generated from ngrok and connected to Jenkin web hook.

To build, edit your GitHub repo, commit changes and refresh the jenkins page
Image description

On success, we get
Image description


Manage users

Let's create another user apart from the first user created initially

⏭️ Click manage Jenkins
Image description

⏭️ Under the security section, click manage users
Image description

⏭️ Click create users
Image description

⏭️ Enter the credentials

Image description

Result:

Image description


Switching Users

Now we can logout of our admin user and use the testuser account to login.

Image description

On login, you can see that the access is denied because we need to give this user permission or elevated rights to perform certain roles in Jenkins.

Image description

Let us log out of the test user and login as the admin user to grant access rights via the Role Base Access control.

⏭️ Let us begin with the user being able to see all of the jobs we have. Go to Manage Jenkins and then Configure Global Security.

Image description

I have set the authorization to Role Based before now, I will change it to Anyone can do anything
Image description
Click apply, save and logout. Thereafter, you can login with the test account.

Image description

Now the testuser account can see all jobs created. This is not the best practice, because we should not give any user the permission to do certain things on Jenkins.

We use the Role-Based Authorization strategy plugin for this. Click manage plugin and search role based
Image description

To use the Role base, Go to manage jenkins and Config Global Security. The role base option should now be available
Image description
Then go back to manage jenkins and click on Manage and Assign Roles
Image description

You get redirected to this page
Image description

Click on manage roles
Image description

Now you can check the access you want to enable for this particular role. Save and Apply
Image description

Now let's go and assign role by adding our testuser.
Image description

Now we can tick the role we want to assign to the testuser account
Image description

Click Apply and Save.
Now let's logout from the main account and login with our testuser`
Image description

We see access denied because i did not give this user account view access based on the configuration we set earlier. Let me edit and give it view access. I will logout from the testuser into the admin and enable the view access and overall access.

Image description

Image description
Click apply and save. Now logout from the admin and login with the testuser account

Result: Image description


Config system

This is where you can set certain parameters, like the Jenkins URL, GitHub servers etc
Image description
Image description


Email notifications

Email notifications allows us to get notified when we build or have failed transaction during our CI/CD Pipeline.

To set up, go Configure System and locate the email section.
Image description

Go back to configure

Image description

And click on the preferred post build action
Image description


Conclusion

I hope this post was helpful in starting out with Jenkins for your CI/CD workflow. I hope to deploy a NodeJS Project in the next tutorial. Thanks for reading.

Resources

I highly recommend Thetips4you, Jenkins Tutorial for Beginners. I extracted part of this tutorial here with his full permission.

Learn about CI/CD here

Installation guide by guru99

Discussion (5)

Collapse
mikolajbuchwald profile image
Mikołaj Buchwald

Great post! Jenkins is cool. Once you configure it. Until then it is a nightmare :)

Collapse
drsimplegraffiti profile image
Abayomi Ogunnusi Author

You are correct ..... Thanks for your contribution

Collapse
drsimplegraffiti profile image
Abayomi Ogunnusi Author

😃😃😃...

Collapse
makar2 profile image
Makar

Thanks @drsimplegraffiti, this was very helpful.

Collapse
drsimplegraffiti profile image
Abayomi Ogunnusi Author

Glad it was helpful