Sling Academy
Home/DevOps/How to define a Jenkins pipeline: A practical guide

How to define a Jenkins pipeline: A practical guide

Last updated: February 03, 2024

Introduction

Jenkins pipelines are a crucial aspect of automating tasks in the software development lifecycle, enabling continuous integration and delivery. This guide explores defining Jenkins pipelines, from basic concepts to more advanced use cases, illustrated with practical examples.

Getting Started with Jenkins Pipelines

A Jenkins pipeline is defined as code, which can be written in Groovy-based DSL (Domain Specific Language). To start, you’ll need to have Jenkins installed and running.

Example 1: Basic Pipeline

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building...'
      }
    }
    stage('Test') {
      steps {
        echo 'Testing...'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying...'
      }
    }
  }
}

This example demonstrates a basic pipeline with three stages: Build, Test, and Deploy. Each stage contains steps that execute commands or scripts.

Working with Parameters

Parameters allow you to pass values into the pipeline at runtime. This can be useful for specifying environment variables, version numbers, or any other configuration that might change between runs.

Example 2: Parameterized Pipeline

pipeline {
  agent any
  parameters {
    string(name: 'DEPLOY_ENV', defaultValue: 'staging', description: 'Deployment environment')
  }
  stages {
    stage('Deploy') {
      steps {
        script {
          echo "Deploying to ${params.DEPLOY_ENV}..."
        }
      }
    }
  }
}

This example introduces a parameter DEPLOY_ENV that is used within a single Deploy stage, demonstrating how to dynamically adjust pipeline behavior.

Using Groovy Script

For more complex scenarios, you can embed Groovy scripts inside the pipeline. This is particularly useful for conditional logic, loops, and accessing external resources.

Example 3: Advanced Pipeline with Groovy

pipeline {
  agent any
  stages {
    stage('Conditional Execution') {
      steps {
        script {
          if (env.BRANCH_NAME == 'main') {
            echo 'Deploying to production...'
          } else {
            echo 'Non-production branch, skipping...'
          }
        }
      }
    }
  }
}

This advanced example shows how to use environment variables and conditional logic based on the BRANCH_NAME to control the flow of the pipeline.

Parallel Execution and Post Actions

To speed up execution time and handle post-build actions, Jenkins Pipelines allow for parallel execution and defining post-actions.

Example 4: Parallel Execution

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

This example illustrates the parallel execution of both unit tests and integration tests within the same pipeline stage.

Example 5: Post Actions

pipeline {
  agent any
  stages {
    stage('Build and Test') {
      steps {
        echo 'Building and testing...'
      }
    }
  }
  post {
    always {
      echo 'This will always run'
    }
    success {
      echo 'Build succeeded!'
    }
    failure {
      echo 'Build failed!'
    }
  }
}

This example highlights how to define actions that execute after the stages complete, such as cleanup tasks or notifications, based on the outcome of the pipeline.

Conclusion

This guide covered the basics of defining Jenkins pipelines, progressing to more advanced features like parameters, Groovy scripts within pipelines, parallel execution, and post-actions. With these examples, you should be equipped to start automating your development workflows in Jenkins.

Next Article: Working with Jenkinsfile: A complete guide

Previous Article: 3 Ways to Install Jenkins on Ubuntu

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