├── .github ├── FUNDING.yml └── bastion.yml ├── LICENSE ├── send.sh └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: k3rn31p4nic 4 | patreon: # Replace with a single Patreon username 5 | open_collective: DiscordHooks 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | custom: https://paypal.me/snkrsnkampa 10 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.github/bastion.yml: -------------------------------------------------------------------------------- 1 | ################################################## 2 | # Bastion GitHub Bot 3 | # Configuration File 4 | # https://github.com/TheBastionBot/Bastion-GitHub-Bot 5 | ################################################## 6 | 7 | ## Invite Contributors to Organization ## 8 | 9 | inviteContributors: true 10 | 11 | 12 | ## Issue Welcome Comments ## 13 | 14 | # Comment to send on a user's first issue in the repository. 15 | firstIssueWelcomeComment: | 16 | Thank you for opening this issue. A maintainer will get by as soon as possible to address this issue. 17 | Since this is your first issue in this repository, please make sure to follow the issue template and provide as much detail as possible. 18 | 19 | # Comment to send on the user's forthcoming issues in the repository. 20 | issueWelcomeComment: | 21 | Thank you for opening this issue. A maintainer will get by as soon as possible to address this issue. 22 | In the mean time, please check out our contributing guidelines to explore other ways you can get involved. 23 | 24 | 25 | ## Pull Request Welcome Comments ## 26 | 27 | # Comment to send on a user's first pull request in the repository. 28 | firstPullRequestWelcomeComment: | 29 | Thank you for opening this pull request. A maintainer will get by as soon as possible to review this pull request. 30 | Since this is your first pull request in this repository, please make sure to follow the pull request template and provide as much detail as possible. 31 | 32 | # Comment to send on the user's forthcoming pull requests in the repository. 33 | pullRequestWelcomeComment: | 34 | Thank you for opening this pull request. A maintainer will get by as soon as possible to review this pull request. 35 | -------------------------------------------------------------------------------- /send.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | case $1 in 4 | "success" ) 5 | EMBED_COLOR=3066993 6 | STATUS_MESSAGE="Passed" 7 | ARTIFACT_URL="$CI_JOB_URL/artifacts/download" 8 | ;; 9 | 10 | "failure" ) 11 | EMBED_COLOR=15158332 12 | STATUS_MESSAGE="Failed" 13 | ARTIFACT_URL="Not available" 14 | ;; 15 | 16 | * ) 17 | EMBED_COLOR=0 18 | STATUS_MESSAGE="Status Unknown" 19 | ARTIFACT_URL="Not available" 20 | ;; 21 | esac 22 | 23 | shift 24 | 25 | if [ $# -lt 1 ]; then 26 | 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/gitlab-ci-discord-webhook" && exit 27 | fi 28 | 29 | AUTHOR_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%aN")" 30 | COMMITTER_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%cN")" 31 | COMMIT_SUBJECT="$(git log -1 "$CI_COMMIT_SHA" --pretty="%s")" 32 | COMMIT_MESSAGE="$(git log -1 "$CI_COMMIT_SHA" --pretty="%b")" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' 33 | 34 | 35 | if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then 36 | CREDITS="$AUTHOR_NAME authored & committed" 37 | else 38 | CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed" 39 | fi 40 | 41 | if [ -z $CI_MERGE_REQUEST_ID ]; then 42 | URL="" 43 | else 44 | URL="$CI_PROJECT_URL/merge_requests/$CI_MERGE_REQUEST_ID" 45 | fi 46 | 47 | TIMESTAMP=$(date --utc +%FT%TZ) 48 | 49 | if [ -z $LINK_ARTIFACT ] || [ $LINK_ARTIFACT = false ] ; then 50 | WEBHOOK_DATA='{ 51 | "avatar_url": "https://gitlab.com/favicon.png", 52 | "embeds": [ { 53 | "color": '$EMBED_COLOR', 54 | "author": { 55 | "name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'", 56 | "url": "'"$CI_PIPELINE_URL"'", 57 | "icon_url": "https://gitlab.com/favicon.png" 58 | }, 59 | "title": "'"$COMMIT_SUBJECT"'", 60 | "url": "'"$URL"'", 61 | "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", 62 | "fields": [ 63 | { 64 | "name": "Commit", 65 | "value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'", 66 | "inline": true 67 | }, 68 | { 69 | "name": "Branch", 70 | "value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'", 71 | "inline": true 72 | } 73 | ], 74 | "timestamp": "'"$TIMESTAMP"'" 75 | } ] 76 | }' 77 | else 78 | WEBHOOK_DATA='{ 79 | "avatar_url": "https://gitlab.com/favicon.png", 80 | "embeds": [ { 81 | "color": '$EMBED_COLOR', 82 | "author": { 83 | "name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'", 84 | "url": "'"$CI_PIPELINE_URL"'", 85 | "icon_url": "https://gitlab.com/favicon.png" 86 | }, 87 | "title": "'"$COMMIT_SUBJECT"'", 88 | "url": "'"$URL"'", 89 | "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", 90 | "fields": [ 91 | { 92 | "name": "Commit", 93 | "value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'", 94 | "inline": true 95 | }, 96 | { 97 | "name": "Branch", 98 | "value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'", 99 | "inline": true 100 | }, 101 | { 102 | "name": "Artifacts", 103 | "value": "'"[\`$CI_JOB_ID\`]($ARTIFACT_URL)"'", 104 | "inline": true 105 | } 106 | ], 107 | "timestamp": "'"$TIMESTAMP"'" 108 | } ] 109 | }' 110 | fi 111 | 112 | for ARG in "$@"; do 113 | echo -e "[Webhook]: Sending webhook to Discord...\\n"; 114 | 115 | (curl --fail --progress-bar -A "GitLabCI-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "$WEBHOOK_DATA" "$ARG" \ 116 | && echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook." 117 | done 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GitLab CI 🡒 Discord Webhook 2 | [![Backers on Open Collective](https://opencollective.com/discordhooks/backers/badge.svg)](#backers) 3 | [![Sponsors on Open Collective](https://opencollective.com/discordhooks/sponsors/badge.svg)](#sponsors) 4 | 5 | If you are looking for a way to get build (success/fail) status reports from 6 | [GitLab CI](https://gitlab.com) in [Discord](https://discordapp.com), stop 7 | looking. You've came to the right place. 8 | 9 | ## Requirements 10 | - You should be using GitLab CI. 11 | - A Discord Server where notifications will be posted. 12 | - 5 minutes 13 | - A cup of coffee ☕ 14 | 15 | ## Guide 16 | 1. Create a webhook in your Discord Server ([Guide](https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks)). 17 | 18 | 1. Copy the **Webhook URL**. 19 | 20 | 1. Go to your repository CI/CD settings (for which you want status notifications) 21 | in GitLab and add an environment variable called `WEBHOOK_URL` and paste 22 | the **Webhook URL** you got in the previous step. You can also specify 23 | multiple webhook addresses at once, separating each with a whitespace 24 | character, such as space. 25 | 26 | ![Add environment variable in GitLab](https://docs.gitlab.com/ee/ci/variables/img/new_custom_variables_example.png) 27 | See: https://docs.gitlab.com/ee/ci/variables/#via-the-ui 28 | 29 | 1. Add these lines, in their appropriate locations, to the `.gitlab-ci.yml` 30 | file of your repository. 31 | 32 | ```yaml 33 | stages: 34 | - notification 35 | success_notification: 36 | stage: notification 37 | script: 38 | - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh 39 | - chmod +x send.sh 40 | - ./send.sh success $WEBHOOK_URL 41 | when: on_success 42 | failure_notification: 43 | stage: notification 44 | script: 45 | - wget https://raw.githubusercontent.com/DiscordHooks/gitlab-ci-discord-webhook/master/send.sh 46 | - chmod +x send.sh 47 | - ./send.sh failure $WEBHOOK_URL 48 | when: on_failure 49 | ``` 50 | 51 | 1. Grab your coffee ☕ and enjoy! And, if you liked this, please ⭐**Star** 52 | this repository to show your love. 53 | 54 | ### Artifacts 55 | 56 | If you'd like to also link the artifacts in the CI Message, set the variable `LINK_ARTIFACT` to `true`: 57 | 58 | ```yaml 59 | variables: 60 | LINK_ARTIFACT: true 61 | ``` 62 | 63 | Make sure that the artifacts are available to download in the ```success_notification``` job. If they are produced by a previous one, you can follow [this StackOverflow question](https://stackoverflow.com/questions/38140996/how-can-i-pass-artifacts-to-another-stage "this StackOverflow question") 64 | 65 | Please also note that artifacts are only available, if ```success_notification``` has been triggered. 66 | 67 | ### Note 68 | - If you face any issues in the scripts (and you're sure it's not on your side), 69 | please consider opening an issue and I'll fix it ASAP. 70 | - If you want to improve the scripts, feel free to open a pull request. 71 | - If you are using an alpine image, you need to add `git` and `curl` packages before running this script. 72 | ```yaml 73 | # ... 74 | script: 75 | - apk add --update git curl 76 | # ... 77 | ``` 78 | 79 | ### See Also 80 | - [Travis CI -> Discord Webhook](https://github.com/DiscordHooks/travis-ci-discord-webhook) 81 | - [AppVeyor -> Discord Webhook](https://github.com/DiscordHooks/appveyor-discord-webhook) 82 | 83 | ## Contributors 84 | 85 | This project exists thanks to all the people who contribute. 86 | 87 | 88 | ## Backers 89 | 90 | Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/DiscordHooks#backer)] 91 | 92 | 93 | 94 | 95 | ## Sponsors 96 | 97 | Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/DiscordHooks#sponsor)] 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | --------------------------------------------------------------------------------