-
Notifications
You must be signed in to change notification settings - Fork 5
/
notify.sh
executable file
·60 lines (54 loc) · 1.59 KB
/
notify.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
set -eu
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
# 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
# Build state
BUILD_STATUS=${BUILD_STATUS:?err}
BASE_SIZE=${BASE_SIZE:-0}
LAYERED_SIZE=${LAYERED_SIZE:-0}
# 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
{
"username": "$CI_PROJECT_NAMESPACE",
"reponame": "$CI_PROJECT_NAME",
"branch": "$CI_COMMIT_BRANCH",
"state": {
"BUILD_STATUS": "$BUILD_STATUS",
"BASE_SIZE": "$BASE_SIZE",
"LAYERED_SIZE": "$LAYERED_SIZE"
}
}
EOF
)
echo "BODY: $BODY"
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'
exit 0
else
echo 'Failed to send notification'
exit 1
fi