How To Post Jenkins Pipeline Results from the Cycle Appliance to Slack

How To Post Jenkins Pipeline Results from the Cycle Appliance to Slack

The Cycle Appliance provides a platform that facilitates continuous testing and continuous integration through cloud-based infrastructure running Jenkins in Azure. A key aspect of continuous testing and continuous integration is giving your developers and testers notifications on the status of test and integration builds. This allows teams to stay up-to-date on the stability of their executions and allows faster recovery times when something goes wrong.

For many teams, Slack is a primary source for communication. Jenkins pipelines provide the ability to send messages to Slack with relevant info on a complete execution. This article describes an example of configuring Jenkins to send a detailed message to Slack at the end of a Pipeline run.

 

Example Scenario

For the example in this article, we will assume there are tests associated with existing functionality that have already been deployed. Those tests are on a branch of a code repository.

Upon the first pipeline execution, a test case fails and Jenkins posts a message to a channel in Slack detailing the failure and respective commit. After rectifying the issue, a subsequent pipeline execution passes and Jenkins posts a message to Slack confirming the passing tests.

 

Jenkins Pipeline Syntax

We will be using several options available to us in Jenkinsfile syntax to drive our pipeline test execution. Detailed information on Jenksinfile pipeline syntax and usage can be found here.

 

BUILD_URL Environment Variable

The Jenkins environment variable BUILD_URL will provide a link in Slack to the pipeline build. This BUILD_URL variable is set by Jenkins and matches the URL of the pipeline execution in the Jenkins web client.

 

BRANCH_NAME Environment Variable

The Jenkins environment variable BRANCH_NAME will specify in Slack what branch the pipeline executed. This BRANCH_NAME variable is set by Jenkins and matches the branch name checked out from the code repository for that build execution.

 

post and success/failure

The post section of a pipeline always runs at the end of the pipeline’s execution. This is where posting results typically happens. Inside the post section, different actions can be taken based on the success or failure status of the test execution. The post section also supports other status blocks, such as an always block to run a script every time at the end of an execution.

 

httpRequest

The HTTP Request Plugin, which is installed with the Cycle Appliance, provides the ability to send an HTTP request inside of a script block. This is a key part of being able to post messages to Slack in the pipeline.

 

Example Post Syntax

post {
  success {
    script {
       println "All the tests in ${BRANCH_NAME} passed."
    }
  }
  failure {
       println "There are some failing tests on ${BRANCH_NAME}."
  }
}

In the example above, the post block will always run after the completion of preceding stages in the pipeline. If those stages passed, the script in the success block will print out that all the tests passed; however, if the preceding stages failed then the script in the failure block will print out that some tests are failing. 

 

 

Sending Helpful Slack Messages in the Pipeline

Expanding upon the example above, a host of other actions can be taken inside of the success and failure blocks. For the purpose of sending a Slack update, the HTTP Request plugin is employed.

 

Basics of Sending Messages

The HTTP Request can take a number of different options, as defined in the plugin’s documentation. The vital options for sending a Slack message are shown in the following snippet:

httpRequest consoleLogResponseBody: true,

         contentType: 'APPLICATION_JSON',

         httpMode: 'POST',

         requestBody: "{ \"text\" : \"${message}\" }",

         url: "https://hooks.slack.com/services/YOUR-SLACK-API-KEY"

 

The contentType and httpMode must always be ‘APPLICATION_JSON’ and ‘POST’ respectively. The requestBody is where the message to be printed out in Slack is written. Finally, the url is the specific url with the API key that Slack provides.

To learn more about getting a Slack API key and details on the Slack’s options, see the “Sending messages” section of Slack’s documentation.

 

Creating A Helpful Message

While even sending a basic Slack message that simply notifies tests are passing or failing is helpful, more details can be added to the messages to better disseminate information: 

last_commit = powershell(returnStdout: true, script: 'git log -1 --pretty="%h (%an): %B"').trim()

     message = message + "Testing Status: PASSED\n"

     message = message + "Build URL: ${BUILD_URL}\n"

     message = message + "Branch: ${BRANCH_NAME}\n"

     message = message + "Last Commit Information:\n"

     message = message + last_commit + "\n"

By using these Jenkins environment variables, the message can contain the repository’s branch name and the url to the specific test run for quick access to the respective build. Using a Powershell script to call ‘git log’, the commit message from the most recent commit can be added so that testers and developers can quickly diagnose exactly what change may have caused the issue. 

Slack_Message.png

 

    • Related Articles

    • How To Update Jira Statuses With Jenkins Pipeline In Cycle Appliance

      The Cycle Appliance provides a platform that facilitates continuous testing and continuous integration through cloud-based infrastructure running Jenkins in Azure. A key aspect of continuous testing and continuous integration is giving your ...
    • How to manage Multi-Branch Pipeline Testing with the Cycle Appliance

      The Cycle Appliance provides a platform that facilitates continuous testing and continuous integration through cloud-based infrastructure running Jenkins in Azure. A key aspect of continuous testing and continuous integration is giving your ...
    • How to checkout or relinquish a license with cycle-cli

      (This article is applicable to Cycle 2.5.0 and earlier versions) Cycle-cli is the command line interface for executing Features, Playlists or Group test files. For more detailed information on cycle-cli read this article. This article focuses on ...
    • How to use Scenario tag filters

      Cycle 2.5 introduces the ability to filter tags during Feature and Playlist execution. This allows the Cycle user more control over which Scenarios within a Feature or Playlist are executed while also allowing more flexibility when assigning tags to ...
    • How to perform basic math in Cycle using Groovy

      Problem Cycle can increment variable values but does not include steps for simple math Solution Cycle now includes steps to execute simple Groovy statements inline as well as execute complex Groovy scripts.  The 2 Cycle steps that execute Groovy ...