Day 26 - Jenkins Declarative Pipeline

Day 26 - Jenkins Declarative Pipeline

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 several 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("Code") {
            steps {
                echo "Code Cloned"
            }
        }
         stage("Build") {
            steps {
                echo "Code Build"
            }
        }
         stage("Test") {
            steps {
                echo "Code Test"
            }
        }
         stage("Deploy") {
            steps {
                echo "Code Deploy"
            }
        }
    }

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

Step 1:

Click on New Item and give a name for your project (Pipeline) and click on ok

Step 2:

Provide the required description of the project that you are about to create and run

Step 3:

Scroll down to pipeline section, and select the definition as Pipeline script

Pipeline script can be from source code management also from GIT, we will see it in another session

Click on apply and save

  1. Pipeline: This is a mandatory block and the declarative pipeline will always start with this block.

  2. Agent: the agent is where the Jenkins build job should run. Here, we have selected agents as any.

  3. Stages/Stage: stages block consists of different stage blocks. There should be at least one stage block inside the stages.

    You can give any name to your stage block, here it is named Build

  4. Steps: Steps blocks consist of the actual operation which needs to be performed inside Jenkins. Here print the simple hello world by using the echo command.

Step 4:

Click on the Build Now option, and the pipeline will be running.

Step 5:

Console output