This repository has been archived by the owner on Oct 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·146 lines (121 loc) · 5.44 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# ----------------------------------------------------------------------------------------------------------------------
#
# Wercker build step slack notifier
#
# ----------------------------------------------------------------------------------------------------------------------
#
# Fix env expectations
# This supports setting values through wercker environment and also overriding (or setting) them via the wercker.yml
#
# this is also for interchangeability of slack notifiers. i.e. can switch to this one from another based on the
# official notifier.
#
# e.g. Wanting the slack notifier url:
# 1. WERCKER_KELEVRA_SLACK_NOTIFIER_URL is set via option in wercker.yml as `kelevra-slack-notifier` is the step.
# 2. if WERCKER_KELEVRA_SLACK_NOTIFIER_URL is not set then SLACK_NOTIFIER_URL is used from ENV (pipeline else organisation)
# 3. else WERCKER_SLACK_NOTIFIER_URL is empty
#
WERCKER_SLACK_NOTIFIER_URL=${WERCKER_KELEVRA_SLACK_NOTIFIER_URL:-${SLACK_NOTIFIER_URL}}
WERCKER_SLACK_NOTIFIER_NOTIFY_ON=${WERCKER_KELEVRA_SLACK_NOTIFIER_NOTIFY_ON:-${SLACK_NOTIFIER_NOTIFY_ON}}
WERCKER_SLACK_NOTIFIER_CHANNEL=${WERCKER_KELEVRA_SLACK_NOTIFIER_CHANNEL:-${SLACK_NOTIFIER_CHANNEL}}
WERCKER_SLACK_NOTIFIER_USERNAME=${WERCKER_KELEVRA_SLACK_NOTIFIER_USERNAME:-${SLACK_NOTIFIER_USERNAME}}
WERCKER_SLACK_NOTIFIER_BRANCH=${WERCKER_KELEVRA_SLACK_NOTIFIER_BRANCH:-${SLACK_NOTIFIER_BRANCH}}
WERCKER_SLACK_NOTIFIER_WERCKER_TOKEN=${WERCKER_KELEVRA_SLACK_NOTIFIER_WERCKER_TOKEN:-${SLACK_NOTIFIER_WERCKER_TOKEN}}
# check if slack webhook url is present
if [ -z "$WERCKER_SLACK_NOTIFIER_URL" ]; then
fail "Please provide a Slack webhook URL"
fi
# 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
# skip notifications if not interested in passed builds or deploys
if [[ "$WERCKER_RESULT" = "passed" ] && [ ${WERCKER_SLACK_NOTIFIER_NOTIFY_ON} = "failed_or_passed_after_failed" ]]; then
if [ -z ${WERCKER_SLACK_NOTIFIER_WERCKER_TOKEN} ]; then
fail "No Wercker API token is specified."
fi
CURL="curl -H "Authorization: Bearer: ${WERCKER_SLACK_NOTIFIER_WERCKER_TOKEN}" https://app.wercker.com/api/v3"
# get the current pipeline-id (as is not available in the wercker env)
WERCKER_PIPELINE_ID=$(${CURL}/runs/${WERCKER_RUN_ID} | ./jq .pipeline.id )
# get previous run result for the current pipeline & branch
WERCKER_PREVIOUS_RESULT=$(${CURL}/runs/?&limit=1&status=finished&branch=${WERCKER_GIT_BRANCH}&pipelineId=${WERCKER_PIPELINE_ID} | ./jq .result)
if [ ${WERCKER_PREVIOUS_RESULT} != 'failed']; then
return 0;
fi
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 [ -n "$DEPLOY" ]; then
# its a deploy!
export ACTION="deploy ($WERCKER_DEPLOYTARGET_NAME)"
export ACTION_URL=$WERCKER_DEPLOY_URL
else
# its a build!
export ACTION="build"
export ACTION_URL=$WERCKER_BUILD_URL
fi
export MESSAGE="<$ACTION_URL|$ACTION> for $WERCKER_APPLICATION_NAME by $WERCKER_STARTED_BY has $WERCKER_RESULT on branch $WERCKER_GIT_BRANCH"
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
# 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\",
\"color\": \"$COLOR\"
}
]
}"
# 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