Jenkins Declarative Pipeline with Docker

Jenkins Declarative Pipeline with Docker

Use 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.

Task 1:

  1. Create a docker-integrated Jenkins declarative pipeline

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

  3. 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

  • Step1: Create a new pipeline project and named it node declarative

    **Step2:**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: 'you git url' , branch: 'master' 
              }
          }
          stage('Build') { 
              steps {
                  sh 'docker build . -t node_todo_app:latest' 
              }
          }
          stage('Test') { 
              steps {
                  echo "Testing" 
              }
          }
          stage('Deploy') { 
              steps {
                  sh "docker run -p 8000:8000 -d node_todo_app:latest"
              }
          }
      }
  }

**Step3:**Click on Save and Build the application.

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

**Step5:**Now you check the multi-stage view by clicking on the “Full Stage View” on the project main page.

Task 2:

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

  • You won't face errors, you can Follow this documentation

  • Complete your previous projects using this Declarative pipeline approach

    **Step1:**Now we have to add a docker image in the Build stage.

        pipeline {
            agent any 
            stages {
                stage('Clone Code') { 
                    steps {
                        git url: 'yourl git url' , branch: 'master' 
                    }
                }
                stage('Build') { 
                    agent {
                        docker {
                            image 'prabirmahatha/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"
                    }
                }
            }
        }
    

    **Step2:**Click on Save and then click on Build Now.

    For another Jenkins project.

    Follow me on LinkedIn to see interesting posts like this : )

    linkedin.com/in/prabir-kumar-mahatha-6a0025..

    Visit my git hub profile: github.com/PrabirKumarMahatha