Google Chat Notification Jenkins Plugin to send build status to Google Chat.
This Jenkins plugin allows you to send Google Chat notification as a post build action or as a pipeline script.
Install this plugin on your Jenkins server:
Manage Jenkins
Manage Plugins
,Available
,google-chat-notification
,googlechatnotification url: 'web hook(s) URL(s)', message: 'message to be sent'
googlechatnotification url: 'web hook(s) URL(s)', message: 'message to be sent', messageFormat: 'simple|card', sameThreadNotification: 'true', threadKey: '', notifyAborted: 'true', notifyFailure: 'true', notifyNotBuilt: 'true', notifySuccess: 'true', notifyUnstable: 'true', notifyBackToNormal: 'true', notifySingleFailure: 'false', notifyRepeatedFailure: 'false', suppressInfoLoggers: 'true'
url
This is a mandatory String parameter.
Single/multiple comma separated HTTP URLs or/and single/multiple comma separated Credential IDs.
Different Sample Ways to define URL parameter:
message
messageFormat
simple
.card
is provided as value, the parameter message
must be a valid JSON configuration for card message.sameThreadNotification
card
and the provided JSON contains a 'thread' key, this parameter will be ignored.threadKey
JOB_NAME
as threadKey.card
and the provided JSON contains a 'thread' key, this parameter will be ignored.notifyAborted
notifyFailure
notifyNotBuilt
notifySuccess
notifyUnstable
notifyBackToNormal
notifySingleFailure
notifyRepeatedFailure
suppressInfoLoggers
Default behaviour of plugin is to send notifications for all build status unless overridden with true value for above defined build statuses.
You can expose Git commit info in your pipeline with something like this:
environment {
GIT_LAST_AUTHOR = sh(script: 'git --no-pager show -s --format=\'%an\' $GIT_COMMIT', returnStdout: true).trim()
GIT_LAST_COMMIT = sh(script: 'git log -1 --pretty=\'%B\'', returnStdout: true).trim()
}
googlechatnotification url: 'web hook(s) URL(s)',
message: "${env.JOB_NAME} : Build #${env.BUILD_NUMBER} - ${currentBuild.currentResult}: Check output at ${env.BUILD_URL}"
googlechatnotification url: 'web hook(s) URL(s)',
message: "Build ${currentBuild.currentResult}:\n Job ${env.JOB_NAME}\n build ${env.BUILD_NUMBER}\n last commit ```${env.GIT_LAST_COMMIT}```\n author *${env.GIT_LAST_AUTHOR}*\n Full details click on link: ${env.BUILD_URL}"
UNTABLE
or worseString buildResult = currentBuild.currentResult
String buildDetails = currentBuild.resultIsWorseOrEqualTo("UNSTABLE")
? "\nLast commit author: *${env.GIT_LAST_AUTHOR}*\n ```${env.GIT_LAST_COMMIT}```"
: '';
googlechatnotification url: 'web hook(s) URL(s)',
message: "*${env.JOB_NAME}* - Build ${env.BUILD_ID} (<${env.BUILD_URL}|Details>) ${currentBuild.description} ${buildDetails}"
SUCCESS
, UNSTABLE
and FAILURE
)String buildResult = currentBuild.currentResult
def statusIcons = [SUCCESS: '\\u2714', UNSTABLE: '\\u26a0', FAILURE: '\\u274c']
def colors = [SUCCESS: '#5DBCD2', UNSTABLE: '#aca620', FAILURE: '#ff0000']
def buildStatusIcon = statusIcons[buildResult] ?: '\\u1F648'
def buildStatusWithColor = "<font color=\"${colors[buildResult] ?: ''}\">${currentBuild.currentResult}</font>"
googlechatnotification url: 'web hook(s) URL(s)',
message: "${buildStatusIcon} ${buildStatusWithColor}: *${env.JOB_NAME}* - Build ${env.BUILD_ID} (<${env.BUILD_URL}|Details>)"
google-chat-build-notification.json
{
"cardsV2":[
{
"cardId":"unique-card-id",
"card":{
"header":{
"title":"${JOB_NAME}",
"subtitle":"Build ${BUILD_ID}",
"imageUrl":"https://developers.google.com/chat/images/quickstart-app-avatar.png",
"imageType":"CIRCLE"
},
"sections":[
{
"header":"${BUILD_STATUS}",
"collapsible":true,
"uncollapsibleWidgetsCount":1,
"widgets":[
{
"textParagraph":{
"text":"Click <a href=\"${BUILD_URL}\">here</a> for more info"
}
},
{
"divider":{}
},
{
"decoratedText":{
"icon":{
"knownIcon":"PERSON"
},
"topLabel":"Last commit",
"text":"<i>${GIT_LAST_COMMIT}</i>",
"bottomLabel":"Author: ${GIT_LAST_AUTHOR}"
}
}
]
}
]
}
}
]
}
See Format a card message for instructions on how to format text in a card message.
Pipeline
// read from workspace
def cardConfig = readJSON file: 'google-chat-build-notification.json'
googlechatnotification url: 'web hook(s) URL(s)', messageFormat: 'card', message: cardConfig.toString()
// read from global config file
configFileProvider([configFile(fileId: '9d792a84-6224-4529-aa30-2296e97df64e', targetLocation: 'google-chat-build-notification.json')]) {
def cardConfig = readJSON file: 'google-chat-build-notification.json'
googlechatnotification url: 'web hook(s) URL(s)', messageFormat: 'card', message: cardConfig.toString()
}
Use the syntax <users/{GOOGLE_CHAT_USER_ID}>
in a message to mention users directly. See Google Chat User ID for tips on how to obtain a User ID.
When testing the connection, you may see errors like:
WARNING j.p.googlechat.StandardGoogleChatService#publish: Invalid Google Chat Notification URL found: xxx
There's a couple of things to try:
Add a log recorder for the StandardGoogleChatService class this should give you additional details on what's going on.
If you still can't figure it out please raise an issue with as much information as possible about your config and any relevant logs.
Install Maven and JDK.
$ mvn -version | grep -v home
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T08:41:47-08:00)
Java version: 1.7.0_79, vendor: Oracle Corporation
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.4.0-65-generic", arch: "amd64", family: "unix"
Run build and create an HPI file to install in Jenkins (HPI file will be in
target/google-chat-notification.hpi
).
mvn clean verify
Please report issues and enhancements through the Jenkins issue tracker.