Day 26 Task: Jenkins Declarative Pipeline

Day 26 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 {
                // 
            }
        }
    }
}

Task-01

  • Create a New Job, this time select Pipeline instead of Freestyle Project.

  • Follow the Official Jenkins Hello world example

  • Complete the example using the Declarative pipeline

  • In case of any issues feel free to post on any Groups, Discord or Telegram

Prerequisite for this task 01

Please follow the Day 22 task blog. In this blog, you will see how to connect Jenkins to a server.

Step 1: Instead of selecting Freestyle project now you should select pipeline like shown below

now click on Ok

Step 2: Now write your description and run these commands on the pipeline area

pipeline {
    agent any
    stages {
        stage("code") {
            steps {
                echo "hello world"
            }
        }
        stage("build") {
            steps {
                echo "building....."
            }
        }
    }
}

and press save

Step 3: Click on build now

now click on running which is shown in below

Step 4: Click on console output

here you can see we have successfully completed our task 01


Thank you for reading this blog and if any queries or if any corrections to be done in this blog please let me know.

contact us in Linkedin ,Twitter or email-id