From 8277caf1540c584c9238a53bab7a5ff9322ad228 Mon Sep 17 00:00:00 2001 From: RamaXp Date: Tue, 13 Jul 2021 14:48:10 +0700 Subject: [PATCH 1/4] feature (before_build) : Add before build function --- send.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/send.sh b/send.sh index 1e7c38f..61ce67b 100644 --- a/send.sh +++ b/send.sh @@ -1,6 +1,12 @@ #!/bin/bash case $1 in + "building" ) + EMBED_COLOR=1796534 + STATUS_MESSAGE="Building" + ARTIFACT_URL="Not available" + ;; + "success" ) EMBED_COLOR=3066993 STATUS_MESSAGE="Passed" From 173c17bbd127ed3a35996a5f8529c32927b6b55b Mon Sep 17 00:00:00 2001 From: RamaXp Date: Thu, 15 Jul 2021 03:21:37 +0700 Subject: [PATCH 2/4] feature (before_build): Fix add build function --- send.sh.1 | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 send.sh.1 diff --git a/send.sh.1 b/send.sh.1 new file mode 100644 index 0000000..61ce67b --- /dev/null +++ b/send.sh.1 @@ -0,0 +1,125 @@ +#!/bin/bash + +case $1 in + "building" ) + EMBED_COLOR=1796534 + STATUS_MESSAGE="Building" + ARTIFACT_URL="Not available" + ;; + + "success" ) + EMBED_COLOR=3066993 + STATUS_MESSAGE="Passed" + ARTIFACT_URL="$CI_JOB_URL/artifacts/download" + ;; + + "failure" ) + EMBED_COLOR=15158332 + STATUS_MESSAGE="Failed" + ARTIFACT_URL="Not available" + ;; + + * ) + EMBED_COLOR=0 + STATUS_MESSAGE="Status Unknown" + ARTIFACT_URL="Not available" + ;; +esac + +shift + +if [ $# -lt 1 ]; then + 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 +fi + +AUTHOR_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%aN")" +COMMITTER_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%cN")" +COMMIT_SUBJECT="$(git log -1 "$CI_COMMIT_SHA" --pretty="%s")" +COMMIT_MESSAGE="$(git log -1 "$CI_COMMIT_SHA" --pretty="%b")" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' + + +if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then + CREDITS="$AUTHOR_NAME authored & committed" +else + CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed" +fi + +if [ -z $CI_MERGE_REQUEST_ID ]; then + URL="" +else + URL="$CI_PROJECT_URL/merge_requests/$CI_MERGE_REQUEST_ID" +fi + +TIMESTAMP=$(date --utc +%FT%TZ) + +if [ -z $LINK_ARTIFACT ] || [ $LINK_ARTIFACT = false ] ; then + WEBHOOK_DATA='{ + "username": "", + "avatar_url": "https://gitlab.com/favicon.png", + "embeds": [ { + "color": '$EMBED_COLOR', + "author": { + "name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'", + "url": "'"$CI_PIPELINE_URL"'", + "icon_url": "https://gitlab.com/favicon.png" + }, + "title": "'"$COMMIT_SUBJECT"'", + "url": "'"$URL"'", + "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", + "fields": [ + { + "name": "Commit", + "value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'", + "inline": true + }, + { + "name": "Branch", + "value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'", + "inline": true + } + ], + "timestamp": "'"$TIMESTAMP"'" + } ] + }' +else + WEBHOOK_DATA='{ + "username": "", + "avatar_url": "https://gitlab.com/favicon.png", + "embeds": [ { + "color": '$EMBED_COLOR', + "author": { + "name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'", + "url": "'"$CI_PIPELINE_URL"'", + "icon_url": "https://gitlab.com/favicon.png" + }, + "title": "'"$COMMIT_SUBJECT"'", + "url": "'"$URL"'", + "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", + "fields": [ + { + "name": "Commit", + "value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'", + "inline": true + }, + { + "name": "Branch", + "value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'", + "inline": true + }, + { + "name": "Artifacts", + "value": "'"[\`$CI_JOB_ID\`]($ARTIFACT_URL)"'", + "inline": true + } + ], + "timestamp": "'"$TIMESTAMP"'" + } ] + }' +fi + +for ARG in "$@"; do + echo -e "[Webhook]: Sending webhook to Discord...\\n"; + + (curl --fail --progress-bar -A "GitLabCI-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "$WEBHOOK_DATA" "$ARG" \ + && echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook." +done From 6b71073875d6afb9eba84bc4d74fb2c9a600df65 Mon Sep 17 00:00:00 2001 From: RamaXp Date: Thu, 15 Jul 2021 03:23:14 +0700 Subject: [PATCH 3/4] fix (before build): deleted faulty copy --- send.sh.1 | 125 ------------------------------------------------------ 1 file changed, 125 deletions(-) delete mode 100644 send.sh.1 diff --git a/send.sh.1 b/send.sh.1 deleted file mode 100644 index 61ce67b..0000000 --- a/send.sh.1 +++ /dev/null @@ -1,125 +0,0 @@ -#!/bin/bash - -case $1 in - "building" ) - EMBED_COLOR=1796534 - STATUS_MESSAGE="Building" - ARTIFACT_URL="Not available" - ;; - - "success" ) - EMBED_COLOR=3066993 - STATUS_MESSAGE="Passed" - ARTIFACT_URL="$CI_JOB_URL/artifacts/download" - ;; - - "failure" ) - EMBED_COLOR=15158332 - STATUS_MESSAGE="Failed" - ARTIFACT_URL="Not available" - ;; - - * ) - EMBED_COLOR=0 - STATUS_MESSAGE="Status Unknown" - ARTIFACT_URL="Not available" - ;; -esac - -shift - -if [ $# -lt 1 ]; then - 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 -fi - -AUTHOR_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%aN")" -COMMITTER_NAME="$(git log -1 "$CI_COMMIT_SHA" --pretty="%cN")" -COMMIT_SUBJECT="$(git log -1 "$CI_COMMIT_SHA" --pretty="%s")" -COMMIT_MESSAGE="$(git log -1 "$CI_COMMIT_SHA" --pretty="%b")" | sed -E ':a;N;$!ba;s/\r{0,1}\n/\\n/g' - - -if [ "$AUTHOR_NAME" == "$COMMITTER_NAME" ]; then - CREDITS="$AUTHOR_NAME authored & committed" -else - CREDITS="$AUTHOR_NAME authored & $COMMITTER_NAME committed" -fi - -if [ -z $CI_MERGE_REQUEST_ID ]; then - URL="" -else - URL="$CI_PROJECT_URL/merge_requests/$CI_MERGE_REQUEST_ID" -fi - -TIMESTAMP=$(date --utc +%FT%TZ) - -if [ -z $LINK_ARTIFACT ] || [ $LINK_ARTIFACT = false ] ; then - WEBHOOK_DATA='{ - "username": "", - "avatar_url": "https://gitlab.com/favicon.png", - "embeds": [ { - "color": '$EMBED_COLOR', - "author": { - "name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'", - "url": "'"$CI_PIPELINE_URL"'", - "icon_url": "https://gitlab.com/favicon.png" - }, - "title": "'"$COMMIT_SUBJECT"'", - "url": "'"$URL"'", - "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", - "fields": [ - { - "name": "Commit", - "value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'", - "inline": true - }, - { - "name": "Branch", - "value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'", - "inline": true - } - ], - "timestamp": "'"$TIMESTAMP"'" - } ] - }' -else - WEBHOOK_DATA='{ - "username": "", - "avatar_url": "https://gitlab.com/favicon.png", - "embeds": [ { - "color": '$EMBED_COLOR', - "author": { - "name": "Pipeline #'"$CI_PIPELINE_IID"' '"$STATUS_MESSAGE"' - '"$CI_PROJECT_PATH_SLUG"'", - "url": "'"$CI_PIPELINE_URL"'", - "icon_url": "https://gitlab.com/favicon.png" - }, - "title": "'"$COMMIT_SUBJECT"'", - "url": "'"$URL"'", - "description": "'"${COMMIT_MESSAGE//$'\n'/ }"\\n\\n"$CREDITS"'", - "fields": [ - { - "name": "Commit", - "value": "'"[\`$CI_COMMIT_SHORT_SHA\`]($CI_PROJECT_URL/commit/$CI_COMMIT_SHA)"'", - "inline": true - }, - { - "name": "Branch", - "value": "'"[\`$CI_COMMIT_REF_NAME\`]($CI_PROJECT_URL/tree/$CI_COMMIT_REF_NAME)"'", - "inline": true - }, - { - "name": "Artifacts", - "value": "'"[\`$CI_JOB_ID\`]($ARTIFACT_URL)"'", - "inline": true - } - ], - "timestamp": "'"$TIMESTAMP"'" - } ] - }' -fi - -for ARG in "$@"; do - echo -e "[Webhook]: Sending webhook to Discord...\\n"; - - (curl --fail --progress-bar -A "GitLabCI-Webhook" -H Content-Type:application/json -H X-Author:k3rn31p4nic#8383 -d "$WEBHOOK_DATA" "$ARG" \ - && echo -e "\\n[Webhook]: Successfully sent the webhook.") || echo -e "\\n[Webhook]: Unable to send webhook." -done From b71c12dbd327eae4eb7af05900da1894d1b2b47d Mon Sep 17 00:00:00 2001 From: RamaXp Date: Thu, 15 Jul 2021 03:30:41 +0700 Subject: [PATCH 4/4] docs (before build): update README.md for build notif --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 09925f9..3f26bf4 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,22 @@ looking. You've came to the right place. when: on_failure ``` +1. (Optional) If you want a notification before the pipeline started, add these lines, in their appropiate locations on the `.gitlab-ci.yml` + + ```yaml + stages: + - build + - deploy # adjust with your deployment stage + - notification # this is the after-build notification + + start_build: + stage: build + script: + - wget "https://gitlab.assemblrworld.com/api/v4/projects/92/repository/files/send.sh/raw?ref=master&private_token=YCTyexwYkcpV77GQsJgu" -O send.sh + - chmod +x send.sh + - ./send.sh building $WEBHOOK_URL + ``` + 1. Grab your coffee ☕ and enjoy! And, if you liked this, please ⭐**Star** this repository to show your love.