[NOD-13] Notify failing builds in Telegram (#287)

* [NOD-13] Added notify_telegram to deploy.sh.

* [NOD-13] Made a test fail for testing.

* [NOD-13] Added some temporary logging.

* [NOD-13] Wrote a nice message for the bot to send.

* [NOD-13] Made the message nicer.

* [NOD-13] Made the message nicer still.

* [NOD-13] Added the build log as an attachment.

* [NOD-13] Actually added the build log as an attachment.

* [NOD-13] Added a delay to allow the build log to properly flush.

* [NOD-13] Disowned notify_telegram.

* [NOD-13] Disowning doesn't work. Using the "at" command instead.

* [NOD-13] Properly using the at command.

* [NOD-13] Actually properly using the at command.

* [NOD-13] Added a couple of prints to see whether the script is even being called.

* [NOD-13] More printouts...

* [NOD-13] Added a command to start atd if it stopped for some reason.

* [NOD-13] Added slashes in multiline echo command.

* [NOD-13] Added quotes where required and removed debug comments.

* [NOD-13] Revert "[NOD-13] Made a test fail for testing."

This reverts commit 9701e30e

* [NOD-13] Added some comments.
This commit is contained in:
stasatdaglabs 2019-05-05 17:12:55 +03:00 committed by Ori Newman
parent a73f218402
commit 945b3f8fbf
2 changed files with 60 additions and 7 deletions

View File

@ -7,18 +7,33 @@ export IMAGE_TAG=${IMAGE_TAG:-"latest"}
# GIT_COMMIT is set by Jenkins
export COMMIT=${COMMIT:-$GIT_COMMIT}
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-eu-central-1}
export AWS_DEFAULT_REGION
AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output=text)
export AWS_ACCOUNT_ID
ECR_SERVER=${ECR_SERVER:-"$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com"}
export ECR_SERVER
export AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-eu-central-1}
export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output=text)
export ECR_SERVER=${ECR_SERVER:-"$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com"}
CF_PARAM=TaskImage
IMAGE_NAME=${ECR_SERVER}/${SERVICE_NAME}
# Sends a Telegram notification with some details about the failure
# All variables in this function are set by Jenkins
notify_telegram() {
echo "./telegram.sh \
'${TELEGRAM_API_TOKEN}' \
'${TELEGRAM_CHAT_ID}' \
'${BUILD_URL}' \
'${ghprbActualCommitAuthor}' \
'${ghprbPullTitle}' \
'${ghprbPullLink}'" | at -m now + 1 minute
}
trap "exit 1" INT
fatal() { echo "ERROR: $*" >&2; exit 1; }
fatal() {
echo "ERROR: $*" >&2
notify_telegram
exit 1
}
measure_runtime() {
START=$(date +%s)
echo "--> $*" >&2

38
telegram.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/sh
# This file is part of Continuous Integration. When ran by
# the CI agent, it sends a some details about the build failure
# to a Telegram group.
API_TOKEN="$1"
CHAT_ID="$2"
BUILD_URL="$3"
PR_AUTHOR="$4"
PR_TITLE="$5"
PR_LINK="$6"
# Start atd
service atd start
# Build the failure message
MESSAGE="*${PR_AUTHOR}*:
Build *FAILED* for pull request '${PR_TITLE}'
[Github](${PR_LINK}) [Jenkins](${BUILD_URL}console)"
# Send the failure message
curl -s \
-X POST \
"https://api.telegram.org/bot${API_TOKEN}/sendMessage" \
-d chat_id="${CHAT_ID}" \
-d parse_mode=markdown \
-d disable_web_page_preview=true \
-d text="${MESSAGE}"
# Retrieve the build log
LOG=$(curl ${BUILD_URL}consoleText)
# Send the build log
printf "$LOG" | curl \
"https://api.telegram.org/bot${API_TOKEN}/sendDocument" \
-F chat_id="${CHAT_ID}" \
-F document="@-;filename=build.log"