Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: Jenkins Declarative Pipeline with Docker

ยท

4 min read

This is #90DaysofDevops challenge under the guidance of Shubham Londhe sir.

Introduction

Welcome to Day 27 of our journey! Today, we're diving into the integration of Docker with your Jenkins declarative pipeline. If you're ready to level up your pipeline game, let's explore how Docker can enhance your workflows.

Utilizing Docker Build and Run Commands

To incorporate Docker into your Jenkins pipeline, you'll need to leverage two fundamental commands:

  1. docker build: Use this command to build your Docker image. In your pipeline's stage block, you can execute sh 'docker build . -t <tag>'. Ensure that Docker is installed with appropriate permissions.

  2. docker run: This command enables you to run your Docker container. Within your pipeline's stage block, you can execute sh 'docker run -d <image>' to initiate the container.

Structuring Your Pipeline Stages

Let's take a closer look at how your pipeline stages might appear:

stages {
    stage('Build') {
        steps {
            sh 'docker build -t supriya279/node-todo-app:latest'
        }
    }
}

Task-01: Create a Docker-Integrated Jenkins Declarative Pipeline

  1. Access Jenkins Dashboard: Log in to your Jenkins server and navigate to the dashboard.

  2. Create a New Pipeline Job: Click on "New Item" or "New Job" to create a new pipeline job. Provide a name for your job, and choose "Pipeline" as the type of project. Click "OK" to proceed.

  3. Configure Pipeline Script: Scroll down to the "Pipeline" section and select "Pipeline script" as the definition.

  4. Define Stages: Inside the pipeline script block, define your stages. For this task, we're focusing on the "code", "build", "Push to Private Docker Hub Repo", and "deploy" stages. Use the following syntax to integrate Docker commands within each respective stage block:

pipeline{

    agent any{

    stages{
        stage("code"){
            steps{
                git url: "https://github.com/Supu-27/node-todo-cicd-project.git", branch: "master"
                echo "code cloned sucessfully"

            }

        }
        stage("build"){
            steps{
                sh 'docker build -t node-app:latest .'
                echo "code build successfully"

            }
        }
        stage("Push to Private Docker Hub Repo"){
            stpes{
                withCredentials([usernamePassword(credentialsId:"DockerHubCreds",passwordVariable:"dockerPass", usernameVariable:"dockerUser")]){
                sh "docker login -u ${env.dockerUser} -p ${env.dockerPass}"
                sh "docker tag node-app:latest ${env.dockerUser}/node-app:latest"
                sh "docker push ${env.dockerUser}/node-app:latest"
                echo "Docker Image Pushed successfully"
                }
            }

        }
        stage("deploy"){
            steps{
                sh "docker-compose up -d"
                echo "Node-app deployed Sucessfully"

            }
            }

        }
    }
    }
}
  1. Save and Run: Save your pipeline configuration. Now, trigger a build to execute the pipeline. This will initiate the Docker build process to create your Docker image.

  2. Handling Errors: If you attempt to run the job multiple times, you might encounter errors because the Docker container has already been created. To address this, proceed to Task-02.

Task-02: Enhance Pipeline with Docker Groovy Syntax

  1. Access Pipeline Configuration: Go back to the configuration of your pipeline job.

  2. Modify Pipeline Script: Update the pipeline script to utilize Docker Groovy syntax within the stage block. This syntax provides more flexibility and control over Docker commands. Adjust the script as follows:

pipeline {
    agent any

    stages {
        stage("code") {
            steps {
                git url: "https://github.com/Supu-27/node-todo-cicd-project.git", branch: "master"
                echo "code cloned successfully"
            }
        }

        stage("build") {
            steps {
                sh 'docker build -t node-todo-app:latest .'
                echo "code build successfully"
            }
        }

        stage("Push to Private Docker Hub Repo") {
            steps {
                withCredentials([usernamePassword(credentialsId:"DockerHubCreds",passwordVariable:"dockerPass", usernameVariable:"dockerUser")]) {
                    sh "docker login -u ${env.dockerUser} -p ${env.dockerPass}"
                    sh "docker tag node-todo-app:latest ${env.dockerUser}/node-todo-app:latest" // Corrected image name
                    sh "docker push ${env.dockerUser}/node-todo-app:latest" // Corrected image name
                    echo "Docker Image Pushed successfully"
                }
            }
        }

        stage("deploy") {
            steps {
                sh "docker-compose up -d" // Make sure docker-compose is installed and config is correct
                echo "Node-app deployed successfully"
            }
        }
    }
}
  1. Save and Run: Save your changes and trigger another build to ensure that the Docker commands are executed using the Groovy syntax.

  2. Congratulations, our pipeline is running successfully.

By following these steps, you'll effectively integrate Docker into your Jenkins pipeline, making your build and deployment processes more streamlined and efficient.

๐Ÿ’ก
If you need help or have any questions, just leave them in the comments! ๐Ÿ“ I would be happy to answer them!
๐Ÿ’ก
If you found this post useful, please give it a thumbs up ๐Ÿ‘ and consider following for more helpful content. ๐Ÿ˜Š

Thank you for taking the time to read! ๐Ÿ’š

ย