Table of contents
In this blog, I will discuss the Jenkins Pipeline and the types of pipelines: Declarative and Scripted Pipelines. Also, as a starter, I will create an example Pipeline Job.
Let's start!
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.
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.
Pipeline Concepts
Pipeline
A Pipeline is a user-defined model of a CD pipeline. This is a vital part of Declarative Pipeline Syntax.
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.
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.
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.
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.
Syntax
pipeline {
agent any #1
stages {
stage('Build') { #2
steps {
//#3
}
}
stage('Test') { #4
steps {
//#5
}
}
stage('Deploy') { #6
steps {
//#7
}
}
}
}
#1 |
|
#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. |
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.
Syntax
node { #1
stage('Build') { #2
//#3
}
stage('Test') { #4
// #5
}
stage('Deploy') { #6
// #7
}
}
#1 |
|
#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. |
Difference between Declarative and Scripted Pipelines
Feature | Declarative Pipelines | Scripted Pipelines |
Syntax | YAML | Groovy |
Ease of use | Easy to learn and use | More complex to learn |
Scalability | Scalable | Scalable |
Flexibility | Flexible | More flexible |
Security | Secure | Less secure |
Power | Less powerful | More powerful |
Familiarity | Less familiar | More familiar if you know Groovy |
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.
Tasks
Task 1: Create a Jenkins Pipeline Project using declarative pipeline syntax to print "Hello World"
1) Create a Jenkins Pipeline named "HelloWorld Example1"
2) Go to the Dashboard > Configure > Pipeline > Select Definition as "Pipeline Script" > And enter the following command.
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
3) Click on Save and Build the Pipeline.
4) In the Pipeline Dashboard, you can see the Stage View as well. Our job is successful.
5) This is the Console Output. It says SUCCESS. Also, you can observer that the "HELLO WORLD" is printed.
Task 2: Perform the same task which you did in Task 1, but using a Jenkinsfile.
1) Create a file named Jenkinsfile in your repository. Write down the below contents into the file and commit the changes. Copy the repository link for further usage.
2) Now in Jenkins, create a new Pipeline name "HelloWorldExample2"
3) Go to Dashboard > Configuration > Pipeline > Select Definition "Pipeline script from SCM" > Select "Git" for SCM > Paste the repository URL which you previously copied and paste in the Repositories Section > Select the branch name where the Jenkinsfile is located > Click on Save
4) Now build the pipeline. The stage view appears. Our build is successful.
When we see the Console Output, we can see that Jenkinsfile is being obtained from GitHub. And then the build progressed.
And we have successfully built the Pipeline Project for the "Hello-World" example.
In this blog, I have discussed Jenkins Pipelines, Jenkinsfile, types of Jenkins pipelines, and an example of how to run a Jenkins Pipeline job. If you have any questions or would like to share your experiences, feel free to leave a comment below. Don't forget to read my blogs and connect with me on LinkedIn and let's have a conversation.
To help me improve my blog and correct my mistakes, I am available on LinkedIn as Sneha K S. Do reach me and I am open to suggestions and corrections.
#Day26 #90daysofdevops