From 945b3f8fbf40f43723aa021799fadd8f3449b06a Mon Sep 17 00:00:00 2001 From: stasatdaglabs <39559713+stasatdaglabs@users.noreply.github.com> Date: Sun, 5 May 2019 17:12:55 +0300 Subject: [PATCH] [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. --- deploy.sh | 29 ++++++++++++++++++++++------- telegram.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 7 deletions(-) create mode 100755 telegram.sh diff --git a/deploy.sh b/deploy.sh index 76fd929bc..1b91d26e5 100755 --- a/deploy.sh +++ b/deploy.sh @@ -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 diff --git a/telegram.sh b/telegram.sh new file mode 100755 index 000000000..6d9de0ac1 --- /dev/null +++ b/telegram.sh @@ -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" \ No newline at end of file