Skip to main content

Command Palette

Search for a command to run...

Jenkins Declarative Pipeline

Published
2 min read
Jenkins Declarative Pipeline
S

I'm Suraj Barik Aspiring DevOps Engineer with Hands-on experience in Automating,Shell Scripting, Supporting in AWS, management, CI/CD, and DevOps processes.

Some terms for your Knowledge

What is a 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-1:

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

1)To create a Declarative pipeline in Jenkins, go to Jenkins UI and click on New Item

2)Enter the pipeline name "Hello_word_example" select Pipeline, and then click on ok

3)Add a description of this project

4)Configure Pipeline: In the pipeline configuration section: Write declarative pipeline code

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 that needs to be performed inside Jenkins. In the above example, we are printing “Hello World“.

6)Build the project. You can manually build the project by clicking on the "Build Now" link on the project's main page

5)Save pipeline configurations and run the pipeline job by clicking on "Build Now.

6)See the console output

As you can see from the above output, the Pipeline run successfully and printed Hello World ALL !!!.

Thank you for reading my blog! Happy Learning!!!😊

More from this blog

Suraj's Spark ☁️

32 posts