Day 25 Task: Complete Jenkins CI/CD Project - Continued with Documentation

Vibhuti JainVibhuti Jain
2 min read
  1. Setup GitHub repository for source code and CI

Github Repo - https://github.com/Vibhuti456/node-todo-cicd

  1. Setup Job on Jenkins for application

Create two EC2 instance one Jenkins master and another Jenkins Slave:

  1. Jenkins Master: The Jenkins Master is the main server, where you can manage.
  • Scheduling build jobs.

  • Dispatching builds to the slaves for the actual execution.

  • Monitor the slaves (possibly taking them online and offline as required).

  • Recording and presenting the build results.

  • A Master instance of Jenkins can also execute build jobs directly.

  1. Jenkins Slave:
  • Jenkins Slaves are essentially worker machines that execute the actual build jobs.

  • They need Java to run a small Java program called the “agent”.

  • This agent listens for instructions from the Master, fetches build scripts and dependencies, and then uses the JRE to execute the build commands on the Slave machine.

  1. Create Job in Jenkins Application:

Script for Node Application:

pipeline {
    agent { label "dev-server"}

    stages {

        stage("code"){
            steps{
                git url: "https://github.com/Vibhuti456/node-todo-cicd.git", branch: "master"
                echo 'code has cloned in slave system'
            }
        }
        stage("build and test"){
            steps{
                sh "docker build -t node-app-test-new ."
                echo 'image has built in slave system'
            }
        }
        stage("scan image"){
            steps{
                echo 'image has been scanned'
            }
        }
        stage("push"){
            steps{
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker tag node-app-test-new:latest ${env.dockerHubUser}/node-app-test-new:latest"
                sh "docker push ${env.dockerHubUser}/node-app-test-new:latest"
                echo 'Image has been pushed in slave system'
                }
            }
        }
        stage("deploy"){
            steps{
                sh "docker-compose down && docker compose up -d"
                echo 'Deployement has been done successfully'
            }
        }
    }
}

  1. Setup security credentials in Jenkins

Set your credential in Jenkins:

  1. Setup GitHub WebHook

Go to the settings of your project which you are going to deployed.

Go to the webhooks in side dashboard -

In Payload URL we need to give our jenkins slave IP address/-github-webhook/

  1. Configuration of Jenkins Project with Webhooks -

Output of Jenkins Pipeline Stages:

Application:

0
Subscribe to my newsletter

Read articles from Vibhuti Jain directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vibhuti Jain
Vibhuti Jain

Hi, I am Vibhuti Jain. A Devops Tools enthusiastic who keens on learning Devops tools and want to contribute my knowledge to build a community and collaborate with them. I have a acquired a knowledge of Linux, Shell Scripting, Python, Git, GitHub, AWS Cloud and ready to learn a lot more new Devops skills which help me build some real life project.