JenkinsMattermost

Post failed Jenkins builds to a Mattermost channel

A Jenkins + Mattermost agent flow

A nightly job goes red at 2am and the team finds out when the morning standup asks why the deploy didn't ship. Surfaced in the channel the moment the agent next polls, a failure gets eyes hours earlier. The agent reads a job's recent builds with jenkins_get_recent_builds, finds the ones that finished as FAILURE or UNSTABLE, and posts each to a self-hosted Mattermost channel with post_message, carrying the job name, the build number, the result, and a link to the console log. Jenkins answers on request, so the agent runs on a short loop and only a build it hasn't already reported produces a message.

The flow

Jenkinsjenkins_get_recent_builds

Gets recent builds for a job with their status and metadata.

Mattermostpost_message

Post a message to a channel.

Step by step

  1. Name the jobs and the channel

    Give the agent the Jenkins job names to watch and a Mattermost channel ID. Decide which results count as worth posting, usually FAILURE and UNSTABLE, leaving SUCCESS quiet.

  2. Read recent builds

    The agent calls jenkins_get_recent_builds per job and reads each build's number, result, and timestamp, keeping the ones that didn't pass.

  3. Post the failures

    For each new failed build it calls post_message with the job, the build number, the result, and the console URL, so anyone in the channel can open the log and start triaging.

  4. Track the last build seen

    Store the highest build number reported per job. On the next loop the agent posts only builds above that mark, so it doesn't re-announce yesterday's failures.

Tell your agent

Every three minutes, get recent builds for the Jenkins jobs nightly-deploy and integration-tests. Post any build that finished as FAILURE or UNSTABLE to the Mattermost channel 'ci-alerts' with the job name, build number, result, and the console log link. Track the last build number per job and don't repost ones already announced.

Setup

This flow needs both servers connected to your agent. Follow each install guide:

Worth knowing

  • post_message takes a Mattermost channel ID, not a name. Resolve it once with get_channel_by_name, reuse the ID, and confirm the bot has joined the channel.
  • jenkins_get_recent_builds can include a build still in progress with a null result. Skip builds that haven't finished, or the agent posts a failure that was never one.
  • For the actual error, jenkins_get_console_log returns the output. Pulling the whole log into chat is noisy, so post the link and let the agent fetch the tail only if asked.

Questions

Can it post the failing test names, not just that the build failed?
Yes, with a second call. jenkins_get_test_results returns pass and fail counts and suites for a build, so the agent can include the failed test names in the message.
Does it fire the instant a build fails?
No. Jenkins responds when the agent polls it rather than pushing, so the message lags by your loop interval. A three-minute loop keeps it close enough for most pipelines.