-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentrypoint.sh
executable file
·101 lines (87 loc) · 2.82 KB
/
entrypoint.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/bin/sh
# Required environment variables:
# ARGOCD_SERVER
# ARGOCD_ADMIN_PASS or ARGOCD_TOKEN
# ARGOCD_APP
# ARGOCD_HOOKSTATE
# SLACK_WEBHOOK_URL
# SLACK_CHANNEL
# Optional environment variables:
# SLACK_PRETEXT
if [ -z "$ARGOCD_SERVER" ] || [ -z "$ARGOCD_APP" ] || [ -z "$ARGOCD_HOOKSTATE" ] || [ -z "$SLACK_WEBHOOK_URL" ] || [ -z "$SLACK_CHANNEL" ]; then
echo 'One or more of the required variables are not set'
exit 1
fi
# Determine if Admin pass or Token was provided
if [ -z "$ARGOCD_TOKEN" ] && [ -z "$ARGOCD_ADMIN_PASS" ]; then
echo "Missing ARGOCD_TOKEN or ARGOCD_ADMIN_PASS"
exit 1
fi
if [ ! -z "$ARGOCD_ADMIN_PASS" ]; then
ARGOCD_TOKEN=$(curl -s $ARGOCD_SERVER/api/v1/session -d "{\"username\": \"admin\", \"password\": \"$ARGOCD_ADMIN_PASS\"}" | jq -r .token)
fi
if [ -z "$ARGOCD_TOKEN" ]; then
echo "ARGOCD_TOKEN is empty"
exit 1
fi
if [ -z "SLACK_PRETEXT" ]; then
SLACK_PRETEXT="ArgoCD"
fi
# Get token, or simply use it if it was provided as env var
# curl -s $ARGOCD_SERVER/api/v1/applications -H "Authorization: Bearer $ARGOCD_TOKEN" > tmp.json
curl -s $ARGOCD_SERVER/api/v1/applications --cookie "argocd.token=$ARGOCD_TOKEN" > tmp.json
# Set app url to include in the message
ARGOCD_APP_URL="$ARGOCD_SERVER/applications/$ARGOCD_APP"
REVISION=$(jq -r '.items[] | select( .metadata.name == "'$ARGOCD_APP'") | .status.operationState.operation.sync.revision' tmp.json)
# Get information about git repo
REPO_URL=$(jq -r '.items[] | select( .metadata.name == "'$ARGOCD_APP'") | .spec.source.repoURL' tmp.json)
REPO_URL=${REPO_URL%.git*}
REPO_OWNER=$(echo ${REPO_URL##http**.com} | cut -d '/' -f2)
REPO=$(echo ${REPO_URL##http**.com} | cut -d '/' -f3)
# Set Slack color and status based on hook
case $ARGOCD_HOOKSTATE in
SyncFail)
COLOR="danger"
STATUS="error"
;;
PostSync)
COLOR="good"
STATUS="success"
;;
*)
COLOR="warning"
STATUS="unknown"
;;
esac
generate_data()
{
cat <<EOF
{
"channel": "$SLACK_CHANNEL",
"attachments": [
{
"title": "Application: $ARGOCD_APP",
"title_link": "$ARGOCD_APP_URL",
"color": "$COLOR",
"pretext": "$SLACK_PRETEXT",
"fields": [
{
"title": "Status",
"value": "$STATUS",
"short": true
},
{
"title": "Commit",
"value": "<$REPO_URL/commit/$REVISION|$REVISION>",
"short": false
}
],
"footer": "$REPO_URL",
"footer_icon": "https://github.githubassets.com/favicon.ico",
"ts": $(date +%s)
}
]
}
EOF
}
curl -X POST -H 'Content-type: application/json' --data "$(generate_data)" "$SLACK_WEBHOOK_URL"