Freestyle CI/CD Pipeline using Jenkins

Muhammad Qasim Nauman
8 min readJul 14, 2024

--

Hey Folks! I hope you are doing great. Moving on in the DevOps journey, we will see Jenkins in more detail by setting up a freestyle CI/CD pipeline.

What is CI/CD?

It stands for continuous integration and continuous delivery/deployment, a set of practices that help speed up and streamline the software development process. CI/CD is a key part of DevOps methodology, which aims to improve collaboration between development and operations teams. This helps ensure that code is always in a releasable state and that new features and bug fixes reach customers as quickly as possible. It can also help maintain software quality by reducing the chance of errors.

What is a CI/CD Pipeline?

A pipeline is a method that guides software development through the steps of developing, testing, and delivering code, often known as CI/CD. The goal of automating the process is to reduce human error while maintaining a consistent method for releasing software. The pipeline may comprise tools for compiling code, unit testing, code analysis, security, and binary production. For containerized settings, this pipeline would include packaging the code as a container image that could be distributed across a hybrid cloud.

You are getting bored with theory, Right? Let’s move on to hands-on

We will be setting up a freestyle pipeline using Jenkins by creating a new Jenkins Job.

What is Jenkins's Job?

A Jenkins build job defines the configuration for automating a single operation or phase in the application development process. These responsibilities involve acquiring dependencies, compiling, archiving, or converting code, as well as testing and delivering code across several environments.

Jenkins supports a variety of build jobs, including freestyle projects, pipelines, multi-configuration projects, folders, multibranch pipelines, and organizational folders.

Today we will be working on a freestyle project.

Freestyle project in Jenkins allows you to build, test, and deploy software with a range of settings and configurations.

Pre-requisites:

Now that we have Jenkins and our project, we will start building the pipeline. For that head over to the Jenkins dashboard and create a new job.

We will be creating a new item, and in it, we will select the freestyle project, give it a name, and press ok.

It will take us to the pipeline where we will be configuring it.

As our code is stored on GitHub, we will fetch the code from there, and after that, we will be building it and deploying it using docker.

For that, we will select the Github project from the General settings and paste the URL of the repository in the text area. (Link to project)

After scrolling a bit down, under the Source Code Management tab we select Git, and then we need to paste the git-url of the repo.

To establish the connection between Jenkins and GitHub, we need SSH keys because SSH is used by Jenkins to access GitHub and vice versa.

Now to generate the keys for that we use the following command in the terminal.

ssh-keygen

This will generate two keys, one public and one private that are stored in the. ssh directory and can be accessed by

cd .ssh

The file id_rsa is the private key and id_rsa.pub is the public key.

Now we will set them up. Go to your GitHub account, Under the settings you will find SSH and GPG Keys

Here we will create a new SSH key.

We will give it a name and enter the public key here, and we will display it using the following commands

cd .ssh
cat id_rsa.pub

After copying it paste in the key and then press Add SSH key.

Here I have added it, Now head over to Jenkins pipeline. After adding the git URL, we will select add credentials.

After this appears we select the kind as SSH Username and private key.

We will be giving it an ID and description. In the username, we will add the current username which we can get through

whoami

And will use it in the Username

As we scroll down, we select the Enter directly command and then copy and paste the private SSH key by adding a new stored value and hitting add.

#command to display private key
cd .ssh
cat id_rsa

After that, we will see the credentials in the drop-down and will select it from there.

Now we have established the connection between GitHub and Jenkins.
We will then give the branch as well from which the application will be built.

Scroll down, In the build steps we add the executing shell, where we will run the commands to dockerize and run the application.

We will be using the docker-compose and it must be in the code. The docker-compose file for this project is.

#docker-compose.yml
version: '3.9'

services:
web:
build: .
ports:
- "8000:8000"

It will build it and then deploy it on port 8000.

We use the following commands in the execute shell.

docker-compose down
docker-compose up -d

It will first stop all the instances that are running and then will build it and start it which will be accessible on port 8000.

Now we will save the configuration and start the build.

As I hit the Build button it started building by first getting the project from Git Hub and then building it using docker-compose.

As it executed successfully, we can now check on port 8000.

Our Application is successfully running on port 8000. Congratulations you have now implemented the CI/CD pipeline using Jenkins.

Bonus

A problem comes when the developer always pushes the code to the branch we need to run the pipeline manually every time. We can automate the following process as well by using Webhooks.

What are Webhooks?

Webhooks are software functions that allow applications to communicate with each other over HTTP, sending data and commands in XML, JSON, or form-encoded serialization. They are triggered automatically when an event occurs and can be used to receive data in real time, rather than polling an API to see if data is available.

We can set it directly on GitHub. To set up a webhook we first go to the repository setting and there we will find an option of Webhooks.

We will add a new webhook.

On AWS EC2

If it is on AWS EC2, then we will give the following URL structure in the Payload URL.

https://ec2-public-ip:8080/github-webhook/

And in the content type, we select Application/JSON.

Using Local Host

There is a twist. If I add my local host address, Github denies it and says that it is not accessible over the Internet. As I am working on my local system. For that, we need a public URL accessible over the internet. To get that we use a third-party tool called ngrok. Ngrok allows the mapping of a public URL with our local host.

Setting UP ngrok

To set up, we first need to make an account. After we are taken to this dashboard.

As I am using Linux, so I will first set it up for Linux. To install it we run the following commands.

curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc \
| sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null \
&& echo "deb https://ngrok-agent.s3.amazonaws.com buster main" \
| sudo tee /etc/apt/sources.list.d/ngrok.list \
&& sudo apt update \
&& sudo apt install ngrok

After running these commands we run the following command to give access to use it using the authorization token, you will be given yours below the download command. It will look like this

ngrok config add-authtoken <Your_Unique_Token>

As you run it you can use ngrok. To map the local we use the following command.

ngrok http <Port_no>

We need to specify the port number as well which needs to be mapped. As my Jenkins is running on port 8080. We use the following command.

ngrok http 8080

After the following window appears.

The following, displays my email, version, region, A local IP to check, and a Public URL that is being forwarded for the local host running on port 8080.

We copy the public URL and in the GitHub webhook paste it in the following format.

https://public-url/github-webhook/

And in the Content-type we select the

Press Add Webhook and a Webhook is added.

Now In Jenkins, we first need to install the GitHub Integration Plugin.
For that Goto Jenkins Dashboard -> Manage Jenkins -> Plugins

Under the available plugins tab search for the following

As It appears, check it and it will be installed.

Now We have done all the configuration. Head back to the pipeline’s configuration.

Under the build triggers, you will find the option of GitHub hook trigger for GITScm Polin. Check it and save it.

Congratulations, Now you have implemented a Robust Freestyle CI/CD Pipeline.

Now If you head over to GitHub and Commit any changes, it will automatically start the pipeline.

Remember: The NGROK agent must be running all the time while using the local host otherwise, webhooks won’t work.

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

--

--