Day 26 Task: Jenkins Declarative Pipeline
One of the most important parts of your DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins. So let’s build Declarative pipeline today.
What is Pipeline?
A pipeline is a collection of steps or jobs interlinked in a sequence. A pipeline is an automated set of steps that software goes through, from development to deployment. It includes stages like code repository, build, testing, deployment, and monitoring. Pipelines help streamline software delivery, improve efficiency, and enable continuous integration and continuous deliver.
What is Declarative pipeline?
Declarative pipeline is a concept in Jenkins, a popular open-source automation server used for building and deploying software. It is a domain-specific language (DSL) that provides a simplified and structured way to define Jenkins pipelines.
Declarative is a more recent and advanced implementation of a pipeline as a code. In Declarative pipeline, you define the pipeline using a declarative syntax, focusing on the desired outcome rather than the specific steps to achieve it.
What is 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.
Why you should have a Pipeline?
1. Automation: A pipeline automates the steps involved in building, testing, and deploying software. This automation reduces manual errors, saves time, and ensures consistency in the software delivery process.
2. Efficiency: By automating repetitive tasks, a pipeline enables faster and more frequent software releases. It eliminates manual intervention and enables continuous integration and continuous delivery (CI/CD), allowing teams to deliver new features and bug fixes more rapidly.
3. Consistency: A pipeline enforces a standardized process for software delivery. Every change goes through the same set of stages, ensuring consistent build and deployment practices across the development team.
4. Quality Assurance: Pipelines facilitate automated testing, including unit tests, integration tests, and other forms of validation. These tests help catch bugs and issues early in the development cycle, ensuring higher quality software.
5. Collaboration: A pipeline encourages collaboration between development, testing, and operations teams. It provides visibility into the software delivery process, making it easier to track changes, identify issues, and collaborate on problem-solving.
What is Jenkins File?
A Jenkinsfile is a text file that serves as the definition of a Jenkins pipeline.A Jenkinsfile is typically written in either Declarative or Scripted syntax and contains the instructions and configuration needed to define and execute a pipeline. The Jenkins file is typically stored alongside the source code of a project in a version control system, ensuring that the pipeline definition is versioned and easily accessible.
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.
pipeline {
agent any
stages {
stage(‘Build’) {
steps {
}
}
stage(‘Test’) {
steps {
}
}
stage(‘Deploy’) {
steps {
}
}
}
}
Tasks -
Task 1: Create a Jenkins Pipeline Project using declarative pipeline syntax to print “Hello World”
1) Create a Jenkins Pipeline named “HelloWorld”
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.
Above 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 Task1, but using a Jenkins file.
1) Create a file named Jenkins file 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.