Skip to content

Commit

Permalink
Breaking: Update notify.sh to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
leojonathanoh committed Sep 15, 2023
1 parent 28892d7 commit b18844e
Showing 1 changed file with 32 additions and 37 deletions.
69 changes: 32 additions & 37 deletions notify.sh
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
#!/bin/sh
set -eu

# Process job variables
GAME_VERSION=${GAME_VERSION:-}
APPID=${APPID:-}
CLIENT_APPID=${CLIENT_APPID:-}
GAME=${GAME:-}
MOD=${MOD:-}
FIX_APPMANIFEST=${FIX_APPMANIFEST:-}
GAME_UPDATE_COUNT=${GAME_UPDATE_COUNT:-}
LATEST=${LATEST:-}
CACHE=${CACHE:-}
NO_TEST=${NO_TEST:-}
NO_PUSH=${NO_PUSH:-}
STEAM_LOGIN=${STEAM_LOGIN:-}
usage() {
echo "notify.sh: Send build status to a webhook. Requires git and curl"
echo "Examples: "
echo " ./notify.sh"
}
if [ "${1:-}" = '-h' ] || [ "${1:-}" = '--help' ]; then
usage
exit 0
fi

# Read build state file
# CI variables
CI_JOB_ID=${CI_JOB_ID:?err}
CI_PROJECT_NAMESPACE=${CI_PROJECT_NAMESPACE:?err}
CI_PROJECT_NAME=${CI_PROJECT_NAME:-$( git rev-parse --show-toplevel | xargs basename )}
CI_COMMIT_BRANCH=${CI_COMMIT_BRANCH:-$( git rev-parse --abbrev-ref HEAD )}
CI_COMMIT_SHORT_SHA=${CI_COMMIT_SHORT_SHA:-$( git rev-parse HEAD | head -c7 )}

# Read .build.state file
echo "Reading .build.state file"
. ./.build.state

# Process build state variables
# Build state
BUILD_STATUS=${BUILD_STATUS:?err}
BASE_SIZE=${BASE_SIZE:-0}
LAYERED_SIZE=${LAYERED_SIZE:-0}

# Send notification
date
echo 'Sending notification'
# Secrets
X_GITLAB_WEBHOOK_SECRET=${X_GITLAB_WEBHOOK_SECRET:?err}
NOTIFICATION_WEBHOOK=${NOTIFICATION_WEBHOOK:?err}

# Send a webhook notification
date -Iseconds
echo "Sending notification"
BODY=$( cat <<EOF
{
"build_num": "$CI_JOB_ID",
"username": "$CI_PROJECT_NAMESPACE",
"reponame": "$CI_PROJECT_NAME",
"branch": "$CI_COMMIT_BRANCH",
"build_parameters": {
"GAME_VERSION": "$GAME_VERSION",
"APPID": "$APPID",
"CLIENT_APPID": "$CLIENT_APPID",
"GAME": "$GAME",
"MOD": "$MOD",
"FIX_APPMANIFEST": "$FIX_APPMANIFEST",
"GAME_UPDATE_COUNT": "$GAME_UPDATE_COUNT",
"LATEST": "$LATEST",
"CACHE": "$CACHE",
"NO_TEST": "$NO_TEST",
"NO_PUSH": "$NO_PUSH",
"STEAM_LOGIN": "$STEAM_LOGIN"
},
"commit_sha": "$CI_COMMIT_SHORT_SHA",
"status": "$BUILD_STATUS",
"state": {
"BUILD_STATUS": "$BUILD_STATUS",
Expand All @@ -56,13 +51,13 @@ BODY=$( cat <<EOF
EOF
)
echo "BODY: $BODY"
date
STATUS=$( curl -s -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' -H "$X_CI_WEBHOOK_SECRET_HEADER" --data "$BODY" "$NOTIFICATION_WEBHOOK" )
date -Iseconds
STATUS=$( curl -s -o /dev/null -w '%{http_code}' -X POST -H 'Content-Type: application/json' -H "x-gitlab-webhook-secret: $X_GITLAB_WEBHOOK_SECRET" --data "$BODY" "$NOTIFICATION_WEBHOOK" || true )
echo "STATUS: $STATUS"
if [ $STATUS -eq 200 ] || [ $STATUS -eq 201 ]; then
echo 'Notification sent'
if [ "$STATUS" -eq 200 ] || [ "$STATUS" -eq 201 ]; then
echo "Notification sent"
exit 0
else
echo 'Failed to send notification'
echo "Failed to send notification"
exit 1
fi

0 comments on commit b18844e

Please sign in to comment.