Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: Jenkins Declarative Pipeline with Docker

ยท

3 min read

Introduction ๐Ÿ“œ

Welcome to Day 27 of our Jenkins journey! Today's task is an exciting one as we delve into the integration of Docker with Jenkins declarative pipelines. Docker provides a robust and efficient way to package, distribute, and run applications in isolated environments, and combining it with Jenkins pipelines can significantly enhance your continuous integration and delivery processes.

Task 1: Creating a Docker-Integrated Jenkins Declarative Pipeline ๐Ÿšข

Task 1.1: Setting up the Pipeline

Set Up Jenkins: Install and configure Jenkins on your server or cloud platform.

check Jenkins version

Create a job

Let's kick things off by creating a Jenkins declarative pipeline with Docker integration. This pipeline will serve as the foundation for our containerized workflows.

pipeline {
        agent any
          stages {
            stage("Code") {
                steps {
                   git url: "https://github.com/LondheShubham153/django-todo-cicd.git" , branch: "develop"
                   echo "code cloned successfully"
                }
            }

            stage("Build") {
                steps {
                    sh "docker build . -t django-app"
                    echo "docker build successfully"
                }
            }

            stage("Deploy") {
                steps {
                    sh "docker run -d -p 8000:8000 django-app:latest"
                    echo "docker container run successfully:) "
                }
            }
        }
 }

Task 1.2: Use the above-given syntax using sh inside the stage block (Adding Docker Commands in Stage)

To leverage Docker within our pipeline, we'll use the sh command inside the stage block. This ensures that our steps run within a Docker container.

build now,

Check output,

check if the application is running fine:

OSm, the application running fine.

Task 1.3: Handling Duplicate Job Runs

Running the job multiple times may result in errors due to existing Docker containers. To address this, proceed to Task 2.

Task 2: Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block ๐Ÿณ

Task 2.1: Embracing Docker Groovy Syntax

Let's enhance our pipeline by utilizing Docker Groovy syntax directly within the stage block. This simplifies our Docker-related commands and promotes cleaner code.

stage("Deploy") {
                steps {
                    sh "docker-compose down"
                    echo "docker container is down:) "
                    sh "docker-compose up -d"
                    echo "docker container is up & running successfully:) "
                }
            }

I built the same pipeline multiple times, it is working fine

check if the container is running

check if an application running fine :

Conclusion ๐Ÿ’ฅ

In today's hands-on session, we've successfully integrated Docker with Jenkins declarative pipelines. This powerful combination allows us to encapsulate our application environments and streamline the continuous integration process.

By following the steps outlined in Task 1, you've set up a basic Docker-integrated pipeline. Task 2 takes it a step further by introducing Docker Groovy syntax, enhancing readability and maintainability.

Now armed with this knowledge, go ahead and elevate your Jenkins pipelines to new heights with Docker integration! Happy coding! ๐Ÿš€

ย