Skip to content

Instantly share code, notes, and snippets.

@ObserverOfTime
Last active December 28, 2020 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ObserverOfTime/b6265136119757c75a932ebb3246dda0 to your computer and use it in GitHub Desktop.
Save ObserverOfTime/b6265136119757c75a932ebb3246dda0 to your computer and use it in GitHub Desktop.
Travis CI Discord webhook
#!/bin/bash
if [[ -z $1 ]]; then
printf '[Webhook]: ERROR! Missing webhook URL argument.\n'
exit 1
fi
if [[ -n $2 ]]; then
LANG_VAL="$2"
LANG_REF="TRAVIS_${2^^}_VERSION"
else
LANG_VAL='Bash'
LANG_REF='BASH_VERSION'
fi
printf '[Webhook]: Sending webhook to Discord...\n'
if ((TRAVIS_TEST_RESULT == 0)); then
STATUS='passed'
COLOR=$((0x2ECC71))
ICON='TravisCI-Mascot-grey'
else
STATUS='failed'
COLOR=$((0xE74C3C))
ICON='TravisCI-Mascot-red'
fi
SLUG="${TRAVIS_PULL_REQUEST_REPO_SLUG:-$TRAVIS_REPO_SLUG}"
COMMIT="${TRAVIS_PULL_REQUEST_SHA:-$TRAVIS_COMMIT}"
BRANCH="${TRAVIS_PULL_REQUEST_BRANCH:-$TRAVIS_BRANCH}"
URL="https://github.com/$SLUG/commit/$COMMIT"
TREE="https://github.com/$SLUG/tree/$BRANCH"
COMMIT_SUBJECT="$(git log -1 "$COMMIT" --pretty='%s')"
COMMIT_MESSAGE="$(git log -1 "$COMMIT" --pretty='%b')"
COMMITTER_NAME="$(git log -1 "$COMMIT" --pretty='%cN')"
AVATAR="TravisCI-Mascot-$((RANDOM % 4 + 1))"
WEBHOOK_DATA=$'{
"username": "Travis CI",
"avatar_url": "https://travis-ci.com/images/logos/'$AVATAR'.png",
"embeds": [{
"color": "'$COLOR'",
"author": {
"name": "'$TRAVIS_REPO_SLUG' #'$TRAVIS_JOB_NUMBER'",
"url": "'$TRAVIS_JOB_WEB_URL'",
"icon_url": "https://travis-ci.com/images/logos/'$ICON'.png"
},
"title": "'$COMMIT_SUBJECT'",
"url": "'$URL'",
"description": "'${COMMIT_MESSAGE//$'\n'/$'\\n'}'",
"footer": {
"text": "'$COMMITTER_NAME'",
"icon_url": "https://github.com/'$COMMITTER_NAME'.png?s=40"
},
"fields": [{
"name": "Status",
"value": "'$STATUS'",
"inline": true
}, {
"name": "Branch",
"value": "[`'${BRANCH}'`]('$TREE')",
"inline": true
}, {
"name": "'$LANG_VAL'",
"value": "'${!LANG_REF:-N/A}'",
"inline": true
}],
"timestamp": "'$(date --utc +%FT%TZ)'"
}]
}'
GIST='https://gist.github.com/ObserverOfTime/b6265136119757c75a932ebb3246dda0'
if curl -Sf "$1" -d "$WEBHOOK_DATA" \
-A "TravisCI-Webhook ($GIST, v0.2)" \
-H 'Content-Type: application/json'; then
printf '[Webhook]: Successfully sent the webhook.\n'
else
ecode=$?
printf '[Webhook]: Failed to send the webhook.\n'
exit $ecode
fi
@ObserverOfTime
Copy link
Author

The script expects 1 or 2 arguments:

  • The URL of the Discord webhook. (required)
  • One of the following depending on the build language:
    • Dart
    • Go
    • Haxe
    • JDK
    • Julia
    • Node
    • OTP
    • Perl
    • PHP
    • Python
    • R
    • Ruby
    • Rust
    • Scala
    • MariaDB

Here's an example:

language: python
# ...
after_script:
  - curl -LSs https://git.io/travis-ci-webhook.sh | bash -s -- "$WEBHOOK_URL" Python

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment