Skip to main content

Command Palette

Search for a command to run...

Day 26: Jenkins Declarative Pipeline

Published
2 min read
Day 26: Jenkins Declarative Pipeline
S

Hello, I’m Som, a DevOps Engineer passionate about streamlining operations through automation, continuous integration, and deployment. I am deeply passionate about exploring new technologies and continuously expanding my knowledge in the ever-evolving world of IT.

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: Jenkins "Hello World" Declarative Pipeline

  • Create a New Job, this time select Pipeline instead of Freestyle Project.
  1. Click on "New Item" to create a new Jenkins job.

  2. Enter Job Details:

    • Provide a name for the job (e.g., "HelloWorldPipeline").

    • Choose "Pipeline" as the job type.

Configure Pipeline: In the pipeline configuration section:

  • Choose "Pipeline script" from the "Definition" drop-down.

  • In the "Pipeline" section, you can directly enter the Declarative pipeline script.

Add Declarative Pipeline Script: Use the following example as your Declarative pipeline script:

 pipeline {
    agent any
    stages {
        stage('Hello') {
            steps {
                echo 'Hello World!'
            }
        }
    }
}

Save and Run:

  • Click on "Save" to save your pipeline configuration.

  • Run the pipeline job by clicking on "Build Now."

View Console Output: After the build completes, you can click on the build number to view the console output. You'll see the output "Hello World!" printed as part of the pipeline execution.

"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/ for the latest updates and discussions.

More from this blog

Som Pandey's blog

92 posts

Hello, I’m Som, a DevOps Engineer passionate about streamlining operations through automation, continuous integration, and deployment.