In this blog, I will discuss the Jenkins Pipeline and the types of pipelines: Declarative and Scripted Pipelines.
Let’s start!
1. Jenkins Pipeline
According to the official documentation of Jenkins, “ Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. “
Pipelines allow for defining a series of steps that will be executed whenever the change of code like building, testing, and deploying takes place.
Pipelines enable modeling simple-to-complex delivery Pipelines as a Code. The definition of a Jenkins Pipeline is written into a text file called Jenkinsfile.
Jenkins Pipelines are written in a domain-specific language (DSL) called Declarative Pipeline. This DSL makes it easy to define complex pipelines without writing a lot of code.
2. Why Pipelines?
They are easy to use.
They are scalable.
They are flexible.
They are secure.
Automate the software development process.
Improve the quality of your code.
Increase the speed of your deployments.
Improve collaboration.
3. Pipeline Concepts
3.1 Pipeline
A Pipeline is a user-defined model of a CD pipeline. This is a vital part of Declarative Pipeline Syntax.
3.2 Node
A node is a machine part of the Jenkins environment and capable of executing a Pipeline. This is a key part of Scripted Pipeline Syntax.
3.3 Stage
A stage in a pipeline is a logical grouping of steps that are executed together. Stages are typically used to group related tasks, such as building, testing, and deploying code.
3.4 Step
A step in a pipeline is a single action that is executed as part of a stage. Steps are typically used to perform tasks such as building, testing, and deploying code.
Now let us know the types of Pipelines. There are two types: Declarative Pipelines and Scripted Pipelines.
4. Declarative Pipeline
Declarative Pipeline is based on a set of declarative statements that define the structure and behavior of your pipeline. These statements are grouped into sections, which are executed in order.
Declarative Pipeline is a new syntax for writing Jenkins Pipelines. It is designed to be more concise and easier to read than the Scripted Pipeline syntax.
4.1 Syntax
pipeline {
agent any #1
stages {
stage('Build') { #2
steps {
//#3
}
}
stage('Test') { #4
steps {
//#5
}
}
stage('Deploy') { #6
steps {
//#7
}
}
}
}
#1 Execute this Pipeline or any of its stages, on any available agent.
#2 Defines the “Build” stage.
#3 Perform some steps related to the “Build” stage.
#4 Defines the “Test” stage.
#5 Perform some steps related to the “Test” stage.
#6 Defines the “Deploy” stage.
#7 Perform some steps related to the “Deploy” stage.
5. Scripted Pipeline
Scripted Pipeline is based on a Groovy script that defines the structure and behavior of the pipeline. The script is executed in order, and it can contain a variety of steps, including building, testing, and deploying code.
Scripted Pipeline is a syntax for writing Jenkins Pipelines. It is the original syntax for Pipelines, and it is still supported in Jenkins.
5.1 Syntax
node { #1
stage('Build') { #2
//#3
}
stage('Test') { #4
// #5
}
stage('Deploy') { #6
// #7
}
}
#1 Execute this Pipeline or any of its stages, on any available agent.
#2 Defines the “Build” stage. ‘stage’ blocks are optional in Scripted Pipeline syntax.
#3 Perform some steps related to the “Build” stage.
#4 Defines the “Test” stage.
#5 Perform some steps related to the “Test” stage.
#6 Defines the “Deploy” stage.
#7 Perform some steps related to the “Deploy” stage.
6. Difference between Declarative and Scripted Pipelines
7. Jenkinsfile
A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. It is typically stored in the root directory of the project that it is used to automate.
The following is an example of a Jenkinsfile:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install'
}
}
stage('Deploy') {
steps {
sh 'scp -r target/*.jar localhost:/opt/app'
}
}
}
}
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.
Now let us complete tasks as we now know the basics of the Pipelines.