Jenkins Declarative Pipeline

Jenkins Declarative Pipeline

·

4 min read

What is a Pipeline

A pipeline is a collection of steps or jobs interlinked in a sequence.
A pipeline in Jenkins is a series of steps or jobs that are connected together in a specific order. Each step in the pipeline performs a specific function, such as compiling code, running tests, or deploying the code to a production environment. The pipeline is a way of automating the process of building, testing, and deploying software, and it allows developers to define a repeatable process that can be run automatically whenever changes are made to the code.

Declarative:

Declarative is a more recent and advanced implementation of a pipeline as a code.
A declarative pipeline is a way of defining a pipeline using a high-level, abstract syntax. In a declarative pipeline, the developer defines what needs to be done, but not how it should be done. The pipeline software takes care of the details, figuring out the best way to execute the steps in the pipeline. Declarative pipelines are often represented using a domain-specific language (DSL) that is designed specifically for defining pipelines.
Declarative pipelines are useful because they allow developers to focus on what needs to be done, rather than how it should be done. This makes it easier to change or modify the pipeline, because the developer only needs to modify the high-level definition of the pipeline, rather than the low-level implementation details.

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.
A scripted pipeline is a way of defining a pipeline using a scripting language, such as Groovy or Python. In a scripted pipeline, the developer defines not only what needs to be done, but also how it should be done. The developer writes a script that specifies each step in the pipeline and how it should be executed.
Scripted pipelines are useful because they allow developers to have fine-grained control over the execution of the pipeline. They can specify exactly how each step should be executed, which can be useful in complex scenarios where the default behaviour of the pipeline software is not sufficient.
However, scripted pipelines can also be more time-consuming and error-prone than declarative pipelines. The developer needs to worry about the low-level details of how each step is executed, which can be tedious and error-prone. Additionally, scripted pipelines can be less flexible than declarative pipelines, because changing the pipeline requires modifying the script, which can be time-consuming and difficult.

Why you should have a Pipeline:

Having a pipeline in Jenkins is beneficial for several reasons:

  • Automation: A pipeline automates the process of building, testing, and deploying code, which saves time and reduces the chance of human error.

  • Consistency: A pipeline ensures that the same steps are executed every time a change is made to the code, which helps maintain consistency and repeatability.

  • Version Control: By defining the pipeline in a text file (called a Jenkinsfile), you can version and review the pipeline like any other code.

  • Collaboration: A pipeline can be shared among team members, which facilitates collaboration and communication.

  • Flexibility: A pipeline can be easily modified or extended to accommodate changing requirements or new technologies.

  • The definition of a Jenkins Pipeline:

A Jenkins pipeline is defined in a text file called a Jenkinsfile. The Jenkinsfile contains a series of steps that are executed in a specific order, such as building the code, running tests, and deploying the code to a production environment.

The Jenkinsfile is written in a domain-specific language (DSL) that is specific to Jenkins. The DSL allows you to define the steps in the pipeline and their order, as well as any dependencies between steps.

By defining the pipeline in a text file, you can easily version and review the pipeline like any other code. This is the foundation of "Pipeline-as-code", which treats the continuous delivery (CD) pipeline as a part of the application to be versioned and reviewed like any other code.

Having the pipeline defined in a text file also makes it easy to share the pipeline among team members and to collaborate on the development process. Additionally, the text file can be easily modified or extended to accommodate changing requirements or new technologies, which makes the pipeline more flexible and adaptable.


Task-01

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

  • Follow the Official Jenkins Hello world example

Click on Build Now

  • Complete the example using the Declarative pipeline


Thankyou for reading until here. In the next one we will integrate Docker into Jenkins declarative pipeline. Happy Coding!