forked from pjvds/step-slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·105 lines (87 loc) · 3.37 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
#!/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" ]; 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 [ "$WERCKER_SLACK_NOTIFIER_ACTION" = "deploy" ]; then
export ACTION="deploy"
export ACTION_URL=$WERCKER_DEPLOY_URL
elif [ "$WERCKER_SLACK_NOTIFIER_ACTION" = "publish" ]; then
export ACTION="publish"
export ACTION_URL="https://www.npmjs.com/package/$WERCKER_SLACK_NOTIFIER_PKG_NAME"
else
export ACTION="build"
export ACTION_URL=$WERCKER_RUN_URL
fi
export MESSAGE="<$ACTION_URL|$ACTION> of package *$PKG_DIR* ($WERCKER_APPLICATION_NAME) by $WERCKER_STARTED_BY has *$WERCKER_RESULT* on \`$WERCKER_GIT_BRANCH\`"
export FALLBACK="$ACTION of package *$PKG_DIR* ($WERCKER_APPLICATION_NAME) by $WERCKER_STARTED_BY has *$WERCKER_RESULT* on \`$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
# 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
json=$json"
\"username\": \"$WERCKER_SLACK_NOTIFIER_USERNAME\",
\"icon_url\":\"$WERCKER_SLACK_NOTIFIER_ICON_URL\",
\"attachments\":[
{
\"fallback\": \"$FALLBACK\",
\"text\": \"$MESSAGE\",
\"mrkdwn_in\": [\"text\", \"fallback\"],
\"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