Jenkins Declarative Pipeline

Jenkins Declarative Pipeline

Day26

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 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('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

Task

  1. Login to your Jenkins instance and click on the "New Item" link on the left-hand side of the dashboard.

  2. Enter a name for your new Pipeline job and select "Pipeline" as the job type.

    Click on "OK" to create the job.

  3. On the next screen, you will see the configuration page for your new Pipeline job.

    enter a description, and Scroll down to the "Pipeline" section, which should be at the bottom of the page.

  4. In the "Pipeline" section, select "Pipeline script"

  5. Once you have entered your Pipeline script, click on "Save" to save your job configuration.

    Pipeline: is Declarative Pipeline-specific syntax that defines a "block" containing all content and instructions for executing the entire Pipeline.

    Agent: Agent signifies where the Jenkins build job should run. In this case, we have selected agents as any.

    Stages: stages block consists of different executable stage blocks. At least one stage block is mandatory inside the stages block. Here we have names stage as “Hello”

    Steps: Steps blocks consist of the actual operation which needs to be performed inside Jenkins. In the above example, we are printing “Hello World“.

  6. Created a new Pipeline job in Jenkins. Now run your Pipeline by clicking on the "Build Now" button on the job's dashboard page.

  7. After a build is completed, you can view the console output by clicking on the "Console Output" link on the build page.

    You can see from the above output, the Pipeline run successfully and printed Hello World.

Thank you for Reading ........❤❤❤