Category: Apps and Tools

Jenkins Pipelines and the Blue Ocean plugin

With the big wave of DevOps, Pipelines are one of the most used words. Why? Because continuous integration and continuous delivery are about pipelines.

Jenkins is one of the most used components in a Continuous Integration environment, and its community decided to take into this wave too. How?

Well, redefining how the pipelines are configured and developed in Jenkins.

The new pipelines are developed using Groovy Language, which opens a very wide spectrum of possibilities of configurations to the integration process. (so… who said Groovy lang was dying?)

There are 2 ways of developing pipelines in Jenkins, the Declarative and the Scripted way

In the Scripted approach you use the full power of Groovy language, however, the blue ocean plugin will not be used as expected and you cant visualize the pipeline. More on this later.

node {
    stage('chekout from git') {
        
        checkout([$class: 'GitSCM', 
                branches: [[name: '*/master']], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [[]]
        ])
        try {
            sh 'mvn test'
        } catch (e) {
            currentBuild.result = 'FAILURE'
            throw e
        } finally {
            cleanWs cleanWhenFailure: false
        }
    }

    stage('build the project') {
        steps {
            sh 'mvn -B -DskipTests clean package'
        }
    }
}

But it’s the declarative way that draws the pipeline flow as we expected.

This approach brings first an elegant structure of the code and the use of the Blue Ocean plugin where we will visualize the pipeline flow, literally!

An example of a declarative pipeline can be something like this:

pipeline {
    agent none 
    stages {
        stage('chekout from git') {
            steps {
                checkout([$class: 'GitSCM', 
                    branches: [[name: '*/master']], 
                    doGenerateSubmoduleConfigurations: false, 
                    extensions: [], 
                    submoduleCfg: [], 
                    userRemoteConfigs: [[]]
                ])
              }
        }
        stage('build the project') {
            steps {
                sh 'mvn -B -DskipTests clean package'
            }
        }
    }
}

If you install the Blue Ocean plugin and run a declarative pipeline you will see something like this:
(this example was taken from Jenkins website)

Jenkins Pipeline Blue Ocean Plugin

So, should I use Declarative or Scripted Pipelines?
– Scripted Pipelines use the full Groovy language, but you will not have the enhanced visualization.
– Well, declarative is a little more restricted in the structure of the code, but you can always use the step “script { }” to extended the use of Groovy. But you will use the Blue Ocean plugin, something that has its value in a workspace for instance.

The Jenkins website as a good tutorial to follow, please visit it to move forward in this technology.

Hope this post was enough to capture your attention about how Jenkins can have a really beautiful and useful visualization of a pipeline.

Bad Behavior has blocked 51 access attempts in the last 7 days.

Hyper Smash