DEV Community

Cover image for Jenkins pipelines: Types,Characteristics and Explanation
Avesh
Avesh

Posted on

Jenkins pipelines: Types,Characteristics and Explanation

Jenkins pipelines can be customized extensively to fit various CI/CD needs. Here’s an overview of five common types:

  1. Simple Build and Test Pipeline
  2. Multi-Branch Pipeline
  3. Parameterized Pipeline
  4. Pipeline with Parallel Execution
  5. Pipeline with Conditional Steps

Each of these pipeline types uses specific Jenkinsfile configurations and Groovy-based syntax to address different project requirements.


1. Simple Build and Test Pipeline

A Simple Build and Test Pipeline is one of the most basic types of pipelines. It includes separate stages for building, testing, and deploying code.

Key Characteristics

  • Sequential execution: Each stage runs in sequence.
  • Basic stages for CI: Includes build, test, and deploy steps.
  • Useful for small projects that require standard build and test workflows.

Example

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/user/repo.git', branch: 'main'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying to staging server...'
                sh 'scp target/*.jar user@staging-server:/path/to/deploy'
            }
        }
    }

    post {
        success {
            echo 'Pipeline completed successfully.'
        }
        failure {
            echo 'Pipeline failed.'
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

This pipeline starts with a Checkout stage to pull code from a Git repository, then moves on to Build and Test stages, and finishes with a Deploy stage. The post block handles notifications based on the pipeline’s success or failure.


2. Multi-Branch Pipeline

A Multi-Branch Pipeline allows Jenkins to automatically create pipelines for each branch in a repository. It’s useful for projects with multiple branches that require different builds or testing strategies.

Key Characteristics

  • Branch-specific builds: Automatically discovers branches and creates pipelines for each one.
  • Isolated pipelines: Each branch runs independently.
  • Great for repositories with feature branches.

Example Jenkinsfile

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/user/repo.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
    }

    post {
        success {
            echo "Branch ${env.BRANCH_NAME} built successfully!"
        }
        failure {
            echo "Branch ${env.BRANCH_NAME} failed."
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

This pipeline retrieves the branch name using ${env.BRANCH_NAME}. The pipeline’s Checkout, Build, and Test stages execute for each branch. The multi-branch pipeline setup in Jenkins automates the process of running the pipeline per branch and is ideal for feature development or pull request workflows.


3. Parameterized Pipeline

A Parameterized Pipeline allows developers to input parameters when triggering a pipeline, such as specifying an environment (e.g., staging, production) or setting a custom build version.

Key Characteristics

  • Parameterized inputs: Accepts user inputs for custom builds.
  • Flexible deployment: Enables different configurations depending on input parameters.
  • Ideal for running pipelines with different environment configurations.

Example with Parameters

pipeline {
    agent any

    parameters {
        choice(name: 'ENV', choices: ['staging', 'production'], description: 'Choose the environment')
        string(name: 'VERSION', defaultValue: '1.0', description: 'Build version')
    }

    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/user/repo.git'
            }
        }
        stage('Build') {
            steps {
                echo "Building version ${params.VERSION} for ${params.ENV} environment..."
                sh 'mvn clean install'
            }
        }
        stage('Deploy') {
            steps {
                echo "Deploying to ${params.ENV} environment..."
                // Add deploy command here based on environment
            }
        }
    }

    post {
        success {
            echo "Pipeline for ${params.ENV} environment completed successfully."
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

This pipeline accepts two parameters: ENV (environment) and VERSION (build version). In the Build and Deploy stages, these parameters are used to configure actions based on the selected environment and version. This flexibility allows deploying the pipeline in different configurations without modifying the code.


4. Pipeline with Parallel Execution

A Pipeline with Parallel Execution allows multiple stages or tasks to run concurrently, which significantly speeds up the pipeline by processing tasks that don’t depend on each other in parallel.

Key Characteristics

  • Faster builds: Parallelizes tasks that can run independently.
  • Ideal for tests: Commonly used for running multiple test suites (e.g., unit, integration) at the same time.
  • Useful in large projects with multiple independent tasks.

Example

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'https://github.com/user/repo.git'
            }
        }
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Parallel Testing') {
            parallel {
                stage('Unit Tests') {
                    steps {
                        sh 'mvn test -Dtest=unit'
                    }
                }
                stage('Integration Tests') {
                    steps {
                        sh 'mvn test -Dtest=integration'
                    }
                }
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application...'
                // Deployment commands
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

The Parallel Testing stage contains two parallel sub-stages: Unit Tests and Integration Tests. These stages execute simultaneously, reducing the overall pipeline runtime. This structure is beneficial for large projects with multiple test types that can be run independently.


5. Pipeline with Conditional Steps

A Pipeline with Conditional Steps allows stages or steps to run based on certain conditions, such as running a deployment stage only if the branch is main or only if a certain environment variable is set.

Key Characteristics

  • Conditional execution: Skips or includes stages based on conditions.
  • Useful for environment-specific workflows: Commonly used for conditional deployment or testing.
  • Ideal for CI/CD pipelines with multiple environments.

Example with Conditions

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'mvn clean install'
            }
        }
        stage('Test') {
            steps {
                sh 'mvn test'
            }
        }
        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                echo 'Deploying to production server...'
                // Production deployment commands
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation

In this example, the Deploy stage uses a when condition, which restricts it to run only on the main branch. This structure is helpful when the deployment should occur only in specific environments or for certain branches, reducing the risk of accidental deployments in feature branches.


Conclusion

These five types of Jenkins pipelines—Simple Build and Test, Multi-Branch, Parameterized, Parallel Execution, and Conditional Pipelines—offer diverse solutions for automating CI/CD processes. Whether you need a straightforward pipeline or a complex, environment-specific workflow, Jenkins provides the flexibility to adapt to your needs. By understanding and using these examples, you can create efficient, maintainable pipelines suited to various software development workflows.

Top comments (0)