Sling Academy
Home/DevOps/Jenkins Pipeline Syntax: The Complete Cheat Sheet

Jenkins Pipeline Syntax: The Complete Cheat Sheet

Last updated: February 03, 2024

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.

Next Article: Jenkins: How to load external libraries into Pipeline

Previous Article: Branches and Pull Requests in Jenkins: A Deep Dive (with examples)

Series: Jenkins Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide