In this project, I have created CI/CD pipeline for a todo-node-js app. This pipeline handles the following jobs:
Clone the code from GitHub repo
Build and deploys the code through docker tool
Also, I have connected Jenkins and GitHub through ssh
GitHub poll scm method is used to trigger the build
Pre-requisites:
Clone the project from my GitHub account(Click here)
Create an EC2 instance
Download and install Jenkins
Download and install the docker
Create a Dockerfile to install the necessary packages required to create a container
FROM node:12.2.0-alpine WORKDIR app COPY . . RUN npm install RUN npm run test EXPOSE 8000 CMD ["node","app.js"]
In the security groups,
Open port 8080 (as this is where the Jenkins server will be running )and make sure that the source is set to anywhere IP4 as we will also add a GitHub webhook for this port
Open port 8000, as this is where our application is running
Integrating GitHub and Jenkins
First, to integrate GitHub and Jenkins we shall generate an ssh key in the Jenkins system and use this Jenkins ssh public key to connect with GitHub
Go to your instance on which Jenkins is installed and running, go to directory .ssh and type the following command
ssh-keygen
You can see that two keys are generated in the file id_rsa and id_rsa.pub, now copy id_rsa.pub
Go to your GitHub account, navigate to your Profile, select Settings, and choose SSH and GPG keys on your left.
Click on new ssh key, you can choose any title and in the key section, paste the SSH key that you copied from Jenkins system and click "Add SSH key"
Adding webhook
Go to your repository>settings>webhooks>add webhook
Here add your Jenkins URL, followed by GitHub-webhook
Select "Send everything" and click on "Add webhook"
Refer to this blog for more information (Link)
Creating a Jenkins free-style project
Create a new Jenkins freestyle project named "todo-node-app"(Refer this blog for more information on how to create a Jenkins free style project)
Provide the job description as per your wish and move to add the Github project URL
Next in the source code management section, paste the git hub repo link, specify the branch that you want to build this on
In the credentials section click on Add, this will take you to Jenkins Credentials Provider: Jenkins page
Select SSH username with the private key as kind, provide ID and description according to your wish.
For username, I have added ubuntu as this is the username with which I am working on my EC2 instance
Now as we are using an ssh connection to integrate Jenkins and GitHub, we shall provide the private ssh key to Jenkins and since we have already provided the public key to GitHub, a successful SSH connection will be established between GitHub and Jenkins
Next, select the github poll scm method for build
In the build section, select execute shell and add the following 2 docker commands.
docker build . -t cicd-github-integration docker run -d -p 8000:8000 cicd-github-integration:latest
THAT'S IT !!!
We have successfully created a Jenkins freestyle job and now if there are any pushes in the github, it will trigger an automatic build on jenkins and we can see that the server is runnig on port no 8000