Jenkins CI/CD with Github integration

·

3 min read

Jenkins CI/CD with Github integration

In this project, I have created CI/CD pipeline for a todo-node-js app. This pipeline handles the following jobs:

  1. Clone the code from GitHub repo

  2. Build and deploys the code through docker tool

  3. Also, I have connected Jenkins and GitHub through ssh

  4. GitHub poll scm method is used to trigger the build

Pre-requisites:

  1. Clone the project from my GitHub account(Click here)

  2. Create an EC2 instance

  3. Download and install Jenkins

  4. Download and install the docker

  5. 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"]
    
  6. In the security groups,

    1. 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

    2. 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

  1. Go to your instance on which Jenkins is installed and running, go to directory .ssh and type the following command ssh-keygen

  2. You can see that two keys are generated in the file id_rsa and id_rsa.pub, now copy id_rsa.pub

  3. Go to your GitHub account, navigate to your Profile, select Settings, and choose SSH and GPG keys on your left.

  4. 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

  1. Go to your repository>settings>webhooks>add webhook

  2. Here add your Jenkins URL, followed by GitHub-webhook

  3. Select "Send everything" and click on "Add webhook"

    Refer to this blog for more information (Link)

Creating a Jenkins free-style project

  1. 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)

  2. Provide the job description as per your wish and move to add the Github project URL

  3. Next in the source code management section, paste the git hub repo link, specify the branch that you want to build this on

  4. In the credentials section click on Add, this will take you to Jenkins Credentials Provider: Jenkins page

    1. Select SSH username with the private key as kind, provide ID and description according to your wish.

    2. For username, I have added ubuntu as this is the username with which I am working on my EC2 instance

  1. 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

  2. Next, select the github poll scm method for build

  3. 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