Skip to content

Commit

Permalink
Merge pull request #8 from nextstrain/notify-slack
Browse files Browse the repository at this point in the history
Add simple Slack notifications
  • Loading branch information
joverlee521 authored Jul 26, 2023
2 parents a77e727 + b2a0de7 commit 9082700
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ approach to "ingest" has been discussed in various internal places, including:

Scripts for supporting ingest workflow automation that don’t really belong in any of our existing tools.

- [notify-on-job-fail](notify-on-job-fail) - Send Slack message with details about failed workflow job on GitHub Actions and/or AWS Batch
- [notify-on-job-start](notify-on-job-start) - Send Slack message with details about workflow job on GitHub Actions and/or AWS Batch
- [notify-slack](notify-slack) - Send message or file to Slack
- [s3-object-exists](s3-object-exists) - Used to prevent 404 errors during S3 file comparisons in the notify-* scripts
- [trigger](trigger) - Triggers downstream GitHub Actions via the GitHub API using repository_dispatch events.

Expand Down
23 changes: 23 additions & 0 deletions notify-on-job-fail
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"
27 changes: 27 additions & 0 deletions notify-on-job-start
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"
56 changes: 56 additions & 0 deletions notify-slack
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

0 comments on commit 9082700

Please sign in to comment.