How to Create a Jenkins Declarative Pipeline | Day 26 of 90 Days of DevOps

Ajit Fawade
5 min readAug 27, 2023

--

Hello, everyone!

I hope you are having a great day and learning something new every day. Today, I will show you how to create a Jenkins declarative pipeline.

What is a Pipeline?

A pipeline in Jenkins is a way of defining and executing a series of steps or stages that automate the process of building, testing, and deploying software. A pipeline can be written using a graphical user interface or a text-based script.

A pipeline can help you achieve the following benefits:

  • Improve the quality and reliability of your software by running automated tests and code analysis.
  • Reduce the time and effort required to deliver your software by automating repetitive tasks and integrating with various tools.
  • Increase the visibility and feedback of your software development process by providing a clear overview of the status and progress of your pipeline.
  • Enhance the collaboration and communication among your team members by enabling them to work on different parts of the pipeline.

What is a Declarative Pipeline?

A declarative pipeline is a type of pipeline that uses a simple and structured syntax to define your pipeline. A declarative pipeline consists of:

  • A pipeline block that wraps the entire pipeline definition.
  • An agent directive that specifies where to run the pipeline or each stage.
  • A stages block that contains one or more stage blocks that define the steps or actions to perform in each stage.
  • A steps block that contains one or more step directives that define the commands or instructions to execute in each step.
  • An optional post block that contains one or more condition blocks that define the actions to perform after the pipeline or each stage based on the outcome.

Here is an example of a declarative pipeline:

pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
post {
success {
echo 'Pipeline completed successfully.'
}
failure {
echo 'Pipeline failed.'
}
}
}

This pipeline defines three stages: Build, Test, and Deploy. Each stage has one step that prints a message using the echo step. The pipeline also defines two post conditions: success and failure. If the pipeline completes successfully, it prints “Pipeline completed successfully.” If the pipeline fails, it prints “Pipeline failed.”

What is a Scripted Pipeline?

A scripted pipeline is another type of pipeline that uses a Groovy-based syntax to define your pipeline. A scripted pipeline gives you more flexibility and control over your pipeline logic, but it also requires more programming skills and knowledge.

A scripted pipeline consists of:

  • A node block that specifies where to run the pipeline or each stage.
  • A stage block that defines the name and scope of each stage.
  • A steps block that contains one or more Groovy statements or expressions that define the commands or instructions to execute in each step.

Here is an example of a scripted pipeline:

node {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}

This pipeline defines three stages: Build, Test, and Deploy. Each stage has one step that prints a message using the echo step.

Why You Should Use a Declarative Pipeline?

There are several reasons why you should use a declarative pipeline over a scripted pipeline, such as:

  • A declarative pipeline has a simpler and more readable syntax than a scripted pipeline.
  • A declarative pipeline provides validation and syntax highlighting features that help you avoid errors and typos in your pipeline code.
  • A declarative pipeline allows you to use various directives and options that simplify and enhance your pipeline configuration, such as parameters, triggers, options, environment, tools, input, when, etc.
  • A declarative pipeline supports parallel execution of stages and steps, which can improve the performance and efficiency of your pipeline.
  • A declarative pipeline supports reusable code and modularization, which can help you avoid duplication and maintainability issues in your pipeline code.

How to Create a Simple Pipeline in Jenkins?

To create a simple pipeline in Jenkins that prints Hello World, follow these steps:

1. Go to your Jenkins dashboard and click on New Item.

2. Enter a name for your pipeline (e.g., Hello World) and select Pipeline. Then click OK.

3. On the configuration page, scroll down to the Pipeline section and select Pipeline script from the Definition drop-down menu.

4. In the Script text area, enter the following code:

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

This pipeline defines one stage named Hello which has one step that prints “Hello World” using the echo step.

5. Click Save to save your pipeline.

Now, go back to your Jenkins dashboard and click on your pipeline name (e.g., Hello World). Then click on Build Now to run your pipeline.

You will see a new build number appear under Build History with a blue ball indicating that the build is in progress. You can click on it to see the details of the build.

Click on Console Output to see the logs of the build. You should see something like this:

Started by user Ajit Fawade
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/Hello World
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] echo
Hello World
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

You can see that the pipeline was completed successfully and printed “Hello World”.

I hope this blog post helps you understand what a Jenkins declarative pipeline is, how it differs from a scripted pipeline, and how to create a simple pipeline that prints Hello World. In the next blog post, I will show you how to create more complex pipelines using different types of stages, steps, directives, and options.

If you have any questions or feedback, please feel free to contact me on:

Thank you for reading! 😊

--

--

No responses yet