Day 26 Task: Jenkins Declarative Pipeline

Day 26 Task: Jenkins Declarative Pipeline


Overview:
Jenkins Pipelines are pivotal in DevOps, automating software delivery. Declarative and Scripted syntaxes offer different approaches – Declarative for simplicity, Scripted for flexibility.

One of the most important parts of our DevOps and CICD journey is a Declarative Pipeline Syntax of Jenkins

What is Pipeline — A pipeline refers to a sequence of connected steps or processes executed in a specific order to transform input data into output data.

Pipelines are an effective way to automate and streamline complex workflows, reducing the likelihood of errors and improving overall efficiency. They can also help to ensure that processing steps are consistent and reproducible.

Declarative: Declarative Pipeline Syntax is a simpler and more opinionated syntax for defining pipelines, where the focus is on declaring what the pipeline should do, rather than how it should do it. The Declarative Pipeline Syntax is designed to make it easier to write and understand pipelines, with pre-defined stages, steps, and directives.

Scripted: The Scripted Pipeline Syntax is a more flexible and powerful way to define pipelines, compared to the Declarative Pipeline Syntax. It is a Groovy-based syntax that provides a programmatic way of defining pipelines, with more control over the pipeline flow and more flexibility in defining the pipeline stages and steps.

Why you 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

There are several reasons why having a Pipeline is important for DevOps and software development:

1. Standardization: A Pipeline and Jenkinsfile provide a standardized way to define and execute the build, test, and deployment process. This ensures that the process is consistent and repeatable, reducing the risk of errors and making it easier to troubleshoot issues.

2. Automation: A Pipeline and Jenkinsfile allow for the automation of the build, test, and deployment process, reducing the need for manual intervention and saving time and effort.

3. Version Control: A Jenkinsfile can be version-controlled along with the source code, making it easier to track changes and maintain the pipeline over time.

4. Portability: A Pipeline and Jenkinsfile are platform-independent, making it possible to execute the same pipeline on different systems and platforms. This portability ensures that the pipeline can be easily moved to different environments, such as test and production environments.

5. Integration: A Pipeline and Jenkinsfile can be integrated with other tools and systems, such as Git, Docker, and Kubernetes, making it possible to streamline the entire software development and deployment process.

6. Collaboration: A Pipeline and Jenkinsfile can be shared among different teams and stakeholders, making it easier to collaborate and communicate during the software development process.

Overall, having a Pipeline and Jenkinsfile is an important part of a modern DevOps and software development process. They provide standardization, automation, version control, portability, integration, and collaboration, all of which are essential for delivering high-quality software efficiently and reliably.

Pipeline syntax

The Pipeline syntax in Jenkins is defined using two different syntaxes: Declarative and Scripted. Here is an overview of the syntax for each:

1. Declarative Pipeline Syntax: The Declarative Pipeline Syntax is a more recent addition to Jenkins, and it is designed to provide a simpler, more structured syntax for defining pipelines. It uses a YAML-based syntax, and each pipeline is defined using the pipeline block.

Here is an example of a simple Declarative Pipeline:

2. Scripted Pipeline Syntax: The Scripted Pipeline Syntax is a more flexible syntax for defining pipelines. It is based on the Groovy programming language and provides more control and flexibility over the pipeline flow and stages.

Here is an example of a simple Scripted Pipeline:

Task-01

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

  • Follow the Official Jenkins Hello world example

  1. Login to the Jenkins server and click on “New Item” to create a job.

    2. Enter the desired name, select “Pipeline” and then click on Ok.

    3. In General, give the description for the job. Scroll down and in the “Pipeline” section

    4. In the definition select Pipeline script, start typing the code for building a pipeline, and click on the Save button.

    5. Click on “Build Now” and the job will be built successfully.

    6. After a build is completed, you can view the console output on the build page.

    • Complete the example using the Declarative pipeline

1. Login to your Jenkins and Install the Docker Pipeline plugin through the Manage Jenkins > Manage Plugins page.

2. After installing the docker pipeline plugin click on “New Item”.

3. Enter the desired name, select “Pipeline” and then click on Ok.

4. In general give the description for the job. Scroll down and in the “Pipeline” section and in the definition select Pipeline script, start typing the code for building a pipeline, and click on the Save button.

    //Jenkinsfile (Declarative Pipeline)
    /* Requires the Docker Pipeline plugin */
    pipeline {
        agent { docker { image 'node:16.17.1-alpine' } }
        stages {
            stage('build') {
                steps {
                    sh 'node --version'
                }
            }
        }
    }

5. Click on “Build Now” and the job will be built successfully.

6. After a build is completed, you can view the console output in the build page.

Happy Learning:)

Conclusion:

In conclusion, Jenkins Pipelines, whether using the Declarative or Scripted syntax, serve as a cornerstone for modern CI/CD practices. They provide a systematic and automated approach to software development, ensuring quality, efficiency, and collaboration. The ability to version control pipelines and integrate them into the development process as code enhances the overall agility and reliability of software delivery. As organizations embrace DevOps principles, the Jenkins Pipeline stands out as an essential tool for streamlining workflows and achieving continuous improvement in software development and deployment.