Day 26 - Jenkins Pipeline

Day 26 - Jenkins Pipeline

What is a Jenkins Pipeline?

A pipeline is a collection of steps or jobs which are interlinked with one another in a sequence.

Jenkins Pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

Pipelines are also referred to as a “Deployment-as-a-Code”.

A continuous delivery (CD) pipeline is an automated expression of your process for getting software from version control right through to your users and customers. Every change to your software (committed in source control) goes through a complex process on its way to being released. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a "build") through multiple stages of testing and deployment.

What is Jenkinsfile?

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 a number of 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).

  • Audit trail for the Pipeline.

  • Single source of truth for the Pipeline, which can be viewed and edited by multiple members of the project.

While the syntax for defining a Pipeline, either in the web UI or with a Jenkinsfile is the same, it is generally considered best practice to define the Pipeline in a Jenkinsfile and check that in to source control.

Why Pipeline?

Jenkins is, fundamentally, an automation engine which supports a number of automation patterns. Pipeline adds a powerful set of automation tools onto Jenkins, supporting use cases that span from simple continuous integration to comprehensive CD pipelines. By modeling a series of related tasks, users can take advantage of the many features of Pipeline:

  • Code: Pipelines are implemented in code and typically checked into source control, giving teams the ability to edit, review, and iterate upon their delivery pipeline.

  • Durable: Pipelines can survive both planned and unplanned restarts of the Jenkins controller.

  • Pausable: Pipelines can optionally stop and wait for human input or approval before continuing the Pipeline run.

  • Versatile: Pipelines support complex real-world CD requirements, including the ability to fork/join, loop, and perform work in parallel.

  • Extensible: The Pipeline plugin supports custom extensions to its DSL and multiple options for integration with other plugins.

Types of Jenkins Pipeline

There are 2 types of Jenkins Pipeline: -

  1. Scripted Pipeline

  2. Declarative Pipeline

    Declarative vs Scripted Jenkins Pipelines: What You Need to Know

Scripted Pipeline

Jenkins only allowed the user to write the pipeline as a code, hence a scripted pipeline. Such a pipeline is written in a Jenkinsfile on the web UI of the Jenkins tool. You can write a scripted pipeline with DSL (domain specific language) and use Groovy based syntax.

The Job DSL plugin provides a solution, and allows you to configure Jenkins jobs as code.

What exactly is Groovy? It is a programming language that uses the Java virtual machine(JVM).

A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode.

Advantages of Scripted Pipeline

  • Scripted pipelines offer a full-fledged programming ecosystem to developers

  • Developers can inject Groovy scripts and reference Java APIs inside a scripted pipeline definition

  • Blocks of scripted pipelines can be injected inside the script step of a declarative pipeline definition. This enhances cross-pipeline support.

Disadvantage of Scripted Pipeline

  • Scripted syntax can be harder to learn for beginners as compared to declarative syntax

  • Scripted pipelines don’t offer runtime syntax checking

  • Scripted pipelines don’t have the When directive, which can be used to skip a stage if a condition isn’t met

  • It isn’t possible to restart a scripted pipeline from a previous stage

Syntax

node {  
    stage('Build') { 
        // Execute Some steps here
    }
    stage('Test') { 
        // Execute Some steps here
    }
    stage('Deploy') { 
        // Execute Some steps here
    }
}

Declarative Pipeline

In many ways, declarative syntax represents the modern way of defining pipelines. It is robust, clean, and easy to read and write.

The declarative coding approach dictates that the user specifies only what they want to do, not how they want to do it. This makes it easier to create declarative pipelines, as compared to scripted pipelines, which follow the imperative coding approach.

A declarative pipeline offers capabilities for adding logging and error handling, supports the use of conditional statements, permits access to environment variables, and more.

Advantages of declarative pipelines

  • Syntax checking is performed at runtime in declarative pipelines. Explicit error messages are reported to the user before the execution is started.

  • Users can link their declarative Jenkinsfile using a built-in API endpoint or a CLI command.

  • Declarative pipelines offer extensive support for Docker pipeline integration. Users can choose to run all stages within a single container or each stage in a different container.

  • The visual pipeline editor can be used to write declarative code in a user-friendly manner. The pipeline syntax snippet generator is a useful tool to auto-generate code snippets.

  • Declarative syntax allows users to introduce conditional logic, including taking actions depending on the success or failure of a step or skipping a stage based on the value of a variable.

  • The When directive can be used to skip over steps if certain conditions are not met.

Disadvantage of Declarative Pipeline

  • Developers who have traditionally injected complicated business logic into pipeline code may struggle with certain limitations of declarative pipelines.

  • For organizations that have been dealing with scripted pipelines for a long time, migrating from scripted to declarative code can be time-consuming and error-prone.

  • It’s impossible to inject blocks of declarative code inside a scripted pipeline. This limits cross-pipeline support.

  • In a declarative pipeline, a reference to a Groovy script or a Java API will result in a compilation error.

Syntax

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

Pipeline concepts

The following concepts are key aspects of Jenkins Pipeline, which tie in closely to Pipeline syntax.

  1. Pipeline: A Pipeline is a user-defined model of a CD pipeline. A Pipeline’s code defines your entire build process, which typically includes stages for building an application, testing it and then delivering it.

    Also, a pipeline block is a key part of Declarative Pipeline syntax.

  2. Node: A node is a machine which is part of the Jenkins environment and is capable of executing a Pipeline.

    Also, a node block is a key part of Scripted Pipeline syntax.

  3. Agent: is Declarative Pipeline-specific syntax that instructs Jenkins to allocate an executor (on a node) and workspace for the entire Pipeline.

  4. Stage: A stage block defines a conceptually distinct subset of tasks performed through the entire Pipeline (e.g. "Build", "Test" and "Deploy" stages), which is used by many plugins to visualize or present Jenkins Pipeline status/progress.

  5. Step: A single task. Fundamentally, a step tells Jenkins what to do at a particular point in time (or "step" in the process). For example, to execute the shell command make, use the sh step: sh 'make'. When a plugin extends the Pipeline DSL, that typically means the plugin has implemented a new step.

Declarative Pipeline Examples

We will be moving forward with Declarative pipeline as it is mostly in use now.

EXAMPLE - 1

In this , we are writing pipeline syntax direct in Pipeline.

Step 1: Start your EC2 instance and connect your terminal.

Step 2: Login to your Jenkins via browser using <Your_Public_IP:8080>.

Step 3: Click on "New Item".

Step 4: Enter the name of the item as "First_Pipeline". Click on Pipeline and click Ok.

Step 5: Go to the end of the page, in the Pipeline section: Type the declarative pipeline script for Hello World as shown below and click on save.

Here the script is defined within the pipeline block.

 pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

Step 6: Click on "Build Now".

Step 7: The below image shows that the job is to build And it's Successful.

Step 8: Click on "Console Output" to see the complete Output.

EXAMPLE - 2

In this, we will write a Jenkinsfile and use that in our pipeline.

Step 1: Login to your GitHub and write a Jenkinsfile. We are using the same code used above.

Step 2: Login to your Jenkins and Create a pipeline.

Step 3: Now In pipeline section, select "Pipeline script from SCM". And in SCM, select "Git".
Coy the "Repository URL" from GitHub and paste it in the "Repository URL" under Repositories Section and specify the "branch name" where the Jenkinsfile is located and Click on Save as shown below.

Step 4: Now click on "Build Now". And you can see pipeline is build and it's successful.

Step 5: Click on "Console Output" to see the complete Output.

So, these are the two examples, you can build your declarative pipeline.

Conclusion

In conclusion, a pipeline in the context of continuous integration and continuous delivery (CI/CD) is a set of automated processes that allow for the efficient and reliable delivery of software. Jenkins, a popular automation server, supports the definition of pipelines using Jenkinsfile, which can be written in either scripted or declarative syntax.

A Jenkinsfile serves as a configuration file that defines the pipeline stages, steps, and their relationships.

Scripted pipelines use a more flexible, imperative scripting language for defining pipeline logic, while declarative pipelines use a more structured and concise syntax for a more opinionated and streamlined approach.

Scripted pipelines offer greater flexibility and customization, allowing users to write arbitrary code in a Groovy-based scripting language. On the other hand, declarative pipelines provide a more straightforward syntax with predefined constructs for common use cases, promoting a more declarative and versionable approach to pipeline definition.

Ultimately, the choice between scripted and declarative pipelines depends on the specific needs and preferences of the development team. Both approaches aim to streamline and automate the software delivery process, contributing to more efficient and reliable software development practices.

Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨

👆The information presented above is based on my interpretation. Suggestions are always welcome.😊

~Smriti Sharma✌