šŸš€Day 27 Task: Jenkins Declarative Pipeline with DockeršŸ˜ƒ

šŸš€Day 27 Task: 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

Ā·

2 min read

Overview:

This article demonstrates the creation of a Docker-integrated Jenkins declarative pipeline for a Node.js application in two tasks. Task-01 uses the sh command for Docker build, potentially causing errors on reruns. Task-02 resolves this by employing Docker Groovy syntax with reuseNode to prevent errors during multiple job runs.

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

  • Create a docker-integrated Jenkins declarative pipeline

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

  1. Create a docker-integrated Jenkins declarative pipeline

In Jenkins, Click on ā€œNew Itemā€, create a new pipeline job, and select ā€œPipelineā€ as the project type.

2. Enter pipeline Script

  • 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

Task-02

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

  • You wonā€™t face errors

  • Complete your previous projects using this Declarative pipeline approach

(Note : If you face error regarding docker agent ,install ā€œDocker pipelineā€ plugin)

pipeline {
    agent any

    stages{
      stage('code'){
            steps{
                echo "code"
                }
            }


        stage('Build'){
            agent {
                docker {
                    image 'sanjanathamke/nodejs'
                    reuseNode true
                }

                }
            steps{
                echo "building"
                sh 'node --version'
                }  
            }
            stage('Test'){
            steps{
                echo "Testing"
                }
            }

        stage('Deploy'){
            steps{
                sh "docker run -d sanjanathamke/nodejs"
            }
        }
    }
}

When reuseNode set to true: no new workspace will be created, and current workspace from current agent will be mounted into container, and container will be started at the same node, so whole data will be synchronized.

Click on Save and then click on Build Now.

  • Now you can build Now multiple times ,it wonā€™t show error

Conclusion:

Declarative pipelines in Jenkins, coupled with Docker integration, offer a structured approach to CI/CD workflows. Choosing between sh command and Docker Groovy syntax depends on project needs. The provided configurations enable efficient management of Dockerized applications in Jenkins for a reliable CI/CD process.

_Thank you for reading

Happy Learning:)

Ā