Jenkins Declarative CI/CD Pipeline -docker, Jenkins

Jenkins Declarative CI/CD Pipeline -docker, Jenkins

this is version 1 - jenkins declarative ci/cd pipeline in Jenkins. this pipeline builds an image and pushes it into docker hub then we can run the application on the ec2 instance.

Introduction

In today's fast-paced development environments, automating Continuous Integration and Continuous Deployment (CI/CD) processes is crucial. This blog post will guide you through setting up a Jenkins Declarative Pipeline to automate the build and deployment of a Django web application using Docker.

Prerequisites

Before diving into the CI/CD pipeline, make sure you have the following set up:

  • Jenkins installed and configured.

  • Docker installed on the Jenkins server.

  • Docker Hub account for image storage.

Jenkins - credentials -

To use the docker username and password, add the global credentials in Jenkins-
DockerHub username, DockerHUbpassword, and credentials ID.

GitHub- webhook -

add webhook into the Github project and allow the events as per your choice (here i selected "Send me everything").

Jenkins Declarative Pipeline

Let's break down the Jenkinsfile:

groovyCopy codepipeline {
    agent any 

    stages {
        stage("Clone Code") {
            steps {
                echo "Cloning the code"
                git url:"https://github.com/krishkprawat/django-notes-app-kp", branch: "main"
            }
        }
        stage("Build") {
            steps {
                echo "Building the image"
                sh "docker build -t my-note-app ."
            }
        }
        stage("Push to Docker Hub") {
            steps {
                echo "Pushing the image to Docker Hub"
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]){
                    sh "docker tag my-note-app ${env.dockerHubUser}/my-note-app:latest"
                    sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                    sh "docker push ${env.dockerHubUser}/my-note-app:latest"
                }
            }
        }
        stage("Deploy") {
            steps {
                echo "Deploying the container"
                sh "docker-compose down && docker-compose up -d" 
            }
        }
    }
}

Exploring Each Stage

  1. Clone Code:

    • This stage fetches the latest code from the specified GitHub repository using the Git plugin.
  2. Build:

    • The Docker image for the Django application is built using the Dockerfile present in the same directory as the Jenkinsfile.
  3. Push to Docker Hub:

    • The built image is tagged and pushed to Docker Hub. Jenkins credentials ensure secure access to the Docker Hub account.
  4. Deploy:

    • The existing Docker containers are stopped, and the application is deployed using docker-compose. This ensures that the latest image is used.

Conclusion

By implementing this Jenkins Declarative Pipeline, we created a streamlined CI/CD process for your Django application. Commits to your GitHub repository trigger the pipeline, automating the build, testing, and deployment of your application. This not only saves time but also ensures consistency across different environments.

Thank you for reading, and happy automating! connect with me - https://www.linkedin.com/in/krishnapal-rawat/