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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.