3

We are using Jenkins extensively. We have several jobs, for building and testing. Our problem is that the current email notifications are not so good, so we are improving that. Basically, we are adding html to the email using information we can get from the pipeline.

Now, each job is already complex, with its own script and parameters. Some with parallel jobs. The pipeline of each job is already large and the email notification in html adds more code to it. And this would mostly be repeated for each job, so I'm trying to improve that.

I'm taking a look at the plugins. The idea is that writing a plugin would eliminate code duplication for each job. But I'm wondering if this is the best solution or maybe an overkill because I've never written a jenkins plugin.

So that's my question. When is writing a jenkins plugin necessary and when an overkill?

If I could find another way to share information between all the jobs that would be great.

3

To my mind, a Jenkins plugin allows you to build a simple interface for large and complex behavior, usually involving some kind of integration. What you probably need is the Shared Library feature:

From https://jenkins.io/doc/book/pipeline/shared-libraries/:

As Pipeline is adopted for more and more projects in an organization, common patterns are likely to emerge. Oftentimes it is useful to share parts of Pipelines between various projects to reduce redundancies and keep code "DRY".

Pipeline has support for creating "Shared Libraries" which can be defined in external source control repositories and loaded into existing Pipelines.

A Shared Library is defined with a name, a source code retrieval method such as by SCM, and optionally a default version. The name should be a short identifier as it will be used in scripts.

The shared library is essentially a git (or other) repo that Jenkins can pull. The code within that repo can be called from multiple pipelines.

I have had the same issue as you, and used a shared library to resolve it, creating a custom Groovy method to encapsulate and share the logic around email notification:

#!/usr/bin/env groovy

/*
    This script sends notification of pipeline results
    to the email addresses listed in emailsToNotify.
    Email is sent if the build fails, or if
    the build succeeds, and the previous build failed.
*/

// send notifications based on build result

import org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper

def call(Map<String, Object> options) {

    String[] emailsToNotify = options."emailsToNotify"
    RunWrapper currentBuild = options."currentBuild"
    String SUCCESS = options."success"
    String FAILURE = options."failure"

    // name of the upstream application that triggered the pipeline
    String appName = options."appName"

    Boolean currentBuildFailed = currentBuild.result != SUCCESS
    Boolean previousBuildFailed = false

    // ensure there is a previous build
    if (currentBuild.getPreviousBuild()) {
        previousBuildFailed = currentBuild.getPreviousBuild().getResult().toString() != SUCCESS
    }

    // only send email on failure, or on success if the previous build failed
    Boolean sendEmail = currentBuildFailed || (!currentBuildFailed && previousBuildFailed)

    if(sendEmail) {
        String jobId = "'${env.JOB_NAME} [${env.BUILD_NUMBER}]'".toString()
        String message

        if (previousBuildFailed && !currentBuildFailed) {
            message = "CHANGED FROM ${FAILURE} TO ${SUCCESS}".toString()
        } else {
            message = currentBuild.result
        }

        String subject = "${message}: ${appName} at ${jobId}".toString()
        String details = """\
                     <p>${message}: ${appName} at ${jobId}:</p><p>Check console output at &QUOT;
                     <a href='${env.BUILD_URL}'>${appName} ${jobId}</a>&QUOT; </p>
                """.toString()

        emailext(
                to: emailsToNotify.join(","),
                subject: subject,
                body: details
        )
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.