Complete Step-by-Step Jenkins CICD with GitHub Integration

Nipulpatel
8 min readJan 5, 2024

--

What is CICD?

CI — Continuous Integration

Normally a single code base is used by multiple developers. So that each and every developer has to merge their changes back to the repository once they finish it. This process can be happened multiple times a day. Once they merge their code changes to the central repository, there is an automated process to trigger a build and run all the test cases related to the project. After running the build, it will provide a detailed description about the failed/ passed test cases and any other issues occurred while running the build process. With this automated process, developers don’t need to compile the project after their code change, the process will do it for them.

CD — Continuous Delivery

After CI part finished, the project is now tested and next step is to deliver it to whatever the environment. (production/ QA) The newly added code changes are now needed to be applied to the deployment.

This process make the developers life easy, as they don’t need to manually build and test the project with every change they do. Also it avoid any manual mistakes that can be happened while building.

Step 1- Create an AWS EC2:

  1. First of all, go to AWS portal, and create a new instance

· AMI: ubuntu.

· Instance type: t2.micro (free tier).

· create a new key-pair or use an existing one

· Allow HTTP.

· Allow HTTPS.

Click on Launch Instance.

and SSH into it

Step 2- Install Jenkins in your AWS EC2 Instance.

You can also refer to this blog to install Jenkins

Step 3 — Allow Port 8080 in the Security Group on EC2 Instance as Jenkins works on Port 8080

This is how it should look now,

Enable Custom TCP Port 8080 for Jenkins on AWS EC2 Instance

Congratulations, Jenkins is now installed on your AWS EC2 Instance

Now to unlock Jenkins we need to go to this path /var/lib/jenkins/secrets/initialAdminPassword and copy the administrative password and paste it into the above screenshot.

After pasting the password the below page will appear. Click on Install suggested plugins.

After the plugins are installed, you need to create the first Admin User. Fill in all the information and click on Save and Continue.

Add the fields accordingly.

The Jenkins homepage will look like this,

Step 3: Start Using Jenkins:

  1. On Jenkins Dashboard, Click on New Item to create your first job or item.

2. Enter the name of your first item and select Freestyle Project and click on Ok.

3. Now we need to configure various parameters of our first job in Jenkins. Under the General section, put the description of your project. Under Project Url paste the GitHub URL of the project for which we need to create the CI CD Pipeline.

4. Under Source Code Management we need to paste the GitHub repository URL as shown in the below screenshot:

Under credentials, we first need to generate the ssh keys on our EC2 Server.
The reason to create ssh keys is to make secure integration between our Jenkins Server and Github and this way we can clone any git repo in this Jenkins instance. You do not need to provide the credentials while configuring the job in Jenkins.

Take the public key and insert that under the settings in GitHub to achieve a password-less connection between Jenkins and GitHub.

For that go to the settings of your GitHub, on the left choose SSH and GPG keys, click on New SSH key, give the name under Title, and paste the public key under the Key section we previously generated on the EC2 server.

After adding the ssh key to our GitHub account we will add the credentials in Jenkins. For that, under credentials click on Add button and click on Jenkins in the drop-down option.

A new window will appear. Select SSH Username with private key under the section Kind. Give any name under section ID and Description, as shown in the below screenshot:

On the other half of this page type the Username of the EC2 machine which in our case is ubuntu and then paste the private ssh key copied from your EC2 server and click on Add button.

Now we have to specify the branch of our GitHub repository. It can be any branch the developer would have been working on. In our case, we will select the master branch.

5. To check whether our GitHub integration with Jenkins is successful we will do a small test by clicking on Build Now on the left navigation bar in our first job as shown below:

After clicking on Build Now we see in the output console that the build has been finished successfully and Jenkins was able to fetch the code from our GitHub repository. That means Jenkins Integration with GitHub is successful.

We can also verify by navigating to the path mentioned in the output console which is:

We can also verify by navigating to the path mentioned in the output console which is:

/var/lib/jenkins/workspace/todo-boon-app

And check if the repo is cloned there. As shown in the below screenshot we see all the files of the GitHub repo have been successfully cloned.

Step 4: Run the code:

To run the code we will go through the steps provided by the developer in the Readme file of the Repo which is as shown below:

Let’s run these commands on the EC2 Instance one by one:

$ sudo apt install nodejs
$ sudo apt install npm
$ sudo npm install

The above command will install the dependencies mentioned in the file package.json (ignore the warnings in the output

Finally, to run the app we need to execute the final command, however, before that we also need to allow port 8000 in the Security group of our EC2 Instance as this app will be running on port 8000.

After allowing port 8000 in the inbound policy of Security Group we will run the final command as:

$ node app.js

To check the output open your browser take the public IP of your EC2 Instance with port 8000 and you will see the result as:

Step 5: Install Docker on EC2 Instance:

  1. Install Docker with the following command:
$ sudo apt install docker.io

2. Create your Dockerfile:
We will create the Dockerfile in the same directory /var/lib/jenkins/workspace/todo-boon-app

FROM node:12.2.0-alpine
WORKDIR app
COPY . .
RUN npm install
EXPOSE 8000
CMD ["node", "app.js"]

sudo vim dockerfile

$ sudo vim dockerfile

copy above code into dockerfile

3. Now we will build the Image using the Dockerfile created in the above step:

$ docker build . -t todo-boon-app

In the below output watch the build steps which match with the instructions in the Dockerfile:

We have successfully built the Image from the Dockerfile (just incase if you get error use: sudo usermod -a -G docker $USER
sudo reboot)

&Now its time to build the container from the same Image using

$ docker run -d --name node-boon-app -p 8000:8000 todo-boon-app

We can verify the successful creation of the container with the below command:

$ docker ps

Also, we can test by accessing it on our browser as well:

Up until here, we saw how to dockerize the app manually and make it available for everyone to access.

Step 6: Using Jenkins to Automate the whole process

In Jenkins, we will use all the commands we ran earlier to build the docker Image and then build the container from that image.
For that go under the Build Steps section of Jenkins configure settings of our job and add the following commands in the execute shell area:

docker build . -t todo-node-app
docker run -d --name node-todo-app -p 8000:8000 todo-boon-app

Once you add the commands click on the Save button as shown in the below screenshot:

Now Click on Build Now to build the job using Jenkins.

After you click on Build Now you may encounter an error in your build

To solve this run the below commands in the EC2 Server which adds the Jenkins user to the docker group and restarts the Jenkins server.

$ sudo usermod -aG docker jenkins
$ sudo systemctl restart jenkins

After the Jenkins Server restarts, again run the Build Now and check the Output:

This time around our build is successful and we can see our app accessible from the browser.

thank you for reading!!!!!

--

--

No responses yet