Home
Blog
Certifications
Openings
Refer & Earn
Help
October 07, 2021
Jenkins: An Introduction To The CI/CD Tool
An introduction to Jenkins and how to implement it on your projects
Share
Share
What is CI/CD?
Continuous integration (CI) is a software development practice in which developers merge the changes they have made to the main branch multiple times per day. When this happens, each merge triggers an automated code build and test sequence and if the build succeeds, then the code can be successfully merged and CD steps can be followed to proceed. Meanwhile, continuous delivery (CD) picks up where continuous integration ends and this process automates the delivery of applications to selected infrastructure environments.
Here are some popular CI/CD tools:
In this article we will be focusing on a specific tool called Jenkins.
Jenkins
As an extensible automation server, Jenkins can be used as a simple CI server or be transformed into the continuous delivery hub for any project. It is a continuous integration server, written in Java, for orchestrating a chain of actions to achieve the CI process in an automated fashion. Jenkins supports the complete development life cycle of the software from building and testing to documenting the software and deploying amongst other stages of the software development life cycle. Let's get started on this interesting tool. π€
First, let's get clear idea of what will be covered in this article:
Setting up Jenkins
"Note: An important prerequisite for installing Jenkins is the you have Java installed on your system."
Copy
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
sudo yum upgrade
sudo yum install jenkins java-11-openjdk-devel
sudo systemctl daemon-reload
Copy
sudo systemctl start jenkins
Copy
sudo systemctl status jenkins
"Not on Centos system, no worries! Get help here."
Copy
cat /var/lib/jenkins/secrets/initialAdminPassword
"Note: It is recommended that localhost:8080 is mapped to a domain name/IP or else the GitLab integration might not work properly when using localhost. You can also optionally use a tunnelling software here."
Exploring the Jenkins UI
Here is what the dashboard will look like:
New Item - New Item is any job you want to build. Any fundamental task that one needs to develop or perform in Jenkins has to get started with the creation of a new item.
- Manage Jenkins - It is place where we can manage the entire Jenkins infrastructure. This is where any required configurations can be made.
List of jobs - Here all the jobs are enlisted along with their status (π΅ -> Latest build was a success and π΄ -> Latest Build was a failure). One can get the overview of the jobs and how they have been performing lately from this segment.
- Build Executor Status - All the currently being executed jobs will be seen here in an active state. The others in queue will be visible above in the "Build Queue".
A look into some of the options on the "Manage Jenkins" page:
Configure System - Using this page, one can create general settings on Jenkins like configuring options for installed plugins, setting up global environment variables etc.
- Global Tool Configuration - All the global tools that need to be used by the many jobs present in the Jenkins service can be configured in this section. For example: The installations, versions and other generic settings.
Manage Plugins - Plugins that can increase the functionality of Jenkins can be added, removed, enabled or disabled here.
- Manage Credentials - Any credentials that Jenkins might require for its integration with other tools can be set up here.
GitLab integration to check builds
Step 1: Download the necessary plugins
Step 2: Create Personal Access Tokens on GitLab Account
Step 3: Add the generated token to Jenkins
Here, we will add the generated token so that they can be used anywhere throughout Jenkins. Go to Manage Jenkins -> Manage Credentials -> (global) -> Add Credentials to achieve this. Refer to the image given below:
Fill the required fields:
Scope - This is as per your requirement and you can leave this as it is.
ID - I would suggest to add a meaningful ID/name to it.
Step 4: Create the integration
With this step we establish connection between GitLab and Jenkins. Go to Manage Jenkins -> Configure System to get started. Refer to the photo given below:
Fill in the required fields along with the token created previously by selecting it from the dropdown menu. Ensure to test the connection and if everything went right, you will derive at a successful output.
Step 5: Add ssh keys to GitLab and Jenkins
Copy
ssh-keygen -t rsa -m PEM -f blog-new.key
Copy
cat .ssh/blog-new.key.pub
Copy
cat .ssh/blog-new.key
Step 6: Create the job -
Now this is the most important part for the entire flow to work. Go to New Item -> Freestyle project and you will arrive at a landing page which looks like this:
i) Here select the connection created in the previous step from the dropdown.
i) Here select Git and add enter the repository's URL (git@xxxxx/xxxx.git) with the correct credential from dropdown.
ii) Leave "Branches to Build" as blank as we want all the branches to undergo the build/test process. This field can be edited if any particular branches are required to be built/tested.
Here all the conditions are specified that can trigger a build like as to what action on which branch can trigger the build for this job or any such condition One can edit the build as per the use case and requirement as shown below:
i) Build when a change is pushed to GitLab - This is selected so that events occurring on GitLab can trigger the build. (Copy the GitLab webhook URL and keep it)
ii) Push Events, Opened Merge Request Events - This is selected so that the events on specified branches can act as trigger.
iii) Filter branch by name - Selecting all branches on which of the above events will be set.
iv) Secret token - Generate this to be used along with the webhook URL.
Here one can create any particular build environment that might be required for the build to successfully execute. For e.g., one can set the required Node environment here if is build set Node commands and one can also set a path to the configuration files here.
This is place where the build takes place. In my case, I used Execute Shell and ran some build commands and test cases on the code that triggers the build.
One can set any action that should take place after the completion of a build. For e.g., the easiest being adding the build status(success/failure) on GitLab merge requests or integrating Jenkins with Slack to send messages to a particular channel.
Step 6: Add webhook URL to GitLab -
Update Production
"Note: The ~/.ssh/authorized_keys on production server should have the correct permissions."
"SSH site - The remote host added earlier. Command - The update script/commands."
To Sum It Up
Jenkins is an open source automation server which has many advantages. Some of them are given below:
Hope you found this article interesting and would go ahead and explore this very popular tool. Happy DevOps!! ππ
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Comments
Comments for this article will be shown here.
Please
Β sign inΒ
to post a comment
Explore More
With our Recruiting AI.
Get hired quickly and reliably.
Set up your profile | Add skills | Take automated interviews.
2020 Β© All rights reserved. GeekyAnts India Pvt Ltd