Skip to content

Jenkins Pipeline

Estimated time to read: 4 minutes

A Jenkins Pipeline is an automated sequence of processes (build, test, deploy, etc.) defined as code to implement Continuous Integration (CI) and Continuous Delivery (CD) in a Jenkins environment. By using pipelines, Jenkins automates the entire software delivery process, from code integration to testing, deployment, and even production.

Pipelines in Jenkins are written as code (typically using Groovy in a Jenkinsfile), which allows you to version control your automation process, make it reproducible, and collaborate on it just like application code

Key Concepts of Jenkins Pipelines:

  1. Pipeline as Code:
    • The entire pipeline can be scripted into a Jenkinsfile, stored in the same repository as your project code.
    • This allows for better version control and team collaboration.
  2. Stages:

    • Pipelines are broken down into multiple stages, such as Build, Test, and Deploy. Each stage represents a major phase in your CI/CD pipeline.
  3. Steps:

    • Each stage contains a series of steps, which are the individual commands or tasks that Jenkins runs.
    • Examples of steps include shell commands, running test scripts, building a Docker image, etc.
  4. Agents:

    • An agent specifies where Jenkins should run the pipeline or specific stages (on a Jenkins master, a specific node, or inside a Docker container).
  5. Declarative vs. Scripted Pipelines:

    • Declarative Pipeline: A simpler, structured syntax, making it easier to read and write.
    • Scripted Pipeline: More flexible and powerful but complex, written in Groovy and provides more control.
  6. Continuous Delivery/Continuous Deployment:

    • Jenkins Pipelines facilitate both Continuous Delivery (CD), which ensures your software is always ready to be released, and Continuous Deployment, which automates the deployment process to production.

Jenkins Pipeline

1. Example of a Declarative Pipeline

Declarative Pipelines are structured, simpler, and designed to be more user-friendly. They are defined using a well-structured, block-based syntax and provide pre-defined blocks like stages, steps, and post. Declarative pipelines are typically easier to read and maintain, which makes them the preferred choice for most use cases.

  • Structured Syntax: Uses a simple, predefined structure with blocks like pipeline, agent, stages, etc.
  • Easier to Understand: The syntax is straightforward, making it easier to write and debug.
  • Built-in Error Handling: Automatically handles some common errors, making pipelines more robust.
  • Optional Features: Supports optional blocks such as post, when, and input for conditions and post-build actions.

Example of a Declarative Pipeline:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                // Build steps (e.g., mvn build, npm install, etc.)
            }
        }
        stage('Test') {
            steps {
                echo 'Testing...'
                // Test steps (e.g., unit tests, integration tests, etc.)
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying...'
                // Deploy steps (e.g., deploy to staging, production, etc.)
            }
        }
    }
}

Benefits of Declarative Pipelines:

  • Readability: The pipeline is easier to read and write because of its structured format.
  • Error Handling: With features like post and when, it’s easier to define conditional actions and handle success/failure scenarios.
  • Built-in Steps: Comes with pre-defined steps and error-checking mechanisms.

2. Example of a Scripted Pipeline

Scripted Pipelines use a more flexible Groovy-based syntax. They allow full control over the pipeline, enabling advanced customization, but they are more complex to write and maintain. Scripted pipelines are ideal for more complicated pipelines requiring a lot of flexibility.

  • Groovy Scripting: Written entirely in Groovy, giving full control over the build process.
  • Unstructured Syntax: There is no enforced structure; users can organize the code as needed.
  • More Flexible: Provides more flexibility than declarative pipelines, useful for complex use cases.
  • Requires More Expertise: Since it is based on Groovy, you need to understand Groovy syntax and constructs.
groovy
node {
    try {
        stage('Build') {
            echo 'Building the project...'
            // Add build steps here
        }

        stage('Test') {
            echo 'Running tests...'
            // Add test steps here
        }

        stage('Deploy') {
            echo 'Deploying to production...'
            // Add deploy steps here
        }
    } catch (Exception e) {
        echo "Pipeline failed due to: ${e.message}"
    } finally {
        echo "Cleaning up resources..."
    }
}

Benefits of Scripted Pipelines:

  • Full Control: Provides access to Groovy’s full scripting capabilities, making it more flexible and customizable.
  • Dynamic Behavior: You can add more complex logic, loops, and conditional statements as part of the pipeline.
  • Compatibility with Older Pipelines: Supports more advanced use cases and is backward compatible with older Jenkins jobs.