slackSend – Notify test results to slack with ease

slackSend is a method provided by the Slack Notification Plugin for Jenkins. It allows Jenkins to send messages and notifications to Slack channels.

This step is commonly used to keep teams informed about the status of Jenkins builds, deployments, or any other important events in the CI/CD process. A QA can utilise this to notify the test results from Jenkins to their team’s slack channel.

image showing a notification sent to slack by slackSend

Pre-requisite for slackSend

  1. In Jenkins, navigate to ‘Manage Jenkins’ > ‘Configure System.’
  2. Scroll down to the ‘Global Slack Notifier Settings’ section.
  3. Enter your Slack workspace details, including the Slack team domain and authentication token.
  4. Save the configuration.

Example of Basic slackSend

slackSend(
    color: '#00FF00',
    message: "E2E Test Execution",
    channel: '#your-slack-channel'
)

A sample Jenkins Pipeline

pipeline {
	agent {
        docker {
            image '<docker-image>'
        }
    }
	parameters {
		choice(choices: ['sandbox','qa','uat'], name: 'ENVIRONMENT')
		string(defaultValue: "master", name: 'BRANCH')
	}
	
	stages {
		stage('Compile') {
			steps{
				sh ''npm install'
			}
		}
		stage('Test') {
			steps  {
                         sh 'npm run test'
					}
		}
	}
	post{
		always {
			script{
				slackSend(color: 'green', message: "E2E test executed on ${ENVIRONMENT} | Test Result: ${currentBuild.currentResult} | Build# ${env.BUILD_URL}", channel: "#team-x")
			}
		}
	}
}

slackSend using Blocks for better formatting

def sendSlackNotification() {

    def messageBlocks = [
        [
            type: 'section',
            text: [
                type: 'mrkdwn',
                text: "*E2E Tests Report*",
            ]
        ],
        [
            type: 'section',
            text: [
                type: 'mrkdwn',
                text: "*Job Details:* <${buildUrl}|${env.JOB_NAME} ${env.BUILD_NUMBER}>",
            ],
        ],
        [
            type: 'section',
            text: [
                type: 'mrkdwn',
                text: "*Test Execution Status:* <${currentBuild.currentResult}>",
            ],
        ],
    ]

    slackSend(
        color: slackColor,
        blocks: messageBlocks,
        channel: '#your-slack-channel',
        teamDomain: 'your-slack-workspace-domain',
        authToken: 'your-slack-auth-token'
    )
}

Various Jenkins variables available to use in sendSlack

  • ${currentBuild.currentResult} – To fetch the current build result.
  • ${env.JOB_NAME} – To fetch the Jenkins job name.
  • ${env.BUILD_NUMBER} – To get the build number.

To access the built-in global variables, one can open url ${YOUR_JENKINS_URL}/pipeline-syntax/globals.

Read why selenium is not a good choice for client-side performance testing

About Author