Jenkins Project with Declarative Pipeline

For Jenkins Installation see my blog at below link:

react-django-to-do-app-using-jenkins

  1. Select 'Create a job'

  1. Select Pipeline

  1. Select GitHub project

    1. Select GitHub hook trigger for GITScm polling

      1. Pipeline script in Groovy Syntax

        pipeline {
            agent any

            stages{
                stage("Code"){
                    steps{
                        git url: "https://github.com/rushikesh-rawool10/node-todo-cicd.git", branch: "master"
                    }
                }
                stage("Build & Test"){
                    steps{
                        sh "docker build . -t node-app-test-new"
                    }
                }
                stage("Push to DockerHub"){
                    steps{
                        withCredentials([usernamePassword(credentialsId:"DockerHub",passwordVariable:"DockerHubPassword",usernameVariable:"DockerHubUser")]){
                            sh "docker login -u ${env.DockerHubUser} -p ${env.DockerHubPassword}"
                             sh "docker tag node-app-test-new ${env.DockerHubUser}/node-app-new:latest"
                             sh "docker push ${env.DockerHubUser}/node-app-new:latest" 
                        }
                    }
                }
                stage("Deploy"){
                    steps{
                        sh "docker-compose down && docker-compose up -d"
                    }
                }
            }
        }
  1. GitHub Code URL: https://github.com/rushikesh-rawool10/node-todo-cicd.git

Clone this code to the local server...

git clone https://github.com/rushikesh-rawool10/node-todo-cicd.git

  1. How to create credentials in Jenkins

i> Go to manage Jenkins

ii> In the Security section, select Credentials.

iii> Select global

iv> Add Credentials

v>Provide username, password, id

  1. First, we can check whether our code is running fine or not by manually using the delivery.

Before deploying your code make sure of the following points:

  1. Docker and docker-compose should be installed on your machine.

  2. Your Jenkins user should be added to the docker group.

  3. port 8000 (the port on which app will run)should be open from the ec2 instance.

Jenkins Pipeline using a script is running fine...

  1. To make it a fully Deployment project we can use 'GitHub hook trigger for GITScm polling' & 'pipeline script from scm'

  2. How to set the GitHub hook trigger

    Go to the current repository settings

    go to Webhooks

    Add webhook

    Provide Payload URL like below:

    Webhook is now activated...

    As soon we make any changes to the GitHub code, the pipeline will triggered automatically, which is Jenkins Deployment.

  3. Here we go...!

    Node app using Jenkins declarative Pipeline is Ready...!!!

0
Subscribe to my newsletter

Read articles from Rushikesh Rawool directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rushikesh Rawool
Rushikesh Rawool

Passionate ORACLE DATABASE ADMINISTRATOR | DevOps Enthusiast with a strong background in cloud architecture and solutions engineering.