forked from pjvds/step-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·116 lines (96 loc) · 3.69 KB
/
run.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
#source build-esen.sh
# check if slack webhook url is present
if [ -z "$WERCKER_SLACK_NOTIFIER_URL" ]; then
fail "Please provide a Slack webhook URL"
fi
# check if a '#' was supplied in the channel name
if [ "${WERCKER_SLACK_NOTIFIER_CHANNEL:0:1}" = '#' ]; then
export WERCKER_SLACK_NOTIFIER_CHANNEL=${WERCKER_SLACK_NOTIFIER_CHANNEL:1}
fi
# if no username is provided use the default - werckerbot
if [ -z "$WERCKER_SLACK_NOTIFIER_USERNAME" ]; then
export WERCKER_SLACK_NOTIFIER_USERNAME=werckerbot
fi
# if no icon-url is provided for the bot use the default wercker icon
if [ -z "$WERCKER_SLACK_NOTIFIER_ICON_URL" ] && [ -z "$WERCKER_SLACK_NOTIFIER_ICON_EMOJI" ]; then
export WERCKER_SLACK_NOTIFIER_ICON_URL="https://secure.gravatar.com/avatar/a08fc43441db4c2df2cef96e0cc8c045?s=140"
fi
# check if this event is a build or deploy
if [ -n "$DEPLOY" ]; then
# its a deploy!
export ACTION="deploy"
export ACTION_URL=$WERCKER_DEPLOY_URL
else
# its a build!
export ACTION="build"
export ACTION_URL=$WERCKER_BUILD_URL
fi
export WERCKER_GIT_COMMIT_SHORT=$(echo "$WERCKER_GIT_COMMIT" | cut -c1-7)
export MESSAGE="<$ACTION_URL|$ACTION> for $WERCKER_APPLICATION_NAME by $WERCKER_STARTED_BY has $WERCKER_RESULT on branch $WERCKER_GIT_BRANCH (<https://$WERCKER_GIT_DOMAIN/$WERCKER_GIT_OWNER/$WERCKER_GIT_REPOSITORY/commit/$WERCKER_GIT_COMMIT|$WERCKER_GIT_COMMIT_SHORT>)"
export FALLBACK="$ACTION for $WERCKER_APPLICATION_NAME by $WERCKER_STARTED_BY has $WERCKER_RESULT on branch $WERCKER_GIT_BRANCH"
export COLOR="good"
if [ "$WERCKER_RESULT" = "failed" ]; then
export MESSAGE="$MESSAGE at step: $WERCKER_FAILED_STEP_DISPLAY_NAME"
export FALLBACK="$FALLBACK at step: $WERCKER_FAILED_STEP_DISPLAY_NAME"
export COLOR="danger"
fi
if [ -n "$WERCKER_SLACK_NOTIFIER_APPEND_MESSAGE" ]; then
export MESSAGE="$MESSAGE :: $WERCKER_SLACK_NOTIFIER_APPEND_MESSAGE"
fi
# construct the json
json="{"
# channels are optional, dont send one if it wasnt specified
if [ -n "$WERCKER_SLACK_NOTIFIER_CHANNEL" ]; then
json=$json"\"channel\": \"#$WERCKER_SLACK_NOTIFIER_CHANNEL\","
fi
if [ -z "$WERCKER_SLACK_NOTIFIER_ICON_EMOJI" ]; then
export ICON_TYPE=icon_url
export ICON_VALUE=$WERCKER_SLACK_NOTIFIER_ICON_URL
else
export ICON_TYPE=icon_emoji
export ICON_VALUE=$WERCKER_SLACK_NOTIFIER_ICON_EMOJI
fi
json=$json"
\"username\": \"$WERCKER_SLACK_NOTIFIER_USERNAME\",
\"$ICON_TYPE\":\"$ICON_VALUE\",
\"attachments\":[
{
\"fallback\": \"$FALLBACK\",
\"text\": \"$MESSAGE\",
\"color\": \"$COLOR\"
}
]
}"
# skip notifications if not interested in passed builds or deploys
if [ "$WERCKER_SLACK_NOTIFIER_NOTIFY_ON" = "failed" ]; then
if [ "$WERCKER_RESULT" = "passed" ]; then
return 0
fi
fi
# skip notifications if not on the right branch
if [ -n "$WERCKER_SLACK_NOTIFIER_BRANCH" ]; then
if [ "$WERCKER_SLACK_NOTIFIER_BRANCH" != "$WERCKER_GIT_BRANCH" ]; then
return 0
fi
fi
# post the result to the slack webhook
RESULT=$(curl -d "payload=$json" -s "$WERCKER_SLACK_NOTIFIER_URL" --output "$WERCKER_STEP_TEMP"/result.txt -w "%{http_code}")
cat "$WERCKER_STEP_TEMP/result.txt"
if [ "$RESULT" = "500" ]; then
if grep -Fqx "No token" "$WERCKER_STEP_TEMP/result.txt"; then
fail "No token is specified."
fi
if grep -Fqx "No hooks" "$WERCKER_STEP_TEMP/result.txt"; then
fail "No hook can be found for specified subdomain/token"
fi
if grep -Fqx "Invalid channel specified" "$WERCKER_STEP_TEMP/result.txt"; then
fail "Could not find specified channel for subdomain/token."
fi
if grep -Fqx "No text specified" "$WERCKER_STEP_TEMP/result.txt"; then
fail "No text specified."
fi
fi
if [ "$RESULT" = "404" ]; then
fail "Subdomain or token not found."
fi