Jenkins deployment notifications
Post Jenkins build and deployment results into a TaskForce chat channel with an incoming chat webhook and a ready-made Jenkinsfile.
Jenkins can post the result of every build into a TaskForce chat channel, so your team sees deployments succeed or fail where they already talk. It uses an incoming chat webhook, so TaskForce needs no Jenkins plugin and Jenkins needs no TaskForce account.
Before you start
- A TaskForce role with the channel management permission.
- A Jenkins server with the HTTP Request plugin installed.
- A Jenkins admin or job owner who can add credentials.
Create the webhook
- In TaskForce, open Chat and then the channel that should receive the messages. It must be a regular channel, not a direct message.
- Choose Webhooks in the channel header.
- Enter a name such as
Jenkins(it is shown as the sender of the messages) and click Create webhook. - Copy the secret now: it is shown only once. The URL stays visible in the webhook list. If you lose the secret, use Rotate secret to get a new one.
Store the secret in Jenkins
- In Jenkins, open Manage Jenkins > Credentials and add a credential of kind Secret text.
- Paste the webhook secret as the secret.
- Set the ID to
taskforce-chat-webhook. The Jenkinsfile below looks the secret up by this ID.
Never put the secret in the Jenkinsfile or in your repository.
Add the Jenkinsfile
Paste this into your pipeline and replace https://your-taskforce-host/api/webhooks/chat/WEBHOOK_ID with the URL you copied. The webhook window in TaskForce shows the same snippet with your URL already filled in.
// TaskForce chat notification for Jenkins (declarative pipeline).
// 1. Install the "HTTP Request" plugin.
// 2. Add the webhook secret as a "Secret text" credential with the id taskforce-chat-webhook.
def notifyTaskForce(String status) {
try {
withCredentials([string(credentialsId: 'taskforce-chat-webhook', variable: 'TF_WEBHOOK_SECRET')]) {
def body = groovy.json.JsonOutput.toJson([
title : "${env.JOB_NAME} #${env.BUILD_NUMBER}".toString(),
status : status,
message : "Deployment ${status}: ${env.BUILD_URL}".toString(),
metadata: [duration: currentBuild.durationString]
])
httpRequest httpMode: 'POST',
url: 'https://your-taskforce-host/api/webhooks/chat/WEBHOOK_ID',
contentType: 'APPLICATION_JSON',
customHeaders: [[name: 'X-Webhook-Secret', value: TF_WEBHOOK_SECRET, maskValue: true]],
requestBody: body,
validResponseCodes: '201'
}
} catch (err) {
// A failed notification must not change the build result.
echo "TaskForce notification failed: ${err}"
}
}
pipeline {
agent any
stages {
stage('Deploy') {
steps {
echo 'Your deployment steps go here'
}
}
}
post {
success { notifyTaskForce('success') }
failure { notifyTaskForce('failure') }
unstable { notifyTaskForce('unstable') }
aborted { notifyTaskForce('aborted') }
}
}
It notifies on success, failure, unstable and aborted builds. If you already have a pipeline, keep your own stages and copy only the notifyTaskForce function and the post block.
What appears in the channel
The message is posted as the webhook’s name. For a successful build it looks like this:
my-app/main #482
Status: success
Deployment success: https://jenkins.example.com/job/my-app/job/main/482/
Metadata:
duration: 3 min 12 sec
The first line is the title, then Status:, then the message, then a Metadata: heading with one key: value line per entry.
Scripted pipelines and freestyle jobs
Any job that can send an HTTP request can post to the webhook. In a scripted pipeline, call the same notifyTaskForce function from a try/catch/finally. In a freestyle job, add an Execute shell build step (or a post-build step) with plain curl, using the secret bound as an environment variable:
curl -X POST "https://your-taskforce-host/api/webhooks/chat/WEBHOOK_ID" \
-H "Content-Type: application/json" \
-H "X-Webhook-Secret: $TF_WEBHOOK_SECRET" \
-d '{"title":"'"$JOB_NAME"' #'"$BUILD_NUMBER"'","status":"success","message":"Deployed: '"$BUILD_URL"'"}'
Bind the credential under Build Environment > Use secret text(s) or file(s) and name the variable TF_WEBHOOK_SECRET. The full request format is on the Webhooks API page.
Troubleshooting
| Status | Meaning | Fix |
|---|---|---|
| 401 | The X-Webhook-Secret header is missing or wrong |
Check the credential ID and value. If the secret is lost, rotate the webhook and update the credential. |
| 403 | The webhook was revoked | Rotate it in the channel’s Webhooks window. Rotating turns it back on and gives a new secret. |
| 404 | The webhook id in the URL is unknown | Copy the URL again from the Webhooks window. |
| 422 | The payload is invalid, or the formatted message is over 2000 characters | Send a non-empty message and keep titles and metadata short. |
| 429 | More than 60 requests in a minute for this webhook | Send fewer notifications, for example only on the final result. |
Because the Jenkinsfile never fails the build on a notification error, look for TaskForce notification failed in the build log to see the cause.