Day 27: Jenkins Declarative Pipeline with Docker

ยท

3 min read

Day 27: Jenkins Declarative Pipeline with Docker

Day 26 was all about a Declarative pipeline, now its time to level up things, let's integrate Docker and your Jenkins declarative pipeline

Use your Docker Build and Run Knowledge

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: Docker-Integrated Jenkins Declarative Pipeline

  • 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

  1. Click on "New Item" to create a new Jenkins job.

  2. Enter Job Details:

    • Provide a name for the job

    • Choose "Pipeline" as the job type.

  1. Add a description

  1. Set your stages, steps, and parameters in the pipeline script section of the configuration.
pipeline {
    agent any
    stages{
        stage ('Code Clone') {
            steps {
                git url : 'https://github.com/Sompandey01/django-todo-cicd.git' , branch : 'develop'
            }
        }
        stage ('Build') {
            steps {
                sh 'docker build . -t  django-todo-cicd:latest'
            }
        }
        stage ('Testing') {
            steps {
                echo 'testing'
            }
        }
        stage ('Deploy') {
            steps {
                sh 'docker run -d -p 8000:8000 django-todo-cicd:latest'
            }
        }
    }
}

  1. "Save" and click on "Build Now" to start the pipeline.

  2. Check the "Console Output"

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

You will face errors in case of running a job twice, as the docker container will be already created.

Task-02: Enhancing Docker-Integrated Jenkins Declarative Pipeline

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

  • Complete your previous projects using this Declarative pipeline approach.

Simply, modify the script using docker-compose down and docker-compose up command

Click on "Save" and then click on "Build Now".

When the build is successful check whether the application is working on port 8000 or not.

"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/ for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!

ย