Day 27 - Jenkins Declarative Pipeline with Docker

Day 27 - Jenkins Declarative Pipeline with Docker

Day 27 of 90daysofdevops

Task-01

Create a docker-integrated Jenkins declarative pipeline

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

  • Navigate to Jenkins and click on the new item.

  • Create a pipeline project and enter the name and select pipeline and click on ok.

  • Enter description.

  • Now in the "Project Configuration" section, we have to define a pipeline script in the definition.

        pipeline {
            agent any 
            stages {
                stage('Clone Code') { 
                    steps {
                        git url: 'https://github.com/shreeegupta/node-todo-cicd.git' , branch: 'master' 
                    }
                }
                stage('Build') { 
                    steps {
                        sh 'docker build . -t shreya6/node-todo-app:latest' 
                    }
                }
                stage('Test') { 
                    steps {
                        echo "Testing" 
                    }
                }
                stage('Deploy') { 
                    steps {
                        sh "docker run -p 8000:8000 -d shreya6/node-todo-app:latest"
                    }
                }
            }
        }
    

  • Save and click on Build Now and we can check the multi-stage view by clicking on the “Full Stage View” on the project main page.

  • Once the build is completed we can check in the Console Output. We can see the pipeline is successful.

  • Verify the application by running it on the browser.

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

  • Run the pipeline again by clicking on the “Build Now” button, then we will see the error.

Task-02

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

  • To solve this issue we can use a docker image to build the application and for that, we have to install Docker Pipeline & Docker Plugin in Jenkins.

  • Now we have to add a docker image in the Build stage.

        pipeline {
            agent any 
            stages {
                stage('Clone Code') { 
                    steps {
                        git url: 'https://github.com/shreeegupta/node-todo-cicd.git' , branch: 'master' 
                    }
                }
                stage('Build') { 
                    agent {
                        docker {
                            image 'shreya6/node-todo-app:latest'
                            reuseNode true
                        }
                    }
                    steps {
                       echo "Building app using Docker Image" 
                    }
                }
                stage('Test') { 
                    steps {
                        echo "Testing" 
                    }
                }
                stage('Deploy') { 
                    steps {
                        sh "docker-compose down"
                        sh "docker-compose up -d --build web"
                    }
                }
            }
        }
    
  • Click on Save and click on Build Now.

  • Now we can run the job twice or a couple of times without killing any existing container.

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #jenkins #jenkinsproject #cicd #declarativepipeline