circleci pipeline failed notification in google chat (aka gchat, hangouts)

March 24th, 2023

.circleci/config.yml

workflows:

main:

jobs:

- xxx

- yyy:

requires:

- xxx

 

commands:

notify_failure:

description: Notify our gchat channel about the failure

steps:

- run:

name: Notify failure

command: ./.circleci/scripts/notify_chat_about_pipeline_failure.sh

when: "on_fail"

 

jobs:

xxx:

steps:

- run: echo "step 1"

- run: echo "step 2"

- notify_failure

yyy:

steps:

- run: echo "step a"

- run: echo "step b"

- notify_failure

 

.circleci/scripts/notify_chat_about_pipeline_failure.sh

#!/usr/bin/env bash

 

 

message="pipeline failed:

branch '$CIRCLE_BRANCH'

job: $CIRCLE_JOB

committer: @${CIRCLE_USERNAME}

url: $CIRCLE_BUILD_URL

"

 

 

curl \

--request POST \

--header "Content-Type: application/json" \

--data "{\"text\":\"$message\"}" \

"$CHAT_WEBHOOK_URL"

 

 

 

you can get the webhook url in google chat by

gchat channel -> Apps & Integrations -> manage webhooks

https://chat.googleapis.com/v1/spaces/ZZZ/messages

?key=xxx

&token=yyy

 

&threadKey=pipeline-notifications

if you want all notification in the same thread

add a freely chosen value for "threadKey" (eg. pipeline-notifications)

 


 

problem: sequential jobs

If you are running your jobs sequentially it is not possible to run a final job unconditionally.

https://discuss.circleci.com/t/how-to-always-run-a-sequential-final-job-e-g-slack-notification/35362

 

solution: add the notify_failure at the end of EVERY job's steps list

 


 

inspiration: