Skip to content

Instantly share code, notes, and snippets.

@yuna0x0
Last active May 9, 2018 01:01
Show Gist options
  • Save yuna0x0/b37dba1026b7bcbb4c0a1ad67753e25d to your computer and use it in GitHub Desktop.
Save yuna0x0/b37dba1026b7bcbb4c0a1ad67753e25d to your computer and use it in GitHub Desktop.
Travis CI Discord Webhook
#!/bin/bash
if [ -z "$2" ]; then
echo -e "WARNING!!\nYou need to pass the DISCORD_WEBHOOK_URL environment variable as the second argument to this script." && exit
fi
echo -e "[Discord Webhook]: Sending webhook to Discord...\\n";
case $1 in
"success" )
EMBED_COLOR=3066993
STATUS_MESSAGE="Passed"
;;
"failure" )
EMBED_COLOR=15158332
STATUS_MESSAGE="Failed"
;;
* )
EMBED_COLOR=0
STATUS_MESSAGE="Status Unknown"
;;
esac
AUTHOR_NAME="$(git log -1 "$TRAVIS_COMMIT" --pretty="%aN")"
COMMITTER_NAME="$(git log -1 "$TRAVIS_COMMIT" --pretty="%cN")"
COMMIT_SUBJECT="$(git log -1 "$TRAVIS_COMMIT" --pretty="%s")"
COMMIT_MESSAGE="$(git log -1 "$TRAVIS_COMMIT" --pretty="%b")"
if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then
CREDITS="$AUTHOR_NAME authored & committed"
else
CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed"
fi
if [[ $TRAVIS_PULL_REQUEST != false ]]; then
URL="https://github.com/$TRAVIS_REPO_SLUG/pull/$TRAVIS_PULL_REQUEST"
else
URL=""
fi
TIMESTAMP=$(date -u +%FT%TZ)
DISCORD_WEBHOOK_DATA='{
"username": "Travis CI",
"avatar_url": "https://cdn.edisonlee55.com/resources/images/TravisCI-Mascot-1.png",
"embeds": [ {
"color": '$EMBED_COLOR',
"author": {
"name": "Job #'"$TRAVIS_JOB_NUMBER"' (Build #'"$TRAVIS_BUILD_NUMBER"') '"$STATUS_MESSAGE"' - '"$TRAVIS_REPO_SLUG"'",
"url": "https://travis-ci.org/'"$TRAVIS_REPO_SLUG"'/builds/'"$TRAVIS_BUILD_ID"'"
},
"title": "'"$COMMIT_SUBJECT"'",
"url": "'"$URL"'",
"description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'",
"fields": [
{
"name": "Commit",
"value": "'"[\`${TRAVIS_COMMIT:0:7}\`](https://github.com/$TRAVIS_REPO_SLUG/commit/$TRAVIS_COMMIT)"'",
"inline": true
},
{
"name": "Branch/Tag",
"value": "'"[\`$TRAVIS_BRANCH\`](https://github.com/$TRAVIS_REPO_SLUG/tree/$TRAVIS_BRANCH)"'",
"inline": true
}
],
"timestamp": "'"$TIMESTAMP"'"
} ]
}'
(curl --fail --progress-bar -A "TravisCI-Webhook" -H Content-Type:application/json -H X-Author:edisonlee55#0001 -d "$DISCORD_WEBHOOK_DATA" "$2" \
&& echo -e "\\n[Discord Webhook]: Successfully sent the webhook.") || echo -e "\\n[Discord Webhook]: Unable to send webhook."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment