Jenkins Declarative Pipeline with Docker

Jenkins Declarative Pipeline with Docker

·

5 min read

Welcome to the new blog

Here we'll make a complete project using Jenkins declarative pipeline

Kindly go through previous blog to understand about declarative pipeline :here

Jenkins Declarative Pipeline

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.

  • For example :

      pipeline {
          agent any
          stages {
              stage('Build') {
                  steps {
                      echo 'Building the application...'
                  }
              }
              stage('Test') {
                  steps {
                      echo 'Running tests...'
                  }
              }
              stage('Deploy') {
                  steps {
                      echo 'Deploying the application...'
                  }
              }
          }
          post {
              always {
                  echo 'Pipeline completed.'
              }
          }
      }
    
    • In this example, 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.

  • We can also Integrate third-party tools seamlessly into your pipeline for enhanced functionality. Integrate security scanning, performance testing, or code analysis tools directly into your pipeline stages (such as docker).

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

Pre-requisites :

  • Jenkins, docker must be installed

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

  • Install the docker pipeline plugin in Jenkins (Not necessary)

Step 1: Go to your Jenkins dashboard ( refer this ), click on New item give a name and select Pipeline.

Step 2: Scroll down and check on git , and give URL of your GitHub repo

  • If you want to build any triggers, you can check mark on GitHub hook

Step 3: Let's write the groovy syntax for the pipeline

Here's the basic structure of the stages :

pipeline {
    agent any
    stages {
        stage("Clone Code") {
            steps {
                echo "Cloning the code"
            }
        }
        stage("Build") {
            steps {
                echo "Building the image"
            }
        }
        stage("Push To Docker Hub") {
            steps {
                echo "Pushing the image to docker hub"
            }
        }
        stage("Deploy") {
            steps {
                echo "Deploying the container"
            }
        }
    }
}
  • Save it and click on Build now to check if it's working, as you can see we have successfully built the structure

Step 4: Go back to configure and pass commands in the pipeline script to clone the GitHub repo, build the docker image, push it to the docker hub and finally deploy it

  • In this, we use "sh" to write shell commands in Groovy syntax
pipeline {
    agent any
    stages {
        stage("Clone Code") {
            steps {
                echo "Cloning the code"
                git url:"https://github.com/srahul0502/django-todo-cicd.git", branch: "develop"
            }
        }
        stage("Build") {
            steps {
                echo "Building the image"
                sh "docker build -t django-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 django-app ${env.dockerHubUser}/django-app:latest"
                sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                sh "docker push ${env.dockerHubUser}/django-app:latest"
                }
            }
        }
        stage("Deploy") {
            steps {
                echo "Deploying the container"
                sh "docker run -d -p 8000:8000 srahul0502/django-app: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

      Under kind select "Username with Password

      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

    • Now we use withCredentials which is a groovy syntax to use the credentials and passed them where ever it was required.

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

Pushing to dockerHub completely depends on you, you can skip this if you want, but you can learn about env variables and how to use them. Give it a try.

Stage "Deploy" :

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

Step 5: Click on Build..!!

  • 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