What is Jenkins Pipeline?

·

4 min read

Jenkins Pipeline:

Jenkins Pipeline is a collection of jobs or events that are interlinked together. It has two syntaxes: Scripted Pipeline and Declarative Pipeline.

JenkinsFile:

We can create Jenkins Pipeline by writing code or simply say in text format in a file that will be named Jenkinsfile. By writing Jenkinsfile we can create several stages (jobs) and after creating it we had to push it to the repository that will automatically start making the pipeline. Jenkinsfile is reusable we just had to push it to the repository so that everyone can see the code and customize it accordingly. We can also review the code of the pipeline.

So, in Jenkinsfile there are two types of syntax:

  • Scripted Pipeline

  • Declarative Pipeline

Scripted Pipeline is the first that was introduced in Jenkins while Declarative Pipeline is introduced recently in Jenkins. As scripted pipeline follows an imperative programming model and was based on DSL(Domain Specific Language) and written in a groovy language which is JVM (Java Virtual Machine) based language while the Declarative pipeline follows a declarative programming model that follows a simple syntactical approach.

The declarative programming model is easier to learn and easier to manage so that's why we should always use Declarative pipeline syntax as it is more flexible than Scripted Pipeline and makes the pipeline code easier to understand.

In Declarative Pipeline, directives are used in pipeline code. Directives are being generated from pipeline generator syntax.

But, Why we should use Pipeline?

  • By creating a pipeline from Jenkinsfile anyone can review the code and change it.

  • Suppose the Jenkins master machine is stopped while executing the pipeline the pipeline will resume again where it was stopped. So the Pipeline is robust.

  • With the help of a pipeline, you can create and run multiple jobs in a loop. So, the pipeline can be used in big projects.

Scripted Pipelines

Scripted pipelines were the first version of the “pipeline-as-code” principle. They were designed as a DSL build with Groovy and provide an outstanding level of power and flexibility. However, this also requires some basic knowledge of Groovy, which sometimes isn't desirable.

They have only two basic blocks: “node” and “stage”. A “node” block specifies the machine that executes a particular pipeline, whereas the “stage” blocks are used to group steps that, when taken together, represent a separate operation. The lack of additional rules and blocks makes these pipelines quite simple to understand:

node {

stage('Hello world') {

sh 'echo Hello World'

}

}

If the pipeline is executed successfully then it becomes green otherwise it turns out red. Now, the output of the pipeline is

Declarative Pipelines

Declarative pipelines are a more recent approach to the “pipeline-as-code” principle. They are quite easy to write and understand. The structure may seem to be a bit complex, but overall, it contains only a couple of basic sections. The “pipeline” block is the main block that contains the entire declaration of a pipeline. In this example, we'll consider only the “agent”, “stages”, and “steps” sections:

  • pipeline – contains the whole pipeline

    • agent – defines the machine that will handle this pipeline

    • stages – declares the stages of the pipeline

      • steps – small operations inside a particular stage

Let's take an example in which I am creating the Job and selecting Pipeline instead of Freestyle Project and also making Jenkinsfile.

Create a Jenkins Job Hello-World-Pipeline (New Item)

Now come to the pipeline section and under definition select Pipeline Script. Under this write a Declarative pipeline script that will echo the Hello World.

The pipeline script for Hello-World-Pipeline is:

pipeline {

agent any

stages {

stage('Hello') {

steps {

echo 'Hello World'

}

}

}

}

The Declarative pipeline will start from the pipeline word. Agent means that this pipeline will run on any node. In Stage, we give the name of the Stage (You can say stages are multiple Jobs) and under stages, steps are used to execute commands which means what should be run in this particular stage.

After this click on Save and Apply and then click on Build Now. Now your pipeline is started and executed successfully.

If the pipeline is executed successfully then it becomes green otherwise it turns out red.

Now, the output of the pipeline is: