Declarative Pipeline: Publishing HTML Reports

Category : Devops | Sub Category : Jenkins | By Runner Dev Last updated: 2021-03-20 09:28:28 Viewed : 158


Declarative Pipeline: Publishing HTML Reports 

Declare Your Pipelines! Declarative Pipeline 1.0 is here! This is the third post in a series showing some of the cool features of Declarative Pipeline.

In the previous post, we converted a Scripted Pipeline to a Declarative Pipeline, adding descriptive stages and post sections. In one of those post blocks, we included a placeholder for sending notifications.

In this blog post, we’ll repeat what I did in "Sending Notifications in Pipeline but this time in Declarative Pipeline. First we’ll integrate calls to notification services Slack, HipChat, and Email into our Pipeline. Then we’ll refactor those calls into a single Step in a Shared Library, which we’ll reuse as needed, keeping our Jenkinsfile concise and understandable.

Setup

The setup for this post is almost the same as my previous Declarative Pipeline post. I’ve used a new branch in my fork of the Hermann projectblog/declarative/notifications. I’d already set up a Multibranch Pipeline and pointed it at my repository, so the new branch will be picked up and built automatically.

I still have my notification targets (where we’ll send notifications) that I created for the "Sending Notifications in Pipeline" blog post. Take a look at that post to review how I setup the SlackHipChat, and Email-ext plugins to use those channels.

Adding Notifications

We’ll start from the same Pipeline we had at the end of the previous post.

This Pipeline works quite well, except it doesn’t print anything at the start of the run, and that final always directive only prints a message to the console log. Let’s start by getting the notifications working like we did in the original post. We’ll just copy-and-paste the three notification steps (with different parameters) to get the notifications working for started, success, and failure.

 

pipeline {

  /* ... unchanged ... */

  stages {

    stage (`Start`) {

      steps {

        // send build started notifications

        slackSend (color: `#FFFF00`, message: "STARTED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]` (${env.BUILD_URL})")

 

        // send to HipChat

        hipchatSend (color: `YELLOW`, notify: true,

            message: "STARTED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]` (${env.BUILD_URL})"

          )

 

        // send to email

        emailext (

            subject: "STARTED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]`",

            body: """<p>STARTED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]`:</p>

              <p>Check console output at &QUOT;<a href=`${env.BUILD_URL}`>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",

            recipientProviders: [[$class: `DevelopersRecipientProvider`]]

          )

      }

    }

    /* ... unchanged ... */

  }

  post {

    success {

      slackSend (color: `#00FF00`, message: "SUCCESSFUL: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]` (${env.BUILD_URL})")

 

      hipchatSend (color: `GREEN`, notify: true,

          message: "SUCCESSFUL: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]` (${env.BUILD_URL})"

        )

 

      emailext (

          subject: "SUCCESSFUL: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]`",

          body: """<p>SUCCESSFUL: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]`:</p>

            <p>Check console output at &QUOT;<a href=`${env.BUILD_URL}`>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",

          recipientProviders: [[$class: `DevelopersRecipientProvider`]]

        )

    }

 

    failure {

      slackSend (color: `#FF0000`, message: "FAILED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]` (${env.BUILD_URL})")

 

      hipchatSend (color: `RED`, notify: true,

          message: "FAILED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]` (${env.BUILD_URL})"

        )

 

      emailext (

          subject: "FAILED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]`",

          body: """<p>FAILED: Job `${env.JOB_NAME} [${env.BUILD_NUMBER}]`:</p>

            <p>Check console output at &QUOT;<a href=`${env.BUILD_URL}`>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>&QUOT;</p>""",

          recipientProviders: [[$class: `DevelopersRecipientProvider`]]

        )

    }

  }

Blue Ocean Run with Notifications

Search
Sub-Categories
Related Articles

Leave a Comment: