Jenkins Pipeline, a suite of plugins that enables continuous integration and delivery (CI/CD) within Jenkins, has revolutionized the way software development teams manage their build, test, and deployment processes. By automating these workflows, Jenkins Pipeline ensures consistency, improves efficiency, and reduces manual intervention. Let's explore some practical Jenkins Pipeline examples to illustrate its capabilities and benefits.

Before diving into the examples, let's briefly recap the basic structure of a Jenkins Pipeline script, known as a Jenkinsfile. A Jenkinsfile typically starts with pipeline definition and includes stages, steps, and directives that define the workflow. Now, let's delve into some real-world Jenkins Pipeline examples.

Basic Jenkins Pipeline Example
This example demonstrates a simple Jenkins Pipeline that builds, tests, and deploys a Java application using Maven, JUnit, and a fictional deployment tool called "Deployer".

```groovy pipeline { agent any stages { stage('Build') { steps { sh 'mvn clean package' } } stage('Test') { steps { sh 'mvn test' junit 'target/surefire-reports/TEST-*.xml' } } stage('Deploy') { steps { sh 'deployer deploy' } } } } ```
Pipeline as Code

In this example, the Jenkins Pipeline is defined as code within a Jenkinsfile, allowing for version control, easy maintenance, and better collaboration among team members.
The script above demonstrates the use of stages, steps, and directives to define a simple CI/CD pipeline. The 'agent any' directive specifies that the pipeline can run on any available Jenkins agent. The 'stages' block contains three stages: 'Build', 'Test', and 'Deploy', each with its respective steps.
Using Parameters and Input Steps

This example shows how to incorporate user input and parameters into a Jenkins Pipeline, allowing for dynamic and flexible workflows.
```groovy pipeline { agent any parameters { string(name: 'VERSION', defaultValue: '1.0.0', description: 'The version number to use') } stages { stage('Build') { steps { sh "mvn clean package -Dversion=${params.VERSION}" } } stage('Test') { steps { sh "mvn test" junit 'target/surefire-reports/TEST-*.xml' } } stage('Deploy') { input 'Are you sure you want to deploy?' steps { sh "deployer deploy ${params.VERSION}" } } } } ```
The 'parameters' block defines a string parameter called 'VERSION', which is then used within the 'Build' and 'Deploy' stages. The 'input' step in the 'Deploy' stage pauses the pipeline and requires user confirmation before proceeding with the deployment.

Advanced Jenkins Pipeline Example: Multi-branch Pipeline
In this example, we'll explore a more complex Jenkins Pipeline that builds, tests, and deploys multiple branches of a Java project, using a matrix of Jenkins agents to parallelize the process.




















The following Jenkinsfile demonstrates the use of matrix-based parallelism, dynamic view steps, and the 'when' directive to control pipeline behavior based on branch names.
Matrix-based Parallelism
```groovy pipeline { agent { label 'matrix' dimensions { axis { name 'OS' values 'linux', 'windows' } axis { name 'JDK' values '8', '11' } } } stages { stage('Build') { steps { sh "mvn clean package -DskipTests=true" } post { always { junit 'target/surefire-reports/TEST-*.xml' } } } stage('Test') { steps { sh "mvn test" } } stage('Deploy') { when { branch 'master' } steps { sh "deployer deploy" } } } } ```
The 'agent' block defines a matrix of Jenkins agents, with dimensions for 'OS' and 'JDK'. This allows the pipeline to run builds in parallel for each combination of operating system and Java Development Kit (JDK) version.
Dynamic View Steps and Branch-specific Behavior
```groovy pipeline { agent any stages { stage('Build') { steps { sh "mvn clean package -DskipTests=true" } post { always { junit 'target/surefire-reports/TEST-*.xml' publishHTML(target: 'target/site/jacoco/index.html', alwaysLinkToLastBuild: true, keepAll: true) } } } stage('Test') { steps { sh "mvn test" } } stage('Deploy') { when { branch 'master' } steps { sh "deployer deploy" } } stage('View Results') { when { branch 'feature/*' } steps { echo 'Viewing test results for feature branch' viewResults = readJSON file: 'target/jacoco.json' echo "Total coverage: ${viewResults.total.coverage}" } } } } ```
The 'post' block in the 'Build' stage demonstrates the use of the 'always' directive to run steps regardless of the stage's outcome. In this case, it publishes test results using the 'junit' step and generates a coverage report using the 'publishHTML' step.
The 'Deploy' and 'View Results' stages use the 'when' directive to control pipeline behavior based on the current branch name. The 'Deploy' stage only runs when the branch is 'master', while the 'View Results' stage only runs when the branch matches the pattern 'feature/*'.
In conclusion, Jenkins Pipeline offers a powerful and flexible way to automate CI/CD workflows. By leveraging the examples discussed in this article, you can create tailored pipelines that meet the specific needs of your development team. Embrace the power of Jenkins Pipeline to streamline your software development processes and drive continuous improvement. Happy pipelining!