Skip to content

Build status pipelines and Job DSL

This tutorial contains pipelines to produce builds with success, unstable, failed, aborted, and not-built statuses as well as Job DSL script to create a folder with projects that have these five different statuses.

Preparing the Jenkins instance

The pipeline provided by this tutorial can be added to any Jenkins instance you have administrator access. For example, Jenkins configuration from Jenkins with access to hosts Docker engine tutorial can be used.

In order to be able to run the seed project we will need Job DSL plugin. Install the plugin through Available tab in Manage Jenkins > Manage Plugins.

Creating and running the seed project

To run the job DSL script, create a new pipeline with following script as an inline pipeline script and run the created pipeline.

node {
    git branch: 'main', url: 'https://github.com/cicd-tutorials/cicd-tutorials.net.git'
    jobDsl targets: 'docs/tutorials/jenkins/build-status-pipelines/jobs.groovy'
}

The execution will likely fail with ERROR: script not yet approved for use message. To enable this script, navigate to Manage Jenkins > In-process Script Approval, inspect the script, and click Approve. Then try to run the created seed project again. It should now succeed and list the created resources.

The scripted pipeline listed above executes jobs.groovy script. This script creates five new pipelines and executes four of those.

jobs.groovy
String d = "docs/tutorials/jenkins/build-status-pipelines"

folder('Status') {
    description('Example pipelines to produce success, unstable, failed, aborted, and not-built statuses.')
}

def statuses = ['Success', 'Unstable', 'Failed', 'Aborted']
for (status in statuses) {
    def name = "Status/${status}"

    pipelineJob(name) {
        definition {
            cps {
                script(readFileFromWorkspace("$d/${status.toLowerCase()}.Jenkinsfile"))
                sandbox()
            }
        }
    }
    queue(name)
}

pipelineJob('Status/Not built') {
    definition {
        cps {
            script(readFileFromWorkspace("$d/success.Jenkinsfile"))
            sandbox()
        }
    }
}

The four different pipeline scripts used to create the jobs are listed below. The final job, Status/Not built, uses the same script as Status/Success, but the build is not executed.

Defines a pipeline that has a three minute timeout and build step that takes more than three minutes.

aborted.Jenkinsfile
pipeline {
    agent any
    options {
        timeout(time: 3, unit: 'MINUTES')
    }
    stages {
        stage('Sleep') {
            steps {
                sleep time: 5, unit: 'MINUTES'
            }
        }
    }
}

Defines a pipeline with single sh step that produces a non-zero exit code.

failed.Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Fail on purpose') {
            steps {
                sh 'exit 1'
            }
        }
    }
}

Defines a pipeline with single succeeding sh step.

success.Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Say hello') {
            steps {
                echo 'Hello!'
            }
        }
    }
}

Defines a pipeline with single unstable step.

unstable.Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Set unstable') {
            steps {
                unstable 'Demo unstable usage'
            }
        }
    }
}