Streamline Your Continuous Integration and Delivery with Jenkins Pipeline: A Comprehensive Guide

Jenkins is a popular open-source automation server that is widely used for Continuous Integration and Continuous Deployment [CI/CD] in software development. Jenkins Pipeline is a powerful tool that allows developers to define their build process as code. In this blog, we will discuss the basics of Jenkins Pipeline and the advantages of using it.

Pipeline Scripting

Jenkins Pipeline enables developers to configure jobs with code. This means that you can define your job as a script and then run it in Jenkins. This approach has several advantages:

  • Jobs can be divided into parts [build, test, deploy] and each part can run on different agents.
  • Stages can be executed in parallel, which can save time.
  • Each stage can execute with different versions of JDK/Maven versions
  • Failed stages can be re-triggered
  • The build flow can be visualized
  • Build can be paused and restarted.

Types of Pipeline

There are two types of Jenkins Pipeline:

  1. Declarative Pipeline
  2. Scripted Pipeline

Declarative vs. Scripted Pipeline

Declarative Pipeline is a more recent addition to Jenkins Pipeline. It has a more simplified and opinionated syntax when compared to Scripted Pipeline. Declarative syntax is easier to read and write, and it follows a specific structure. Scripted Pipeline, on the other hand, has a more flexible syntax that allows developers to write any Groovy code they want.

Declarative Pipeline Syntax

// Declarative //
pipeline {
    agent any
    stages {
        stage[‘Example’] {
            steps {
                echo ‘Hello World’
            }
        }
    }
    post { 
        always { 
            echo ‘I will always say Hello again!’
        }
    }
}
// Script //

Scripted Pipeline Syntax

// Scripted //
node {
    stage[‘Example’] {
        try {
            sh ‘exit 1’
        }
        catch [exc] {
            echo ‘Something failed, I should sound the klaxons!’
            throw
        }
    }
}
// Declarative //

Pipeline Basics

The following are some of the basics of Jenkins Pipeline:

  • Steps

Steps are the commands or scripts that we use to build. We need to write a step inside the stage directive. A stage directive can contain one or more steps.

  • Stage

 A stage defines a particular stage of our job, such as build, test, or deploy. Each stage has a name, which is displayed on the Jenkins dashboard.

  • Stages

Stages are sequences of stages. At least one stage must be defined in a pipeline.

  • Agent

The agent defines where we want to run our pipeline script. This can be the master, slave, container, or any other agent.

  • Stage Colors

The colors of a stage represent the status of the stage:

  1. White [stage is not executed]
  2. Green [stage is success]
  3. Blue lines [stage is executing]
  4. Red lines [stage is failed]
  5. Red [a few stages are successful, but any one failed, so all stages show red]

Hello World Pipeline Example:

Here is a simple Jenkins Pipeline example that prints “Hello World” to the console:

pipeline {
    agent any
    stages {
        stage['Hello'] {
            steps {
                echo 'Hello World'
            }
        }
    }
}

This Pipeline script declares a single stage called “Print Hello World”, which contains a single step that prints the string “Hello World” to the console. The echo step is a built-in Pipeline step that prints the specified string to the console log.

This script can be run on any agent that is available in the Jenkins environment by specifying agent any. By declaring the agent as any, Jenkins will automatically choose an available agent to run the script on.

Overall, this is a very basic example of a Jenkins Pipeline that demonstrates how to create a simple Pipeline with a single stage and step.

Finally, Jenkins Pipeline is a powerful tool that can automate software development processes, making them faster, more efficient, and less prone to errors. With the right setup, it can help teams streamline their workflows and increase productivity.

In short, if you’re a software development team that wants to take your processes to the next level, Jenkins Pipeline is an essential tool to have in your toolbox.

Thanks for reading, and be sure to follow us for more insights and tips on DevOps and Cloud.

Leave a Reply

Your email address will not be published. Required fields are marked *