Day-24 Task: Complete Jenkins CI/CD  Project

Day-24 Task: Complete Jenkins CI/CD Project

Β·

6 min read

Hey, I am going to Integrate and Deploy the Node JS To-Do app using EC2server, GitHub, Docker, and most important SSH connection between Jenkins Job and GitHub and Jenkins with a GitHub hook trigger for GITScm polling feature of Jenkins.

Prerequisite

  • Git Understanding

  • GitHub Account

  • AWS Account for the Virtual Machine(EC2 Instance)

  • Docker Understanding

  • Docker Compose Understanding

  • Jenkins Understanding

Note: Here we will use AWS EC2 with Ubuntu Linux OS

Now Let's get started implementing the complete Jenkins CI/CD pipeline

Fork Project To Your GitHub Account

You can use this TO-DO app in case you do not have any project

Here is the link TO Do App Git Repo

Create EC2 Instance (Virtual Machine)

Here we will use Ubuntu as an AWS EC2 Instance(Virtual Machine)

Please check here-> How to Create EC2 Instance step by step

Now we will install Jenkins on this EC2 instance and below are the commands.

Installation Off Jenkins

Please Check Here -> Installation of Jenkins on Ubuntu

Connection Between Jenkins Job & GitHub Repository via GitHub Integration.

To Create an SSH connection between your Server(EC2 Instance) and GitHub we need SSH key, Let's create it.

  • Connect your EC2 instance and go to the Terminal and use the below ssh-keygen command to generate SSH key

      ssh-keygen
    

  • You can see the message as "Your identification has been saved in /home/ubuntu/.ssh/id_rsa Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub The key fingerprint is:"

Adding SSH & GPG keys In GitHub And Jenkins Project

  • Go to GitHub >> Setting >> SSH and GPG keys.

  • Click on New SSH key

  • Provide the Title and select the Key type as Authentication Key and also add public key in the key field that we created using ssh-keygen

  • Click on Add SSH Key and provide your GitHub password to confirm and you will see the SSH key is added

Create Jenkins Project

As earlier we have installed Jenkins on our server and make sure port 8080 is open in the server security group to access the Jenkins dashboard

Here we will use Docker, Docker-compose to deploy the application in the Docker container

  • Access the Jenkins dashboard using server ip with port 8080 as shown below

      <server-ip>:8080
    
  • Now go to the dashboard and create New Item/Create a job

  • Enter the Item/project Name and select the Freestyle project and click on Ok

  • Now once you created the job enters the Description and select the GitHub project and copy and enter the GitHub repository project URL as Project url

  • Now in Source Code Management enter the same repository url we have copied as Repository URL field

  • In the Credentials section click on Add

  • Now in the pop-up window keep Domain as it is

  • In the Kind select SSH Username with private key

  • In the Scope keep it as it is

  • In the ID provide as per your

  • In the Description provide as per

  • In the Username provide your server username in my case it is ubuntu

  • In the Private key add the private key which we created using the ssh-keygen command and click on Add

  • Now in Credentials select which we have created now

  • In the Brances to build keep the Branch Specifier as it is in my case it is *master

Automate Building And Running Containers

Note: make sure Docker and Docker-compose should install on the server

Please check Here -> Install Docker on Ubuntu Server

To install Docker-compose use sudo apt install docker-compose -y command

  • Now in the Build Step under Execute shell run the application using Docker compose add the command Apply and Save

  • For this first, we have to create the Docker compose file to deploy the Docker container
    It is best practice to keep the Docker compose file in the Repository

  • Note: If you got a Permission denaied related error while building

    Add the Jenkins as user in the docker group Use the below command and reboot the server

      sudo usermod -a -G docker jenkins
      sudo rebbot
    
  • After Click on Build Now and after successfully build go the Console output and check as a success the message at the end

  • Now you can see the Docker container is deployed and up and running

  • Now copy your Instance Public-IP address and add :8000/todo to access the application as below

    [Instance-public-ip]:8000/todo

  • BBBBoooooooooMMMMM Congratulation we have successfully deployed our application using docker πŸ•ΊπŸ•ΊπŸ‘πŸ‘πŸŽ‰πŸŽ‰

Automate Deployment Using Webhook

Install Pugin:

  • Here we need the GitHub Integration plugin

    • Open your Jenkins dashboard.

    • Click on the Manage Jenkins button on your Jenkins dashboard

    • Click on Manage Plugins

    • Go to the Available plugin

    • Search GitHub Integrationplugin in the search box

    • Click on Install without restart

    • Click on Restart Jenkins when installation is complete.

    • Relogin into Jenkin

GitHub-Webhook Configuration:

  • Go to your GitHub repository and click on Settings.

  • Click on Webhooks and then click on Add webhook.

  • In the Payload URL field copy and paste your Jenkins environment URL. and at the end of this URL add /github-webhook/

  • In the Content typeselect: application/json and leave the Secret field empty.

  • In the Which events would you like to trigger this webhook? section keep Just the push event. and click on Add webhook

  • Refresh the page until you will get the Right sign βœ”

  • Go to Jenkins >> Your Job >> Configure >>Build Triggers and select GitHub hook trigger for GITScm polling and click on apply and save

  • Now we are at our final step make some changes in the code and watch the Jenkins dashboard

    Here we have changed the heading "Welcome To The Amazing & Awesome DevOps World" to "Welcome To the DevOps Community" and committed the changes at the repository itself

  • After we have changed the code in the repository and committed the changes the magic happens πŸ•ΊπŸ•ΊπŸ‘πŸ‘πŸŽ‰πŸŽ‰... Our Job build execution starts without clinking on Build Now.

  • WebHook is added to trigger the execution of Jenkins jobs based on GitHub events. If anyone in the developer team changes the code, it will automate the build now process like this.

  • We can also see the docker container running in the command line

  • And Finally!!! the changes have been reflected as well

BBBBoooooooooMMMMM Congratulation we have successfully automated to trigger the job using Webhook πŸ•ΊπŸ•ΊπŸ‘πŸ‘πŸŽ‰πŸŽ‰


Thank you for reading the article.

Thanks for your valuable time.

Happy Learning !... Keep Learning ! 😊

Β