Created
April 26, 2020 20:14
-
-
Save JakeSteam/671658a8654b0ab19b61cfa9e9c100c9 to your computer and use it in GitHub Desktop.
Posting a Slack message from Travis CI
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
# Environment variables | |
REPO=${TRAVIS_REPO_SLUG} | |
BRANCH=${TRAVIS_BRANCH} | |
COMMIT_HASH=${TRAVIS_COMMIT} | |
COMMIT_HASH_SHORT=${COMMIT_HASH:0:7} | |
COMMIT_MESSAGE=${TRAVIS_COMMIT_MESSAGE} | |
IS_PULL_REQUEST=${TRAVIS_PULL_REQUEST} | |
BUILD_DIR=${TRAVIS_BUILD_DIR} | |
BUILD_NUMBER=${TRAVIS_BUILD_NUMBER} | |
BUILD_URL=${TRAVIS_BUILD_WEB_URL} | |
function sendAlertToSlack { | |
if (($# != 3)); then echo "Please pass webhook, download URL, and build number!"; exit 2; fi | |
SLACK_WEBHOOK=$1 | |
DOWNLOAD_URL=$2 | |
DEBUGGABLE=$3 | |
HTTP_RESPONSE=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" \ | |
--header "Content-type: application/json" \ | |
--request POST \ | |
--data \ | |
"{ | |
\"blocks\":[ | |
{ | |
\"type\":\"section\", | |
\"text\":{ | |
\"type\":\"mrkdwn\", | |
\"text\":\"*$BRANCH build (#$BUILD_NUMBER) available on devices!*\n*Message:* $COMMIT_MESSAGE\" | |
} | |
}, | |
{ | |
\"type\":\"section\", | |
\"fields\":[ | |
{ | |
\"type\":\"mrkdwn\", | |
\"text\":\"*Debuggable:*\n$DEBUGGABLE\" | |
}, | |
{ | |
\"type\":\"mrkdwn\", | |
\"text\":\"*Hash:*\n$COMMIT_HASH_SHORT\" | |
} | |
] | |
}, | |
{ | |
\"type\":\"actions\", | |
\"elements\":[ | |
{ | |
\"type\":\"button\", | |
\"text\":{ | |
\"type\":\"plain_text\", | |
\"text\":\"Install\" | |
}, | |
\"value\":\"internal-app-sharing\", | |
\"url\":\"$DOWNLOAD_URL\" | |
}, | |
{ | |
\"type\":\"button\", | |
\"text\":{ | |
\"type\":\"plain_text\", | |
\"text\":\"Help\" | |
}, | |
\"value\":\"help\", | |
\"url\":\"https://support.google.com/googleplay/android-developer/answer/9303479#on\" | |
}, | |
{ | |
\"type\":\"button\", | |
\"text\":{ | |
\"type\":\"plain_text\", | |
\"text\":\"Travis\" | |
}, | |
\"value\":\"travis\", | |
\"url\":\"$BUILD_URL\" | |
}, | |
{ | |
\"type\":\"button\", | |
\"text\":{ | |
\"type\":\"plain_text\", | |
\"text\":\"GitHub\" | |
}, | |
\"value\":\"github\", | |
\"url\":\"https://github.com/$REPO/commit/$COMMIT_HASH\" | |
} | |
] | |
} | |
] | |
}" \ | |
"$SLACK_WEBHOOK") | |
HTTP_BODY=$(echo ${HTTP_RESPONSE} | sed -e 's/HTTPSTATUS\:.*//g') | |
HTTP_STATUS=$(echo ${HTTP_RESPONSE} | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') | |
if [[ ${HTTP_STATUS} != 200 ]]; then | |
echo -e "Sending Slack notification failed.\nStatus: $HTTP_STATUS\nBody: $HTTP_BODY\nExiting." | |
exit 1 | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment