Day 27 Task: Jenkins Declarative Pipeline with Docker

Day 27 Task: Jenkins Declarative Pipeline with Docker

ยท

4 min read

๐Ÿš€ Mastering CI/CD with Jenkins Declarative Pipeline and Docker ๐Ÿณ๐Ÿ’ป

Ready to supercharge your Continuous Integration and Deployment process? Let's explore the seamless integration of Jenkins Declarative Pipeline with Docker, a match made in DevOps heaven! ๐Ÿš€๐Ÿ”ง

1. Docker in the Pipeline: The Power Duo

  • Containerization Magic: Leverage Docker containers for consistent and reproducible builds, ensuring your application runs smoothly across different environments.

  • Isolation Benefits: Ensure a clean and isolated environment for each stage of your pipeline, preventing dependency conflicts and guaranteeing reliability.

2. Declarative Pipeline Syntax: Simplify, Clarify, Optimize

  • Human-Readable Code: Craft cleaner and more readable pipeline code using the Declarative Pipeline syntax, reducing complexity and making maintenance a breeze.

  • Structured Steps: Define stages, steps, and post-actions effortlessly, providing a clear and concise structure to your CI/CD process.

3. Seamless Integration Steps: A Quick How-To

  • Agent Configuration: Set up your pipeline to run on a Docker-enabled agent, ensuring compatibility and scalability.

  • Docker Commands: Use Docker commands within your pipeline stages to build, tag, and push images seamlessly.

  • Artifact Management: Employ Docker for artifact management, simplifying the storage and retrieval of application artifacts.

4. Parallelism and Efficiency: The Docker Advantage

  • Parallel Execution: Leverage Docker's lightweight nature to parallelize tasks, optimizing build and deployment times for faster results.

  • Resource Efficiency: Docker's resource efficiency allows for running multiple builds concurrently, reducing overall pipeline execution time.

5. Version Control and Dockerfiles: Best Practices

  • Versioning Images: Implement best practices for versioning Docker images, ensuring traceability and rollback options.

  • Optimized Dockerfiles: Craft efficient Dockerfiles to create minimalistic images, improving build speed and reducing resource consumption.

Docker Build and Run

docker build โ€” you can use sh 'docker build . -t <tag>' in your pipeline stage block to run the docker build command. (Make sure you have docker installed with correct permissions.

docker run: you can use sh 'docker run -d <image>' in your pipeline stage block to build the container.

How will the stages look

stages {
        stage('Build') {
            steps {
                sh 'docker build -t trainwithshubham/django-app:latest'
            }
        }
    }

Task-01

  • Create a docker-integrated Jenkins declarative pipeline

  • Use the above-given syntax using sh inside the stage block

  • You will face errors in case of running a job twice, as the docker container will be already created, so for that do task 2.

Step 1: Create a New Pipeline Job

  • Open Jenkins and navigate to the dashboard.

  • Click on "New Item" to create a new pipeline job.

Step 2: Configure Your Pipeline Script

  • In the job configuration, scroll down to the "Pipeline" section.

  • Choose "Pipeline script" from the Definition dropdown.

  • In the script box, enter the following:

pipeline {
    agent any
    stages {
        stage("Code"){
            steps{
                git url: "https://github.com/Mliaqatpk/node-todo-cicd", branch:"master"
                echo "Code Clone"
            }
        }
        stage("Build & Test"){
            steps{
                sh "docker build -t node-app:latest ."
                echo "Code Build & Test"
            }
        }
        stage("Scan"){
            steps{
                echo "Code scan completed !!!"
            }
        }
        stage("Run"){
            steps{
                sh "docker run -d -p 8000:8000 node-app"
                echo "Run Completed"
            }
        }
    }
}

Save and Run Your Job

  • Save your configuration.

  • Run your Jenkins job.

  • Check whether the application is working on port 8000 or not.

Task-02

  • Create a docker-integrated Jenkins declarative pipeline using the docker groovy syntax inside the stage block.

Update Your Pipeline Script

  • Open the configuration of the same job you created for Task-01.

  • Update the script box with the following:

pipeline {
    agent any
    stages {
        stage("Code"){
            steps{
                git url: "https://github.com/Mliaqatpk/node-todo-cicd", branch:"master"
                echo "Code Clone"
            }
        }
        stage("Build & Test"){
            steps{
                sh "docker build -t node-app:latest ."
                echo "Code Build & Test"
            }
        }
        stage("Scan"){
            steps{
                echo "Code scan completed !!!"
            }
        }
        stage("Push"){
            steps{
                withCredentials([usernamePassword(credentialsId:"dockerHub",passwordVariable:"dockerHubPass",usernameVariable:"dockerHubUser")]) {
                    sh "docker login -u ${env.dockerHubUser} -p ${env.dockerHubPass}"
                    sh " docker tag node-app:latest ${env.dockerHubUser}/node-app:latest"
                    sh " docker push ${env.dockerHubUser}/node-app:latest"
                    echo "Code pushed to the DockerHub"
                }
            }
        }
        stage("Deploy"){
            steps{
            sh "docker-compose down"
            sh " docker-compose up -d"
            }
        }
    }
}

Save and Run Your Job

  • Save your configuration.

  • Run your Jenkins job again.

ย