Day 26: An Introduction to Jenkins Declarative Pipelines

Day 26: An Introduction to Jenkins Declarative Pipelines

ยท

3 min read

What is Pipeline?

  • A pipeline refers to a series of automated steps and actions that define the continuous integration and continuous delivery (CI/CD) process for software development.

  • Jenkins pipelines are often defined within the version control system (Git) making it easy to manage changes to the pipeline itself.

  • A pipeline can execute multiple stages in parallel, improving efficiency.

Declarative

  • Declarative is a simplified and structured way to define Jenkins pipelines using a Domain-specific language (DSL).

  • Declarative is a more recent and advanced implementation of a pipeline as a code.

  • Declarative pipelines are defined using declarative blocks and include pre-defined sections for stages, steps and other pipeline elements.

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 we should have a pipeline?

The definition of a Jenkins Pipeline is written into a text file (called a Jenkinsfile) which in turn can be committed to a projectโ€™s source control repository.
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.

Creating a Jenkinsfile and committing it to source control provides several immediate benefits:

  • Automatically creates a Pipeline build process for all branches and pull requests.

  • Code review/iteration on the Pipeline (along with the remaining source code).


Pipeline Syntax

A pipeline is defined with a specific syntax known as groovy syntax. There's a proper way or method to write a pipeline. Out of the box method of the pipeline will cause an error during the deployment of an application.

Here's the proper syntax to write a pipeline:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                //
            }
        }
        stage('Test') {
            steps {
                //
            }
        }
        stage('Deploy') {
            steps {
                //
            }
        }
    }
}

Task 01

Create a New Job, this time select Pipeline instead of Freestyle Project.

Complete the example using the Declarative pipeline

steps:

  • Set up your Jenkins server as you did in the previous day's tasks.

  • Click on Create a new job on your Jenkins server.

  • Write the name of your job and select the pipeline instead of the freestyle project.

  • Write the description of your pipeline and select the GitHub project if it's from GitHub.

  • Go to the pipeline section and write the pipeline for your project. Write a simple pipeline for your understanding.

  • Then click on Save and Build Now to run your pipeline. The pipeline will be shown on your screen in real time.

  • This means that your pipeline and its groovy syntax running successfully. If any stage fails, the whole pipeline will fail from the failed step.

  • It's enough for today and your basic understanding of the pipeline. Practice to write the pipeline to make it perfect.


<That's all for today. Hope you like it. FOLLOW to join me in the journey of DevOps>

ย