"JenDockFlow"

"JenDockFlow"

JenDockFlow is a continuous integration and continuous deployment (CI/CD) solution that uses Jenkins as the central server and Docker for containerization.

Here are some of the key features of JenDockFlow:

  1. Utilizes Jenkins as the central CI/CD server.

  2. Integrates with Docker to enable efficient containerization of applications.

  3. Leverages GitHub's webhooks to automate the cloning of code repositories, Docker builds, and pushes to Docker Hub.

  4. Incorporates declarative checkout SCM function for streamlined retrieval of source code from version control systems.

  5. Eliminates manual intervention, ensuring a smooth and continuous flow from development to production.

  6. Maintains a focus on collaboration and ease of version control through GitHub integration.

  • To successfully set up your CI/CD pipeline, you will need the following pre-requisites:

Provision the EC2 instances:

  • Instance 1: Jenkins Master

  • Instance 2: Dev Agent

  • Install the essential packages for setting up a Jenkins Declarative CI/CD project:

    1. Install Java Development Kit (JDK):

      • Ubuntu/Debian:

          sudo apt update
          sudo apt installopenjdk-17-jre
          java -version
        
    2. Install Jenkins:

      • Ubuntu/Debian:

          curl -fsSL https://pkg.jenkins.io/debian-
          stable/jenkins.io-2023.key | sudo tee \
          /usr/share/keyrings/jenkins-keyring.asc >
          /dev/null
          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
          sudo apt-get update
          sudo apt-get install jenkins
        

        Refer this - Link to install jenkins

  1. Install Git:

    • Ubuntu/Debian:

        sudo apt update
        sudo apt install git
      
  2. Install Docker:

  1. Install Jenkins plugins:

    • Log in to the Jenkins web interface.

    • Go to given path copy the password and pasre it below.

  • Install the suggested plugins.

  • We have successfully logged in to Jenkins.

GitHub Repository:

Have a repository in GitHub where your source code is hosted.

Dockerfile:

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

This Dockerfile builds a Node.js application image:

  • It uses the node:12.2.0-alpine base image.

  • The working directory is set as /app.

  • The current directory is copied to the container's /app.

  • Application dependencies are installed.

  • Tests are executed.

  • Port 8000 is exposed.

  • The container will run the node app.js command to start the application.

    Docker-compose.yml

      version: '3.9'
    
      services:
        web:
          image: mehboob1129/node-todo-cicd-with-jenkins:latest
          ports:
            - "8000:8000"
    
    • version: '3.9': Specifies the version of the Docker Compose file syntax being used.

    • services: Begins the section where you define the services for your application.

    • web: Sets the name of the service.

    • image: mehboob1129/node-todo-cicd-with-jenkins:latest: Specifies the Docker image to use for the "web" service.

    • ports: ["8000:8000"]: Maps port 8000 on the host system to port 8000 within the container. This allows traffic to be directed from the host to the container's application running on port 8000.

Add a webhook to your Jenkins pipeline for GitHub integration:

  1. In Jenkins project configuration, enable "GitHub hook trigger for GITScm polling" in the "Build Triggers" section.

  2. On your GitHub repository, go to "Settings" > "Webhooks" > "Add webhook".

  3. Set the Jenkins webhook URL as <Jenkins URL>/github-webhook/ and choose the desired events.

  4. Save the webhook configuration.

  • Steps to connect Jenkins Master to Jenkins Agent:

  1. Generate SSH keys on the Jenkins Master: ssh-keygen -t ed25519.

  2. Copy the public SSH key to the Jenkins Agent:

  3. Configure Jenkins to connect with the Jenkins Agent:

    • Access Jenkins web interface.

    • Go to "Manage Jenkins" > "Manage Nodes and Clouds" > "New Node".

    • Provide a name, select "Permanent Agent", and set remote directory.

    • Choose the "Launch agent agents via SSH" option.

    • Specify the Jenkins Agent IP/Hostname, credentials (using the generated SSH private key).

    • Save the configuration.

  1. Launch the Jenkins Agent and verify the connection

We have successfully connected Jenkins Master with Jenkins Agent. Now, we can leverage the Jenkins Agent for executing CI/CD tasks in collaboration with the Jenkins Master.

Login to Docker Hub and push an image with credentials .

  • Create a new credential in Jenkins with your Docker Hub account details(userID &Password).

  • Go to Manage Jenkins > Manage Credentials > Global > Add Credentials and fill out the form with your username and password.

  • In your Jenkinsfile, use the withCredentials block to access your Docker Hub credentials.

Wright jenkins file

pipeline {
    agent { label "Dev-agent" }

    stages {
        stage("Clone Code") {
            steps {
                echo "Cloning the code"
                git url: "https://github.com/Mehboob1129/node-todo-cicd-with-jenkins.git", branch: "master"
            }
        }

        stage("Building") {
            steps {
                echo "Building the image"
                sh "sudo docker build -t node-todo-cicd-with-jenkins ."
            }
        }

        stage("Push to Docker Hub") {
            steps {
                echo "Pushing the image to Docker Hub"
                withCredentials([usernamePassword(credentialsId:"DockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                sh "docker tag node-todo-cicd-with-jenkins ${env.dockerHubUser}/node-todo-cicd-with-jenkins:latest"
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker push ${env.dockerHubUser}/node-todo-cicd-with-jenkins:latest"
                }
            }
        }

        stage("Deploy") {
            steps {
                echo "Deploying the container"
                sh "docker-compose down && docker-compose up -d"
            }
        }
    }
}
  1. The pipeline is set to execute on a Jenkins agent labeled "Dev-agent", indicating where the pipeline will run.

  2. pipeline starts with connecting to agent

  3. "Clone Code" stage: Clones the code repository from GitHub.

  4. "Building" stage: Builds a Docker image named "node-todo-cicd-with-jenkins."

  5. "Push to Docker Hub" stage: Tags the Docker image, logs in to Docker Hub using credentials, and pushes the image to Docker Hub.

  6. "Deploy" stage: Stops and removes existing containers and deploys the application using Docker Compose.

The stages represent different phases of the CI/CD pipeline, and the steps within each stage define the specific actions to be performed.

Now we are going to use declarative checkout SCM function in Jenkins:

  1. First save your pipeline code in your github repository as jenkinsfile .

  2. Open Configurations in jenkins

  3. select "pipeline scipt from SCM"

  4. In "SCM" select git>paste repository url

  5. In "script path" select jenkinsfile

Refer my you tube channel-

https://youtu.be/SStWCruT_e4

Conclusion

Congratulations! you have successfully set up a declarative Jenkins CI/CD pipeline for your project "jendockflow". This pipeline includes steps such as provisioning EC2 instances, connecting the Jenkins master and build node, code cloning via webhook, Docker login and code push using Groovy syntax, and declarative checkout SCM.

By following these steps, you now have a robust CI/CD pipeline in place for your project, allowing for seamless automation and continuous integration. Happy coding!

Thank you,