├── LICENSE ├── README.md ├── get_pull_request_title.rb ├── send.sh └── testing_utils ├── README.md └── local_env_run.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Discord Hooks 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Discord Webhook for Github Actions 2 | ## Setup 3 | 1. In Github secrets, add a `WEBHOOK_URL` variable with the Discord web hook URL 4 | 1. In your Github actions yml file, add this to reference the variable you just created: 5 | - To see a real example, visit [here](https://github.com/unthreaded/git-hooks/blob/92ea6bde348431fbe25d05c33398c969eec5d3ee/.github/workflows/build.yml#L48). 6 | ```yaml 7 | - uses: actions/setup-ruby@v1 8 | - name: Send Webhook Notification 9 | if: always() 10 | env: 11 | JOB_STATUS: ${{ job.status }} 12 | WEBHOOK_URL: ${{ secrets.WEBHOOK_URL }} 13 | HOOK_OS_NAME: ${{ runner.os }} 14 | WORKFLOW_NAME: ${{ github.workflow }} 15 | run: | 16 | git clone https://github.com/DiscordHooks/github-actions-discord-webhook.git webhook 17 | bash webhook/send.sh $JOB_STATUS $WEBHOOK_URL 18 | shell: bash 19 | ``` 20 | 1. Enjoy! 21 | -------------------------------------------------------------------------------- /get_pull_request_title.rb: -------------------------------------------------------------------------------- 1 | require 'net/http' 2 | require 'json' 3 | 4 | if ARGV.length != 1 then 5 | puts "Expected only one argument: ENDPOINT_URL" 6 | exit 1 7 | end 8 | 9 | for i in 0..10 10 | response = Net::HTTP.get_response(URI(ARGV[0])) 11 | if response.code == "200" 12 | puts JSON.parse(response.body)["title"] 13 | exit 0 14 | end 15 | end 16 | 17 | puts "Had an issue calling endpoint...%s %s %s" % [response, response.message, response.body] 18 | exit 1 19 | -------------------------------------------------------------------------------- /send.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This original source of this code: https://github.com/DiscordHooks/travis-ci-discord-webhook 3 | # The same functionality from TravisCI is needed for Github Actions 4 | # 5 | # For info on the GITHUB prefixed variables, visit: 6 | # https://help.github.com/en/articles/virtual-environments-for-github-actions#environment-variables 7 | 8 | AVATAR="https://github.com/actions.png" 9 | 10 | # More info: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameter-Expansion 11 | case ${1,,} in 12 | "success" ) 13 | EMBED_COLOR=3066993 14 | STATUS_MESSAGE="Passed" 15 | ;; 16 | 17 | "failure" ) 18 | EMBED_COLOR=15158332 19 | STATUS_MESSAGE="Failed" 20 | ;; 21 | 22 | * ) 23 | STATUS_MESSAGE="Status Unknown" 24 | EMBED_COLOR=0 25 | ;; 26 | esac 27 | 28 | shift 29 | 30 | if [ $# -lt 1 ]; then 31 | echo -e "WARNING!!\nYou need to pass the WEBHOOK_URL environment variable as the second argument to this script.\nFor details & guide, visit: https://github.com/DiscordHooks/github-actions-discord-webhook" && exit 32 | fi 33 | 34 | AUTHOR_NAME="$(git log -1 "$GITHUB_SHA" --pretty="%aN")" 35 | COMMITTER_NAME="$(git log -1 "$GITHUB_SHA" --pretty="%cN")" 36 | COMMIT_SUBJECT="$(git log -1 "$GITHUB_SHA" --pretty="%s")" 37 | COMMIT_MESSAGE="$(git log -1 "$GITHUB_SHA" --pretty="%b")" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' 38 | COMMIT_URL="https://github.com/$GITHUB_REPOSITORY/commit/$GITHUB_SHA" 39 | 40 | # If, for example, $GITHUB_REF = refs/heads/feature/example-branch 41 | # Then this sed command returns: feature/example-branch 42 | BRANCH_NAME="$(echo $GITHUB_REF | sed 's/^[^/]*\/[^/]*\///g')" 43 | REPO_URL="https://github.com/$GITHUB_REPOSITORY" 44 | BRANCH_OR_PR="Branch" 45 | BRANCH_OR_PR_URL="$REPO_URL/tree/$BRANCH_NAME" 46 | ACTION_URL="$COMMIT_URL/checks" 47 | COMMIT_OR_PR_URL=$COMMIT_URL 48 | if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then 49 | CREDITS="$AUTHOR_NAME authored & committed" 50 | else 51 | CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed" 52 | fi 53 | 54 | if [ "$GITHUB_EVENT_NAME" == "pull_request" ]; then 55 | BRANCH_OR_PR="Pull Request" 56 | 57 | PR_NUM=$(sed 's/\/.*//g' <<< $BRANCH_NAME) 58 | BRANCH_OR_PR_URL="$REPO_URL/pull/$PR_NUM" 59 | BRANCH_NAME="#${PR_NUM}" 60 | 61 | # Call to GitHub API to get PR title 62 | PULL_REQUEST_ENDPOINT="https://api.github.com/repos/$GITHUB_REPOSITORY/pulls/$PR_NUM" 63 | 64 | WORK_DIR=$(dirname ${BASH_SOURCE[0]}) 65 | PULL_REQUEST_TITLE=$(ruby $WORK_DIR/get_pull_request_title.rb $PULL_REQUEST_ENDPOINT) 66 | 67 | COMMIT_SUBJECT=$PULL_REQUEST_TITLE 68 | COMMIT_MESSAGE="Pull Request #$PR_NUM" 69 | ACTION_URL="$BRANCH_OR_PR_URL/checks" 70 | COMMIT_OR_PR_URL=$BRANCH_OR_PR_URL 71 | fi 72 | 73 | TIMESTAMP=$(date -u +%FT%TZ) 74 | WEBHOOK_DATA='{ 75 | "avatar_url": "'$AVATAR'", 76 | "embeds": [ { 77 | "color": '$EMBED_COLOR', 78 | "author": { 79 | "name": "'"$STATUS_MESSAGE"': '"$WORKFLOW_NAME"' ('"${HOOK_OS_NAME}"') - '"$GITHUB_REPOSITORY"'", 80 | "url": "'$ACTION_URL'", 81 | "icon_url": "'$AVATAR'" 82 | }, 83 | "title": "'"$COMMIT_SUBJECT"'", 84 | "url": "'"$COMMIT_OR_PR_URL"'", 85 | "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", 86 | "fields": [ 87 | { 88 | "name": "Commit", 89 | "value": "'"[\`${GITHUB_SHA:0:7}\`](${COMMIT_URL})"'", 90 | "inline": true 91 | }, 92 | { 93 | "name": "'"$BRANCH_OR_PR"'", 94 | "value": "'"[\`${BRANCH_NAME}\`](${BRANCH_OR_PR_URL})"'", 95 | "inline": true 96 | } 97 | ], 98 | "timestamp": "'"$TIMESTAMP"'" 99 | } ] 100 | }' 101 | 102 | for ARG in "$@"; do 103 | echo -e "[Webhook]: Sending webhook to Discord...\\n"; 104 | 105 | (curl --fail --progress-bar -A "GitHub-Actions-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "${WEBHOOK_DATA// / }" "$ARG" \ 106 | && echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook." 107 | done 108 | -------------------------------------------------------------------------------- /testing_utils/README.md: -------------------------------------------------------------------------------- 1 | # Testing utils 2 | Testing this on GitHub Actions can be annoying due to wait times. While you will eventually have to do so, when developing locally, it's nice to have these tool(s). 3 | 4 | ## Tools 5 | - [`local_env_run.sh`](local_env_run.sh) Setup dummy variables and run send.sh! -------------------------------------------------------------------------------- /testing_utils/local_env_run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | export GITHUB_SHA="69a3b64e5b36db68b54ca7291d8acfde261dda76" 4 | # Enter your url.....don't commit this 5 | export DISCORD_URL="" 6 | export GITHUB_REPOSITORY="DiscordHooks/github-actions-discord-webhook" 7 | export GITHUB_REF="refs/heads/4/merge" # refs/heads/feature/new-stuff 8 | export HOOK_OS_NAME="Windows" 9 | export WORKFLOW_NAME="Main Pipeline" 10 | export GITHUB_EVENT_NAME="pull_request" # Could also be push, ext. 11 | ./send.sh Success $DISCORD_URL --------------------------------------------------------------------------------