Day 27 of #90DaysOfDevOps || Jenkins Declarative Pipeline with Docker

Day 27 of #90DaysOfDevOps || Jenkins Declarative Pipeline with Docker

·

2 min read

Task:

  • Create declarative docker build pipeline

  • Also in same pipeline push docker image to public repository

  • Use github integrations to perform push triggers to build the job


We will take this project. This is a news application frontend project.

  • First start your instance and install jenkins. To get complete procedure to install jenkins visit this link

  • Now start jenkins server. And create new item and choose pipeline and put name to project.

  • Now put details description, github project URL and in pipeline choose SCM file and provide git url to it and credentails created using git. For detailed explanation to create and setup credentails visit here. Also set branch to build in this there is only one branch main. And in script path set filename from where pipeline will run in this case we are using Jenkinsile as name. We will save it.

  • Now we will build and test the file. Note you need Jenkinsfile in the code already otherwise it will show error.

  • Now we can see integration is successfull now we will put Docker file to our code and also write jenkinsfile. To know about Dockerfile visi here.

  • Docker file

      FROM node:14
    
      WORKDIR /app
    
      COPY package.json .
    
      RUN npm install --legacy-peer-deps
    
      COPY . .
    
      CMD ["npm","start"]
    
  • Now for jenkinsfile, we will provide build of docker image and start docker container also. Also going to write post build, so that we know about the status

      pipeline{
          agent any
          stages{
              stage("Docker build "){
                  steps{
                      sh 'sudo docker build . -t news/app'
                  }
              }
              stage("Docker run "){
                  steps{
                      sh 'sudo docker container run -d -p 3000:3000 news/app'
                  }
              }
          }
          post{
              always{
                  echo "========always========"
              }
              success{
                  echo "========pipeline executed successfully ========"
              }
              failure{
                  echo "========pipeline execution failed========"
              }
          }
      }
    
  • Now we will push this code to github. And set build in jenkins master. Also we can set webhook for push trigger for complete explanation to set webhook visit here

  • Now we can check our public ip with port 3000. And our application will up and running.

End Of Post


Reach me on:

  1. Github-> github.com/Hrmn97

  2. Twitter -> twitter.com/Harman9765

  3. LinkedIn -> linkedin.com/in/chetan-harman-56310424a

  4. Website -> devhrmn.netlify.app