Pipeline in Jenkins is a group of jobs (or events) that are interlinked in a particular sequence. Jenkins Pipeline is a set or suite of plugins that provides support for implementation and integration of Continuous Delivery pipelines into Jenkins.
The Pipeline also provides a set of tools that are useful for modeling simple as well as complex delivery pipelines ‘as code’ through ‘Pipeline Domain-Specific Language (DSL)’ syntax.
Every job in the Jenkins pipeline has some dependency on one or more events. Continuous delivery pipeline in Jenkins consists of four states – Build, Deploy, Test, and Release. Each of these states consist of events that execute in a sequence.
What Is Jenkinsfile?
Now that you understand what is Jenkins pipeline, we can dive deeper into the concept. The entire definition of a Jenkins Pipeline is written into a text file called Jenkinsfile. It contains the steps required for running a Jenkins Pipeline. ‘Pipeline as code’ can be implemented using Jenkinsfile and Domain Specific Language (DSL) is used for defining the same.
Jenkinsfile can also be committed to the source control repository of the project. With Jenkinsfile, the CD Pipeline is also treated as a part of the application that is versioned, committed, and reviewed like any other piece of code.
Some of the major benefits of Jenkinsfile are:
Single Jenkinsfile can be used for creating a Pipeline build process for all the branches and executing pull requests.
The implementation in a Pipeline can be reviewed like normal source code.
Audit trail of the Pipeline.
Singular source for the Pipeline can be viewed as well as edited by multiple members associated with the project.
Though Pipeline can be defined either in web UI or with a Jenkinsfile, it is recommended to define what is Jenkins Pipeline in a Jenkinsfile and maintain the same in a source control repository.
Syntax For Defining A Jenkinsfile
To define what is Jenkins pipeline, Jenkinsfile can be written using the following types of Syntax:
a. Declarative
Declarative Pipeline is a recent feature of Jenkins Pipeline that makes reading and writing the Pipeline code a lot easier. Unlike Scripted Syntax with Pipelines, Declarative Syntax helps in controlling different aspects of the Pipeline in a simplistic manner.
Advantages:
Modern way of developing Pipelines and favored by the company behind Jenkins
Structured. Simpler syntax
Easy to read
Steep learning curve
Best integration with Blue Ocean interface
Allows restarting from specific stage
Disadvantages:
Less suitable for Pipelines with complex logic
Lack of compatibility with old plugins
Restrictive syntax
b. Scripted
Scripted Pipeline is a more traditional way of writing a Jenkins Pipeline as code. It uses traditional Groovy-based syntax. The Web UI of Jenkins is normally used for writing the Scripted Pipeline in a Jenkinsfile.
Advantages:
Flexibility powered by Groovy language
Extensibility
Allows restarting from specific Stage
Disadvantages:
Shallow learning curve
Less readability harder to maintain
Harder to maintain
No integration with Blue Ocean
Restart from Stage option unavailable
Run the pipeline
Now lets configure the Jenkins pipeline. Following are the steps to create Jenkins Pipeline.
Step 1:
Select “New Item” from the dashboard of the Jenkins instance
Step 2:
Name the item and select pipeline as the style of the job item.
Step 3:
Scroll down and select the definition of the Pipeline. Here we get two options.
One is the Pipeline script from SCM which is for Declarative Pipeline
Another is the Pipeline script which is for Scripted Pipeline that we will write directly on UI.
If it is the Pipeline Script, which will be written directly on UI, Need to write it on Script place (Scripted pipeline)
If we are selecting the Pipeline script from SCM definition, then we need to select the Git > SCM Repository URL > Credentials. (Declarative Pipeline).
Declarative Pipeline from SCM
In the Declarative pipeline, We need to select the repository of the Jenkinsfile, Credentials, Branch and the path of the Jenkinsfile as shown in the above screenshot.
Important Concepts Of Jenkins Pipeline
Moving on with what is Jenkins pipeline, here are some basic concepts that need to be well-understood if you are planning to use the Jenkins Pipeline:
a. Pipeline
The Pipeline consists of a set of instructions written as code. It defines the entire build process, which ideally consists of different stages for building, testing, and delivering the application.
b. Node
A node is a machine that is a part of the Jenkins environment. The Jenkins Pipeline executes on a node block that is usually a part of the Scripted Pipeline syntax.
c. Stage
Stage in a Jenkins Pipeline consists of a unique subset of tasks such as Build, Test, Deploy, etc. The Stage block is used by many plugins for providing the visualization of Jenkins status (and progress).
d. Step
Step is a single task that tells Jenkins what exactly needs to be done. For example, setting an environment variable can be done in a step, executing a build command can also be a step. On the whole, a Jenkins Pipeline constitutes a series of steps.
Advantages Of Using Jenkins Pipeline
Jenkins Pipeline is instrumental in adding a rich set of automation tools onto Jenkins. Hence, Jenkins can be used for simple continuous integration as well as for comprehensive CD pipelines.
These are some of the primary reasons for using the Jenkins Pipeline:
As the Jenkins Pipeline is implemented as code, it can be checked in the source control repository. Teams can view, edit, as well as iterate upon the delivery pipeline.
Jenkins Pipelines are robust. A Pipeline is automatically resumed in case the server witnesses an unplanned restart.
Pipeline process can pause and wait for input for any input from the user.
Pipelines are versatile as they can be used for realizing complex CD requirements, including performing work in parallel.
Jenkins Pipelines are extensible by plugin developers and users with Pipeline Shared Libraries.
The Tech Platform
Comentarios