Jenkins CICD with GitHub Integration

Jenkins CICD with GitHub Integration

We will discuss how to deploy a node.js application on EC2 instance, and furthermore, we will create a robust CI/CD pipeline by utilizing Jenkins.

The following are the prerequisites required for this project:

AWS EC2 account: You should have an active AWS account to launch and configure an EC2 instance.

GitHub account: You need a GitHub account to store your project code and connect your Jenkins pipeline to your code repository.

Docker: Docker is required to create and run a containerized version of your node.js application.

Jenkins: You should have a Jenkins server set up to create a continuous integration and deployment pipeline for your application.

What is CICD pipeline?

A CI/CD pipeline is a set of automated processes that help developers to build, test, and deploy software changes quickly and reliably. It involves frequent integration of code changes into a shared repository, followed by automated testing to ensure code quality. If the testing phase passes, the code is automatically deployed to production or a staging environment. This helps teams to detect and fix issues early on in the development process, reduce the risk of errors, and speed up the delivery of software changes.

Step 1: Create EC2 instance and connect.

Step 2: Install Jenkins on the EC2 by using the following commands.

For Ubuntu :

#Install Jenkins using the package manager: ubuntu
sudo apt update
sudo apt install openjdk-11-jdk
# Check the java version.
java -version

#Import the GPG keys of the Jenkins repository using the following wget command
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
# Add the Jenkins repository
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null

# Update and install the jenkins
sudo apt update
sudo apt install jenkins
#Check the jenkins are installed properly or not.
systemctl status jenkins
#you will see the Active: active (running)

After running the above command we can access the Jenkins.

#use the following code to add the Administrator Password
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

In Jenkins set up the credentials.

Dashboard > NewItem > (Enter an item name) > Select the Freestyle project > Click Ok.

Add Description and Select the GitHub project and add paste the GitHub URL.

https://github.com/guru-dath/node-todo-cicd.git

In Below - Source Core Management > select Git and paste the same URL there also.

--- we will add the credentials so that Jenkins can access the code from GitHub.

Here we need add the SSH Key.

Go to your EC2 instance and type the command

ssh-keygen

It will create 2 keys. and go inside .ssh folder you will see the id_rsa id_rsa.pub files.

Now we need a private key for Jenkins so use cat command to see the id_rsa file. (# cat id_rsa).

copy the private key and paste it to the private key section in Jenkins.

In addition to that we need to add the public key to Github.

Go to setting In GitHub > setting > select the SSH and GPG Key in the right and add the New SSH key. Its a public key we will get from by doing #cat id_rsa.pub in Ec2 instance).

Now go to Jenkins > Test Build.

As a result, it clones the repository from GitHub to Instance.

To check the README section of a repository on GitHub and install the necessary dependencies.

Next, add port 8000 to the security group settings of your EC2 instance.

After executing all these commands, check if you can access the URL. If it is accessible. if it's accessible then congratulation.

Next step, we have to Dockerise the application so that anyone can use it.

Go EC2 > Install the docker.

sudo apt  install docker.io

Next, create the Dockerfile. If a Dockerfile already exists in the folder, remove it first and then create a new one.

Use vi or nano command. # nano Dockerfile. add the following commands.

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

Build the Docker image using this Dockerfile

docker build -t todo-node-app

Image (todo-node-app:latest) has been created successfully.

Now we will create a container from this image - #docker run -d --name todoappnode -p 8000:8000 todo:latest

After the run, the docker command container will be created and it will run.

Till now we are doing the manual method.

Let's do automate the process.

Go to Jenkins -> add the same steps in executed shell.

And Now, we will configure a webhook so that the job is automatically triggered every time there is an update or deletion in the repository, and it should perform the subsequent processes.

Note: Remove/Kill the existing container first.

Go to repository settings > webhook > Add webhook -> Payload URL add Jenkins URL here and content type > application.json > Add webhook.

Go to Jenkins select the GitHub hook trigger for GITScm Polling.

Go to GitHub, add something to the repository, and commit it. This will automatically trigger Jenkins to build.

If it builds successfully, it is called automation. As developers, we commit the code and Jenkins automatically takes the code and runs the application.

***********************************************************************

Thanks for reading my blog about Jenkins CICD with GitHub Integration! I hope it was helpful in understanding how to manage software packages and services on your system. Don't hesitate to reach out if you have any questions or feedback.