Pipeline Job in Jenkins

Pipeline Job in Jenkins

In the last post, we showed how to get started with the Jenkins with freestyle job. However, in most cases, we should use a pipeline job instead of the freestyle job in Jenkins.

Create a Simple Pipeline

Let's first create a hello world script in the pipeline job.

Then, let's convert it into the SCM-based pipeline. Here is the sample repository we use.

#!/usr/bin/env groovy
pipeline {
    agent none
    stages {
        stage('build') {
            steps {
                script {
                    echo "Building the application..."
                }
            }
        }
        stage('test') {
            steps {
                script {
                    echo "Testing the application..."
                }
            }
        }
        stage('deploy') {
            steps {
                script {
                    echo "Deploying the application..."
                }
            }
        }
    }
}

Here is the test result:

Groovy Script

In the deployment process, sometimes we need the complex logic. We can introduce the Groovy script. In this example, the script pipeline does the following tasks.

  • Build the jar file in the repository

  • Build the application into docker image

  • Deploy the docker image into my docker hub account.

#!/usr/bin/env groovy

def gv

pipeline {
    agent any
    tools {
        maven 'maven'
    }
    stages {
        stage("init") {
            steps {
                script {
                    gv = load "script.groovy"
                }
            }
        }
        stage("build jar") {
            steps {
                script {
                    gv.buildJar()
                }
            }
        }
        stage("build image") {
            steps {
                script {
                    gv.buildImage()
                }
            }
        }
        stage("deploy") {
            steps {
                script {
                    gv.deployApp()
                }
            }
        }
    }
}
def buildJar() {  
    echo "building the application..."  
    sh 'mvn package'  
}   

def buildImage() {  
    echo "building the docker image..."  
    withCredentials([usernamePassword(credentialsId: 'Dockerhub', passwordVariable: 'PASS', usernameVariable: 'USER')]) {  
        sh 'docker build -t stevenchen521/demo-app:jma-2.0 .'  
        sh "echo $PASS | docker login -u $USER --password-stdin"        sh 'docker push stevenchen521/demo-app:jma-2.0'  
    }  
}   

def deployApp() {  
    echo 'deploying the application...'  
}   

return this

In the script, we use the credentials "Dockerhub" which is defined in the previous blog.

Multi-branch Pipeline Job

It is very common in practice we create multiple branches for different purposes, i.e., new features or bug fixings. There could be different requirements for different branches. For instance, if it's just the feature or bug fix branch, we should only do the test and skip the others. We do the build only for the main branch.

Let us do the sample, we create the new branch "feature1". In both "master" and "feature1" branches, we change the Jenkins-basic file to the following

pipeline {
    agent any
    tools {
        maven 'maven'    
    }
    stages{
        stage("test"){
            steps{
                echo "testing the application"
                echo "Executing pipeline for branch $BRANCH_NAME"
            }
        }

        stage("build"){
            when{
                expression{
                    BRANCH_NAME == 'main'
                }
            }

            steps{
                echo "building the application..."
            }
        }


        stage("deploy"){
            steps{
                echo "deploying the application..."
            }
        }
    }

}

Then we click the button "Scan Multibranch Pipeline Now". Once it finished, we will see two branches on the right side.

Here are the execution logs of "feature1" branch.

Summary

In this post, we started a simple Jenkins Pipeline job. Then we introduced the groovy scripts in the Pipeline. Finally we demonstrated the multi-branch pipeline job which is very common in practice.

Reference

Module 8 of DevOps Bootcamp