#Day26 and #Day27 Task: Jenkins Declarative Pipeline

#Day26 and #Day27 Task: Jenkins Declarative Pipeline

One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins

Some terms for your Knowledge

What is Pipeline - A pipeline is a collection of steps or jobs interlinked in a sequence.

Declarative: Declarative is a more recent and advanced implementation of a pipeline as a code.

Scripted: Scripted was the first and most traditional implementation of the pipeline as a code in Jenkins. It was designed as a general-purpose DSL (Domain Specific Language) built with Groovy.

Why you should have a Pipeline

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a project’s source control repository.
This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.

Creating a Jenkinsfile and committing it to source control provides a number of immediate benefits:

  • Automatically creates a Pipeline build process for all branches and pull requests.

  • Code review/iteration on the Pipeline (along with the remaining source code).

Pipeline syntax

pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

Pre-steps

First go to

Jenkins dashboard

Select Manage Jenkins

Under security, select Manage credentials

In System global add credentials for Docker so that we can push and pull images from docker-hub by logging in.

Also add Webhook to GitHub repository

Go to the repository>Settings>Webhook(left pane)

Steps to build pipeline

Go to New item on dashboard

Give a name (My-First-Pipeline)

Give description

Check Github Project and enter url

Under build triggers, select GitHub hook trigger for GITScm polling

Enter pipeline script as follows -

Now go to the pipeline from dashboard and run build now

It will show build stepwise

Webhook

Suppose you make some changes in your Github code, You do not need to build again manually.

Using webhook, a build will immediately be triggered as soon as a push is made on the master branch.

Now lets make some changes on the Github code.

As soon as I make some changes, a build will automatically be triggered as shown below.