Jenkins Pipeline Syntax: The Complete Cheat Sheet

Updated: February 3, 2024 By: Guest Contributor Post a comment

Overview

Jenkins, an open-source automation server, widely used for continuous integration and continuous delivery (CI/CD), supports defining pipelines as code (PaC). This approach enables automation of the building, testing, and deploying phases for software projects. Jenkinsfiles, which store the pipeline definitions, use a domain-specific language (DSL) based on Groovy for this purpose. In this comprehensive cheat sheet, we’ll guide you through the syntax of Jenkins Pipeline, starting from basic to advanced examples.

Introduction to Jenkins Pipeline

Before diving into the syntax, it’s essential to understand the two types of Jenkins pipelines:

  • Declarative Pipeline: A newer syntax that prioritizes ease of use and readability. It starts with a pipeline block and uses predefined structures and statements.
  • Scripted Pipeline: An older, more flexible syntax that offers more control through Groovy scripting. It’s defined within a node block.

Basic Example: Hello World

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Hello, World!'
            }
        }
    }
}

This simple declarative pipeline script runs on any available agent, defines a single stage called ‘Build’, and executes the command to print ‘Hello, World!’.

Running On Specific Agent

pipeline {
    agent { label 'my-agent' }
    stages {
        stage('Test') {
            steps {
                script {
                    println('Testing on a specific agent')
                }
            }
        }
    }
}

Here, the pipeline is configured to run on an agent with the label ‘my-agent’.

Using Parameters

pipeline {
    agent any
    parameters {
        string(name: 'GREETING', defaultValue: 'Hello', description: 'The greeting to use')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params.GREETING}, World!"
            }
        }
    }
}

This example illustrates how to define and use parameters within your Jenkins Pipeline.

Conditional Execution

pipeline {
    agent any
    stages {
        stage('Conditional') {
            steps {
                script {
                    if (env.BUILD_NUMBER == '1') {
                        echo 'This is the first build'
                    } else {
                        echo 'This is not the first build'
                    }
                }
            }
        }
    }
}

Conditional logic allows different execution paths based on specific conditions, such as the build number in this case.

Parallel Stages

pipeline {
    agent any
    stages {
        stage('Parallel Stages') {
            parallel {
                stage('Unit Test') {
                    steps {
                        echo 'Running unit tests'
                    }
                }
                stage('Integration Test') {
                    steps {
                        echo 'Running integration tests'
                    }
                }
            }
        }
    }
}

This script demonstrates how to run multiple stages in parallel, significantly reducing the pipeline’s total execution time.

Using Shared Libraries

The use of shared libraries can greatly simplify and reduce the duplication of script code in Jenkinsfiles. For common functionality across multiple pipelines, you can define shared libraries that can be imported and used in your Jenkinsfile. Detailed documentation and examples can be found in the Jenkins documentation.

Advanced Example: Deploying with Docker

pipeline {
    agent any
    stages {
        stage('Build Image') {
            steps {
                script {
                    docker.build('my-image:${env.BUILD_NUMBER}').push()
                }
            }
        }
    }
}

In this advanced example, the pipeline builds a Docker image with a tag corresponding to the Jenkins build number and then pushes it to a registry. This is a common use case in CI/CD workflows for deploying applications.

Conclusion

Understanding the syntax of Jenkins Pipeline allows you to automate your CI/CD workflows efficiently. Starting with basic examples and progressing to more complex scenarios, this cheat sheet provides a solid foundation for using Jenkins Pipeline effectively. As you become more familiar with the syntax, you’ll find it simpler to tailor Jenkinsfiles to your specific needs.