Continuous Integration and Deployment with Jenkins
Continuous Integration and Deployment with 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
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.
⏭️ After that, type in the username and password you created during the setup process. This is what your dashboard should look like:
Creating jobs
Let us now create our first job.
Give it a name, choose free style project
and click OK
You are redirected to this page.
I chose the window shell because I have Jenkins installed on a Windows machine.
⏭️ Following that, we will build our job.
📝 Green denotes success.
Let's look at the results.
Result
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.
⏭️ Select the build periodically option
Fig 1.8
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.
Read more about cron expression here
⏭️ Let's check our build history to see if the cron scheduler actually works.
Remote triggers
We have other options of building our files. Let's untick
the build periodically and let's choose build triggers
.
⏭️ 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.
💡 Let us now run the URL above in our browser; we now have a new build trigger.
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
⏭️ Choose the parameterized option
⏭️ Add parameter, choose string parameter
⏭️ If you observe closely you will see build with parameter
⏭️ Click on the build
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.
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
GitHub web hook
📝 Go to configure and choose GitHub web-hook
📝 Go to your the repository on GitHub, locate and click web-hook under the settings section
.
📝 Click on add
Add the Jenkins URL to the payload
⚠️ 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
When we visit the URL, we get:
⏭️ Next, we paste the forwarded URL from ngrok to the github webhook
Click on Add webhook
, and refresh. On refresh we get a green ✅
⏭️ 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
On success, we get
Manage users
Let's create another user apart from the first user created initially
⏭️ Click manage Jenkins
⏭️ Under the security section, click manage users
⏭️ Click create users
⏭️ Enter the credentials
Result:
Switching Users
Now we can logout of our admin user and use the testuser
account to login.
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.
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.
I have set the authorization
to Role Based before now, I will change it to Anyone can do anything
Click apply, save and logout. Thereafter, you can login with the test account.
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
To use the Role base, Go to manage jenkins and Config Global Security. The role base option should now be available
Then go back to manage jenkins and click on Manage and Assign Roles
You get redirected to this page
Click on manage roles
Now you can check the access you want to enable for this particular role. Save and Apply
Now let's go and assign role by adding our testuser
.
Now we can tick the role we want to assign to the testuser account
Click Apply and Save. Now let's logout from the main account and login with our testuser`
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.
Click apply and save. Now logout from the admin and login with the testuser
account
Result:
Config system
This is where you can set certain parameters, like the Jenkins URL, GitHub servers etc
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.
Go back to configure
And click on the preferred post build action
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.