-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from nextstrain/notify-slack
Add simple Slack notifications
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
: "${SLACK_TOKEN:?The SLACK_TOKEN environment variable is required.}" | ||
: "${SLACK_CHANNELS:?The SLACK_CHANNELS environment variable is required.}" | ||
|
||
: "${AWS_BATCH_JOB_ID:=}" | ||
: "${GITHUB_RUN_ID:=}" | ||
|
||
bin="$(dirname "$0")" | ||
job_name="${1:?A job name is required as the first argument}" | ||
github_repo="${2:?A GitHub repository with owner and repository name is required as the second argument}" | ||
|
||
echo "Notifying Slack about failed ${job_name} job." | ||
message="❌ ${job_name} job has FAILED 😞 " | ||
|
||
if [[ -n "${AWS_BATCH_JOB_ID}" ]]; then | ||
message+="See AWS Batch job \`${AWS_BATCH_JOB_ID}\` (<https://console.aws.amazon.com/batch/v2/home?region=us-east-1#jobs/detail/${AWS_BATCH_JOB_ID}|link>) for error details. " | ||
elif [[ -n "${GITHUB_RUN_ID}" ]]; then | ||
message+="See GitHub Action <https://github.com/${github_repo}/actions/runs/${GITHUB_RUN_ID}?check_suite_focus=true|${GITHUB_RUN_ID}> for error details. " | ||
fi | ||
|
||
"$bin"/notify-slack "$message" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
: "${SLACK_TOKEN:?The SLACK_TOKEN environment variable is required.}" | ||
: "${SLACK_CHANNELS:?The SLACK_CHANNELS environment variable is required.}" | ||
|
||
: "${AWS_BATCH_JOB_ID:=}" | ||
: "${GITHUB_RUN_ID:=}" | ||
|
||
bin="$(dirname "$0")" | ||
job_name="${1:?A job name is required as the first argument}" | ||
github_repo="${2:?A GitHub repository with owner and repository name is required as the second argument}" | ||
build_dir="${3:-ingest}" | ||
|
||
echo "Notifying Slack about started ${job_name} job." | ||
message="${job_name} job has started." | ||
|
||
if [[ -n "${GITHUB_RUN_ID}" ]]; then | ||
message+=" The job was submitted by GitHub Action <https://github.com/${github_repo}/actions/runs/${GITHUB_RUN_ID}?check_suite_focus=true|${GITHUB_RUN_ID}>." | ||
fi | ||
|
||
if [[ -n "${AWS_BATCH_JOB_ID}" ]]; then | ||
message+=" The job was launched as AWS Batch job \`${AWS_BATCH_JOB_ID}\` (<https://console.aws.amazon.com/batch/v2/home?region=us-east-1#jobs/detail/${AWS_BATCH_JOB_ID}|link>)." | ||
message+=" Follow along in your local clone of ${github_repo} with: "'```'"nextstrain build --aws-batch --no-download --attach ${AWS_BATCH_JOB_ID} ${build_dir}"'```' | ||
fi | ||
|
||
"$bin"/notify-slack "$message" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/bin/bash | ||
set -euo pipefail | ||
|
||
: "${SLACK_TOKEN:?The SLACK_TOKEN environment variable is required.}" | ||
: "${SLACK_CHANNELS:?The SLACK_CHANNELS environment variable is required.}" | ||
|
||
upload=0 | ||
output=/dev/null | ||
thread_ts="" | ||
broadcast=0 | ||
args=() | ||
|
||
for arg; do | ||
case "$arg" in | ||
--upload) | ||
upload=1;; | ||
--output=*) | ||
output="${arg#*=}";; | ||
--thread-ts=*) | ||
thread_ts="${arg#*=}";; | ||
--broadcast) | ||
broadcast=1;; | ||
*) | ||
args+=("$arg");; | ||
esac | ||
done | ||
|
||
set -- "${args[@]}" | ||
|
||
text="${1:?Some message text is required.}" | ||
|
||
if [[ "$upload" == 1 ]]; then | ||
echo "Uploading data to Slack with the message: $text" | ||
curl https://slack.com/api/files.upload \ | ||
--header "Authorization: Bearer $SLACK_TOKEN" \ | ||
--form-string channels="$SLACK_CHANNELS" \ | ||
--form-string title="$text" \ | ||
--form-string filename="$text" \ | ||
--form-string thread_ts="$thread_ts" \ | ||
--form file=@/dev/stdin \ | ||
--form filetype=text \ | ||
--fail --silent --show-error \ | ||
--http1.1 \ | ||
--output "$output" | ||
else | ||
echo "Posting Slack message: $text" | ||
curl https://slack.com/api/chat.postMessage \ | ||
--header "Authorization: Bearer $SLACK_TOKEN" \ | ||
--form-string channel="$SLACK_CHANNELS" \ | ||
--form-string text="$text" \ | ||
--form-string thread_ts="$thread_ts" \ | ||
--form-string reply_broadcast="$broadcast" \ | ||
--fail --silent --show-error \ | ||
--http1.1 \ | ||
--output "$output" | ||
fi |