Jenkins Declarative Pipeline with Docker

Jenkins Declarative Pipeline with Docker

In today's blog, we'll be diving into the exciting world of Docker integration within Jenkins pipelines.

If you're familiar with the basics of Declarative Pipelines and Docker commands, you're in for a treat. If not, don't worry; we'll guide you through the process step by step.

Let's understand more about the Jenkins declarative pipeline :

  • In Jenkins, a Declarative Pipeline is a way to define your software delivery process as code. It's intended to make building continuous integration and continuous delivery (CI/CD) pipelines easier by providing a structured and opinionated syntax. Rather than scripting the pipeline logic from scratch, Declarative Pipelines allow you to describe the desired flow of your pipeline using a human-readable and simple syntax.

  • the pipeline runs on any available agent goes through three stages (Build, Test, Deploy), and then always prints a message indicating pipeline completion.

  • By default, stages in a Jenkins Declarative Pipeline run sequentially, meaning one stage starts only after the previous stage has been completed successfully.

Let's make a project Integrating the docker and Jenkins pipeline :

  • Jenkins, docker must be installed

  • make sure you add Jenkins and $USER to the docker group

  • Open your web browser and navigate to your Jenkins instance.

  • log in to your Jenkins account.

  • Once logged in, you should be on the Jenkins dashboard.

    1. Create New Job:

      • Look for an option such as "New Item," "Create New Job," or "New Project" on the dashboard. Click on it.

Enter Job Details:

  • Name: Provide a name for your job. This will be used to identify the job within Jenkins.

  • Type: Choose "Pipeline" as the job type. This will allow you to define your job's tasks using a pipeline script.

    Add a description and Scroll down and check on git , and give URL of your GitHub repo

    Configure Pipeline:

    • After selecting "Pipeline," you'll typically see a section where you can define the pipeline

    • Write or configure your pipeline script based on your requirements. A basic pipeline script might look like this

    • In this pipeline script to clone the GitHub repo, build the docker image, push it to the docker hub, and finally deploy it

        pipeline {
            agent any
            stages {
                stage("Clone Code") {
                    steps {
                        echo "Cloning the code"
                        git url:"https://github.com/LondheShubham153/node-todo-cicd/", branch: "master"
                    }
                }
                stage("Build") {
                    steps {
                        echo "Building the image"
                        sh "docker build -t node-todo-cicd ."
                    }
                }
                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-app-demo ${env.dockerHubUser}/node-app-demo:latest"
                        sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                        sh "docker push ${env.dockerHubUser}/node-app-demo:latest"
                        }
                    }
                }
                stage("Deploy") {
                    steps {
                        echo "Deploying the container"
                        sh "docker run -d -p 8000:8000 kajalgirdhar05/node-app-demo:latest"
                    }
                }
            }
        }
      

      Let's break down this syntax :

      Stage "Clone Code" :

      • git URL - in this, we pass the GitHub repo URL , branch to be cloned

Stage "Build" :

  • We use sh to write shell commands in groovy

  • docker build -t django-app. is used to build the image out of a docker file in the GitHub repo it will clone

    Stage "Push to docker Hub" :

    • Here we push the image to the docker hub, for that we have to first login to docker hub, for that we use environment variables to pass username and password to log in.

    • And we save these environment variables under an ID , now let's see how to create environment variables and use them

      • Go to manage Jenkins > Security > credentials >system > add credentials

Fill in all the remaining details, username and password of your dockerhub account, as you can see there's an ID section where these variables will be stored,make sure you remeber the id .

Click on add/save and you are done with creating the env variable.

  • Make sure you tag your image with your username to push it to the docker hub username/image.

    Stage "Deploy" :

    • Deployed the container to port 8000 in detached more (running in the background)

      • Successfully built and deployed

      • It pushed the image to the docker hub as well

        Finally deployed :

I hope now you can create your own declarative pipeline for your projects..!!

Happy Learning :D