Declarative Pipeline with docker

1 min read

Cover Image for Declarative Pipeline with docker

Task 1

  • Create a docker-integrated pipeline with docker

    1. Docker builds: use sh 'docker build. -t <tag> ' to run the docker build command.

    2. Docker run: use sh 'docker run -d <image>' in your pipeline

      to stage block to run the container.

  • Now, Steps to do it.Let's Start

    1. create a pipeline

    2. use sh syntax in stage block

        pipeline{
             agent any
      
             stages{
                 stage("Code Clone"){
                     steps{
                         git branch : 'master', url:'https://github.com/sri766/node-todo-cicd.git'
                     }
                 }
             stage("Code Build"){
                     steps{
                         sh 'docker build . -t django-node-todo:latest ' 
                     }
                 }
             stage("Code Testing"){
                     steps{
                       echo 'testing'  
                     }
                 }
             stage("code Deploy"){
                     steps {
                         sh 'docker run -d -p 8000:8000 django-node-todo:latest'
                     }
                 }
             }
         }
      
    3. save and run the pipeline.

      console output -

app Is running at port 8000.

Task 2

we can create declarative pipeline with docker by using groovy syntax.

  1. replacing deploy code with docker compose up and down command.

      stage("code Deploy"){
                   steps {
                       sh 'docker-compose down'
                       sh 'docker-compose up'
                   }
               }
    

    #possible error may come:

    1. permission denied to run docker commands: just give sudo permission to jenkins by adding sudo to sh commands.

    2. webhooks 403 error: it can be managed. go to manage userProfile-> configure ->generateToken

      copy it and add it to secret while adding webhooks to github.