An Introduction to Jenkins Pipeline: Simplifying Continuous Integration and Delivery with Examples
Tags: jenkins, devops, groovy
Published: 2023-04-22 17:25:00

In today's fast-paced software development world, continuous integration and delivery (CI/CD) have become essential practices to ensure high-quality software and rapid deployment. 

Jenkins, an open-source automation server, has long been a popular choice for implementing CI/CD workflows. In this article, we introduce you to Jenkins Pipeline, a powerful feature that simplifies creating and managing CI/CD pipelines with easy-to-understand examples.

## What is Jenkins Pipeline?

Jenkins Pipeline is a plugin suite allowing you to define and automate your CI/CD workflows directly within your source code repository. With Pipeline, you can create complex pipelines as code using a domain-specific language called Groovy. 

This approach enables better collaboration, version control, and increased flexibility in managing and customizing your channels.

**Example 1: A Simple Jenkins Pipeline**

Let's consider a simple example of a Jenkins Pipeline to get started. In this scenario, we will create a pipeline that builds a Java project and archives the build artifacts.

1. First, create a new file called `Jenkinsfile` in the root 
   directory of your project's source code repository.
2. Next, open the `Jenkinsfile` and add the following code:

```
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the project...'
                sh 'mvn clean compile'
            }
        }
        stage('Archive artifacts') {
            steps {
                echo 'Archiving build artifacts...'
                archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
            }
        }
    }
}
```
In this example, the pipeline has two stages: `Build` and `Archive artifacts`. The Build stage runs the Maven command `mvn clean compile` to build the Java project, while the `Archive artifacts` stage archives the build artifacts (JAR files).

**Example 2: Adding a Test Stage**

Now let's extend the previous example by adding a stage for running unit tests.

1. Modify your `Jenkinsfile` by adding a new stage called 'Test' 
   after the 'Build' stage:

```
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the project...'
                sh 'mvn clean compile'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'mvn test'
            }
        }
        stage('Archive artifacts') {
            steps {
                echo 'Archiving build artifacts...'
                archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
            }
        }
    }
}

```
The new `Test` stage runs the Maven command `mvn test` to execute the unit tests for the project.

**Example 3: Adding a Deployment Stage**

In this example, we will extend the pipeline by adding a deployment stage that deploys the application to a staging environment.

1. Update your `Jenkinsfile` by adding a new stage called 'Deploy 
   to staging' after the 'Archive artifacts' stage:

```
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the project...'
                sh 'mvn clean compile'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
                sh 'mvn test'
            }
        }
        stage('Archive artifacts') {
            steps {
                echo 'Archiving build artifacts...'
                archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true
            }
        }
        stage('Deploy to staging') {
            steps {
                echo 'Deploying to the staging environment...'
                script {
                    def server = 'staging.example.com'
                    def artifact = findFiles(glob: '**/target/*.jar')[0]
                    sh "scp ${artifact} user@${server}:/path/to/deploy/directory"
                }
            }
        }
    }
}
              
```
In this new `Deploy to staging` stage, we use the `script` block to define a custom deployment script that transfers the build artifact (JAR file) to the staging environment using the `scp` command. Replace `staging.example.com`, `user`, and `/path/to/deploy/directory` with your actual server address, username, and deployment directory, respectively.

## Conclusion

Jenkins Pipeline is a powerful feature that simplifies the creation and management of CI/CD pipelines by defining them as code. 

With Jenkins Pipeline, you can quickly create complex workflows, version control your pipelines, and customize them according to your project's needs. 

The examples in this article offer a glimpse into how you can leverage Jenkins Pipeline to build, test, and deploy your applications, making your development process more efficient and streamlined.

Thank you for sticking with me till the end. You’re a fantastic reader!

**Ahsan Mangal**

I hope you found it informative and engaging. If you enjoyed this content, please consider following me for more articles like this in the future. Stay curious and keep learning!