Jenkins: A Guide to Setting Up CI/CD Pipelines

Muhammad Qasim Nauman
5 min readJul 11, 2024

--

Hey Folks! We are done with the containerization part of the application, and we will move on to implementing the CI/CD pipeline.

Jenkins. A tool used to automate the build and deploy process. It takes the code from Github or local storage and then builds and deploys it automatically.

We will first begin with the installation of Jenkins on our local system, it can be an EC2 machine running Ubuntu, or VM on my local system.

As Jenkins is a Java-based program first we need to install JDK for that. We use the following command to install it but first, we will update our system.

#updating the system
sudo apt update

After updating we need to add the key to our system, for that, we run the following command

  sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian/jenkins.io-2023.key

Then we add a Jenkins apt repository entry using the following command

 echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]" \
https://pkg.jenkins.io/debian binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null

Now we will run the following commands and Jenkins will be installed on our computer.

#updating the system
sudo apt-get update

#installing jdk
sudo apt-get install fontconfig openjdk-17-jre

#Installing jenkins
sudo apt-get install jenkins

Now to verify the installation we run the following command

sudo systemctl status jenkins

Jenkins runs on port 8080 on the local system. If we are using the EC2 machine then we need to update the security group of our machine by opening a port 8080. Like below

You can choose any IP or you can specify it as well.

But on the other hand on your local system, it runs on http://localhost:8080/. As you will open it will ask for a password, which can be found in the following directory

/var/lib/jenkins/secrets/initialAdminPassword

We display it using the following command

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

The output will be like this

This will be unique for everyone.

As I am using it on the local system If I open http://localhost:8080/ Jenkins will start and I need to enter the password here.

After Entering the password, It takes me to the page where I have to choose the plugins for it.

We will be installing the suggested plugins. It will start the installation and will complete it automatically. After we need to create a new admin user,

We will fill this out and then we will be taken to our Jenkins Dashboard.

As you can see here, mine is a bit different as I have already configured some projects. This was all of the installation and setup of Jenkins.

Now we will implement a simple pipeline and will print Hello World from it.

As for the first time using Jenkins the dashboard will look like this

We will first understand two things about Jenkins, Job and Agent.

Agent is typically a machine, or container, which connects to a Jenkins controller and executes tasks when directed by the controller. Artifact. An immutable file generated during a Build or Pipeline run is archived onto the Jenkins Controller for later retrieval by users.

Jobs are a given set of tasks that run sequentially as defined by the user. Any automation implemented in Jenkins is a Jenkins Job. These jobs are a significant part of Jenkins’s build process. We can create and build Jenkins jobs to test our application or project.

We will first set up Jenkins Agent.

As I click on setup an agent, I have a new page

After you hit, create node this appears

We need to add the remote repository here, from which we will be running our project. For linux it is the following directory.

/home/username

We will leave the rest of it as it is and save it.

We will now create a job, that we call it a pipeline as well.

After hitting the create a new job button, this appears.

Here we need to give it a name, and the type of it. For now, we will be using the freestyle project. Click OK, this will create a new item, and the following dashboard will appear.

We will learn about other things later, For today we will scroll down, and in the build steps, we add the executing shell.

This here is a bash shell, we will be printing hello-world with this using the following command.

echo "Hello World, this is my first Jenkins Demo - $(date)"

After we save it, we come back to main.

Now if we hit on build now It will start building it.

As the build was successful, we will check the console output now.

As you can see we have our output is logged in the console.

Congratulations! You have successfully setup your first code pipeline.

This was all from today, Thank you for giving it a read.

--

--