-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.yml
462 lines (425 loc) · 20.1 KB
/
cmd.yml
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
slack-workflow-monitor:
description: >
Add this job to your workflow with no require statements.
This job will run in parallel with the rest of your workflow for monitoring
and will exit when all other jobs have completed.
parameters:
circle-token:
type: env_var_name
default: "CIRCLECI_TOKEN"
description: >
Enter your CircleCI Personal Access Token for interacting with the API.
You may generate one here: https://circleci.com/account/api
giphy-token:
type: env_var_name
default: "GIPHY_TOKEN"
description: >
Enter your giphy Personal Access Token for interacting with their API.
You may generate one here: https://developers.giphy.com/docs/api/
giphy-success-keyword:
type: string
default: "party"
giphy-failure-keyword:
type: string
default: "broken"
slack-webhook:
type: env_var_name
default: "SLACK_WEBHOOK"
description: "Enter your slack webhook url"
workflow-name:
type: string
docker:
- image: cimg/base:stable
steps:
- checkout
- jq/install
- run:
name: Slack Workflow Monitor
command: |
###############
# Begin Collecting
###############
CIRCLE_TOKEN=$<< parameters.circle-token>>
GIPHY_TOKEN=$<< parameters.giphy-token>>
SLACK_WEBHOOK=$<< parameters.slack-webhook>>
GIPHY_SUCCESS_KEYWORD="<< parameters.giphy-success-keyword>>"
GIPHY_FAILURE_KEYWORD="<< parameters.giphy-failure-keyword>>"
WORKFLOW_NAME="<< parameters.workflow-name >>"
CIRCLE_WORKFLOW_URL="https://app.circleci.com/pipelines/workflows/${CIRCLE_WORKFLOW_ID}"
echo "CIRCLE_BUILD_URL=$CIRCLE_BUILD_URL"
echo "CIRCLE_WORKFLOW_URL=$CIRCLE_WORKFLOW_URL"
echo "CIRCLE_WORKFLOW_ID=$CIRCLE_WORKFLOW_ID"
echo "CIRCLE_PULL_REQUEST=$CIRCLE_PULL_REQUEST"
echo "CIRCLE_PROJECT_REPONAME=$CIRCLE_PROJECT_REPONAME"
#https://stackoverflow.com/questions/8903239/how-to-calculate-time-elapsed-in-bash-script
SECONDS=0
GIT_COMMIT_DESC=$(git log --format=%B -n 1 $CIRCLE_SHA1)
GIT_NO_COMMITS=$(git rev-list HEAD --count)
SUCCESS_URL="http://api.giphy.com/v1/gifs/random?api_key=${GIPHY_TOKEN}&tag=${GIPHY_SUCCESS_KEYWORD}"
SUCCESS_GIF=$(curl -s $SUCCESS_URL | jq -r '.data.images.downsized.url')
FAIL_URL="http://api.giphy.com/v1/gifs/random?api_key=${GIPHY_TOKEN}&tag=${GIPHY_FAILURE_KEYWORD}"
FAIL_GIF=$(curl -s $FAIL_URL | jq -r '.data.images.downsized.url')
PROJECT_SLUG=${CIRCLE_BUILD_URL#https://circleci.com/}
PROJECT_SLUG=${PROJECT_SLUG%/*}
# yamllint disable rule:line-length
PREVIOUS_BUILD_DATA=$(curl -s GET https://circleci.com/api/v2/insights/${PROJECT_SLUG}/workflows/${WORKFLOW_NAME}?branch=${CIRCLE_BRANCH} \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header "Circle-Token: ${CIRCLE_TOKEN}")
PREVIOUS_BUILD_STATUS=$(echo "${PREVIOUS_BUILD_DATA}" | jq -r '.items[0].status')
# yamllint enable
PB_ITEMS=$(echo "$PREVIOUS_BUILD_DATA" | jq '.items')
PB_LENGTH=$(echo "$PB_ITEMS" | jq length)
echo "{PREVIOUS_BUILD_DATA}"
i="0"
while [ "$i" -lt "$PB_LENGTH" ]
do
BUILD_STATUS=$(echo "${PREVIOUS_BUILD_DATA}" | jq --arg i "$i" -r ".items[$i].status")
echo "A Previous build status was ${BUILD_STATUS}"
if [ "${BUILD_STATUS}" == "success" ] || [ "${BUILD_STATUS}" == "failed" ]; then
PREVIOUS_BUILD_STATUS=$BUILD_STATUS
break
fi
i="$((i+1))"
done
if [ "${PREVIOUS_BUILD_STATUS}" == "success" ] || [ "${PREVIOUS_BUILD_STATUS}" == "failed" ]; then
echo "known status '${PREVIOUS_BUILD_STATUS}' detected."
else
# assume its the first run on this branch, so assume previously was success
echo "unknown status '${PREVIOUS_BUILD_STATUS}' detected. Setting default of 'success'"
PREVIOUS_BUILD_STATUS='success'
fi
echo "https://circleci.com/api/v2/insights/${PROJECT_SLUG}/workflows/<< parameters.workflow-name >>?branch=${CIRCLE_BRANCH}" # yamllint disable-line rule:line-length
echo "PREVIOUS_BUILD_STATUS='${PREVIOUS_BUILD_STATUS}'"
echo "PROJECT_SLUG='${PROJECT_SLUG}'"
echo "SUCCESS_GIF='${SUCCESS_GIF}'"
echo "SUCCESS_GIF='${FAIL_GIF}'"
# yamllint disable-line rule:line-length
DATA_URL="https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID/job?circle-token=${CIRCLE_TOKEN}"
WF_DATA=$(curl -s "$DATA_URL")
WF_MESSAGE=$(echo "$WF_DATA" | jq '.message')
# Exit if no Workflow.
if [ "$WF_MESSAGE" = "\"Workflow not found\"" ];
then
echo "No Workflow was found."
echo "Your circle-token parameter may be wrong or you do not have access to this Workflow."
exit 1
fi
WF_ITEMS=$(echo "$WF_DATA" | jq '.items')
WF_LENGTH=$(echo "$WF_ITEMS" | jq length)
# GET URL PATH DATA
VCS_SHORT=$(echo $CIRCLE_BUILD_URL | cut -d"/" -f4)
case $VCS_SHORT in
gh)
VCS=github
;;
bb)
VCS=bitbucket
;;
*)
echo "No VCS found. Error" && exit 1
;;
esac
# Assume the WF is currently running
WF_FINISHED=false
echo $DATA_URL
echo $WF_DATA
echo $WF_ITEMS
echo -e "Jobs in Workflow: $WF_LENGTH "
# Exit if no other jobs in the Workflow.
if [ "$WF_LENGTH" -lt "2" ];
then
# yamllint disable-line rule:line-length
echo "Only a single job has been found in the workflow, indicating this reporter is the only job in the pipeline."
echo "Please add other jobs to the Workflow you wish to collect data on to send to Sumologic"
exit 0
fi
#####################
## START MAIN LOOP ##
#####################
# Check the status of all jobs in the workflow that are not this job
# and wait until they have all finished.
while [ "$WF_FINISHED" = false ]
do
WF_DATA=$(curl -s "$DATA_URL" | jq '.items')
echo "Waiting for other jobs to finish..."
# for each job in the workflow fetch the status.
# the WF_FINISHED will be assumed true unless one of the jobs in the Workflow is still running
# the flag will then be set back to false.
WF_FINISHED=true
i="0"
################
### JOB LOOP ###
################
while [ "$i" -lt "$WF_LENGTH" ]
do
echo "looping: $i"
# fetch the job info
JOB_DATA=$(echo "$WF_DATA" | jq --arg i "$i" ".[$i]")
JOB_NUMBER=$(echo "$JOB_DATA" | jq ".job_number")
JOB_STATUS=$(echo "$JOB_DATA" | jq -r ".status")
JOB_NAME=$(echo "$JOB_DATA" | jq -r ".name")
# Only check the job if it is not this current job
if [ "$JOB_NUMBER" = "$CIRCLE_BUILD_NUM" ];
then
echo "This is the reporter job. Skipping"
else
# If this job is NOT the current job, check the status
echo "JOB: $JOB_NAME"
echo "JOB NUM: $JOB_NUMBER"
echo "STATUS: $JOB_STATUS"
if [ "$JOB_STATUS" == "success" ];
then
echo "Job $JOB_NAME $JOB_NUMBER is complete - $JOB_STATUS"
elif [ "$JOB_STATUS" == "failed" ];
then
echo "Job $JOB_NAME $JOB_NUMBER Failed, breaking immediately - $JOB_STATUS"
break 2
elif [ "$JOB_STATUS" == "on_hold" ]; #|| [ "$JOB_STATUS" == "blocked" ];
then
# The condition to not block metrics sending when workflow use manually approved steps or is blocked.
echo "Job $JOB_NAME $JOB_NUMBER need manual approval - $JOB_STATUS - skipping"
else
# If it is still running, then mark WF_FINISHED as false.
WF_FINISHED=false
echo "Setting status of WF_FINISHED to false"
fi
fi
echo "rerunning loop"
i="$((i+1))"
echo "increment loop to $i"
echo " ---------- "
echo
done
echo "Waiting 10 seconds"
sleep 10
done
echo
################
# WF COMPLETE #
################
echo
echo "-------------------------------"
echo "All jobs in Workflow complete."
echo "Sending final report."
echo "-------------------------------"
echo
echo
########################################
# Send end-of-workflow data to Sumologic
########################################
WF_SL_PAYLOAD=$(curl -s "https://circleci.com/api/v2/workflow/$CIRCLE_WORKFLOW_ID?circle-token=${CIRCLE_TOKEN}" | jq '.')
########################################
# Send end-of-workflow jobs data to Sumologic
########################################
SLACK_JOBS_FIELDS=$(echo '[]' | jq .)
FINAL_STATUS='success'
FAILED_REASON=''
echo "SENDING FINAL JOB INFORMATION"
i=0
while [ "$i" -lt "$WF_LENGTH" ]
do
echo "looping: $i"
# fetch the job info
JOB_DATA=$(echo "$WF_DATA" | jq --arg i "$i" ".[$i]")
JOB_NUMBER=$(echo "$JOB_DATA" | jq ".job_number")
JOB_STATUS=$(echo "$JOB_DATA" | jq -r ".status")
JOB_NAME=$(echo "$JOB_DATA" | jq -r ".name")
# Only check the job if it is not this current job
if [ "$JOB_NUMBER" = "$CIRCLE_BUILD_NUM" ];
then
echo "This is the reporter job. Skipping"
else
# If this job is NOT the current job, check the status
echo "JOB: $JOB_NAME"
echo "JOB NUM: $JOB_NUMBER"
echo "STATUS: $JOB_STATUS"
# If $JOB_NUMBER is null, probably a blocked/queued Job.
if [ "$JOB_NUMBER" = "null" ];
then
echo "skipping raw Data for JOB"
BUILD_URL="${CIRCLE_WORKFLOW_URL}"
else
# yamllint disable-line rule:line-length
JOB_DATA_RAW=$(curl -s "https://circleci.com/api/v1.1/project/$VCS/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/$JOB_NUMBER?circle-token=${CIRCLE_TOKEN}")
# removing steps and circle_yml keys from object
JOB_DATA_RAW=$(echo $JOB_DATA_RAW | jq 'del(.circle_yml)' | jq 'del(.steps)')
# manually set job name as it is currently null
JOB_DATA_RAW=$(echo $JOB_DATA_RAW | jq --arg JOBNAME "$JOB_NAME" '.job_name = $JOBNAME')
BUILD_URL=$(echo "$JOB_DATA_RAW" | jq -r ".build_url")
SLACK_MSG_USER=$(echo "$JOB_DATA_RAW" | jq ".user")
SLACK_MSG_AUTHOR=$(echo "$JOB_DATA_RAW" | jq -r ".author_name")
fi
if [ "$JOB_STATUS" == "failed" ];
then
echo "Job $JOB_NAME $JOB_NUMBER failed"
FINAL_STATUS='failed'
SLACK_JOBS_FIELDS_EMOJI=':x:'
elif [ "$JOB_STATUS" == "success" ];
then
SLACK_JOBS_FIELDS_EMOJI=':white_check_mark:'
else
SLACK_JOBS_FIELDS_EMOJI=':no_entry_sign:'
fi
SLACK_JOBS_FIELDS=$(echo "$SLACK_JOBS_FIELDS" | jq --arg job "${SLACK_JOBS_FIELDS_EMOJI} *<${BUILD_URL}|$JOB_NAME>*" '. += [
{
"type": "mrkdwn",
"text": $job
}
]')
echo "$JOB_DATA_RAW"
fi
echo "rerunning loop"
i="$((i+1))"
echo "increment loop to $i"
echo " ---------- "
echo
done
echo "SLACK_JOBS_FIELDS=$SLACK_JOBS_FIELDS"
duration=$SECONDS
SLACK_MSG_DURATION=$(echo "$(($duration / 60)) mins and $(($duration % 60)) secs")
if [ "$FINAL_STATUS" == "failed" ]; then
if [ "$PREVIOUS_BUILD_STATUS" == "success" ]; then
echo "Build was broken"
SLACK_MSG_STATE_TITLE="${CIRCLE_USERNAME} broke the build!"
SLACK_MSG_STATE_GIF=$FAIL_GIF
SLACK_MSG_STATE_GIF_ALT=$GIPHY_SUCCESS_KEYWORD
fi
SLACK_MSG_HEADER=":x: ${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
SLACK_MSG_COLOUR='#ed5c5c'
else
if [ "$PREVIOUS_BUILD_STATUS" == "failed" ]; then
echo "Build was fixed"
SLACK_MSG_STATE_TITLE="${CIRCLE_USERNAME} Fixed the build!"
SLACK_MSG_STATE_GIF=$SUCCESS_GIF
SLACK_MSG_STATE_GIF_ALT=$GIPHY_FAILURE_KEYWORD
fi
SLACK_MSG_HEADER=":white_check_mark: ${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
SLACK_MSG_COLOUR='#36a64f'
fi
echo "Preparing initial SLACK_MSG_ATTACHMENT"
SLACK_MSG_ATTACHMENT=$(echo "{ \"attachments\": [ { \"blocks\": [], \"color\": \"${SLACK_MSG_COLOUR}\" }] }" | jq .)
if [ -z ${SLACK_MSG_STATE_TITLE+x} ]; then
echo "No State Change happenned"
else
echo "SLACK_MSG_STATE_TITLE=$SLACK_MSG_STATE_TITLE"
echo "SLACK_MSG_STATE_GIF=$SLACK_MSG_STATE_GIF"
echo "SLACK_MSG_STATE_GIF_ALT=$SLACK_MSG_STATE_GIF_ALT"
SLACK_MSG_ATTACHMENT=$(echo "$SLACK_MSG_ATTACHMENT" | jq --arg msg "$SLACK_MSG_STATE_TITLE" --arg giphy "$SLACK_MSG_STATE_GIF" --arg alt "$SLACK_MSG_STATE_GIF_ALT" '.attachments[0].blocks += [
{
"type": "header",
"text": {
"type": "plain_text",
"text": $msg,
"emoji": true
}
},
{
"type": "image",
"image_url": $giphy,
"alt_text": $alt
}
]')
fi
echo "Preparing SLACK_MSG_HEADER"
SLACK_MSG_ATTACHMENT=$(echo "$SLACK_MSG_ATTACHMENT" | jq --arg msg "$SLACK_MSG_HEADER" '.attachments[0].blocks += [
{
"type": "header",
"text": {
"type": "plain_text",
"text": $msg,
"emoji": true
}
}
]')
echo "CIRCLE_BUILD_URL=$CIRCLE_BUILD_URL"
echo "CIRCLE_WORKFLOW_ID=$CIRCLE_WORKFLOW_ID"
echo "CIRCLE_PULL_REQUEST=$CIRCLE_PULL_REQUEST"
echo "CIRCLE_PROJECT_REPONAME=$CIRCLE_PROJECT_REPONAME"
SLACK_MSG_BRANCH="*Branch:* <https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/tree/${CIRCLE_BRANCH}|${CIRCLE_BRANCH}>"
SLACK_MSG_COMMIT="*Commit:* <https://github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/commit/${CIRCLE_SHA1}|${CIRCLE_SHA1:0:8}>"
SLACK_MSG_NO_COMMITS="*Number of Commits:* $GIT_NO_COMMITS"
SLACK_MSG_DURATION="*Duration:* $SLACK_MSG_DURATION"
SLACK_MSG_PR="*<$CIRCLE_PULL_REQUEST|Pull Request>*"
SLACK_MSG_ATTACHMENT=$(echo "$SLACK_MSG_ATTACHMENT" | jq --arg job "*Job:* <$CIRCLE_WORKFLOW_URL|$WORKFLOW_NAME>" --arg branch "$SLACK_MSG_BRANCH" --arg duration "$SLACK_MSG_DURATION" --arg commit "$SLACK_MSG_COMMIT" --arg pr "$SLACK_MSG_PR" --arg no_commits "$SLACK_MSG_NO_COMMITS" '.attachments[0].blocks += [
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": $job
},
{
"type": "mrkdwn",
"text": $branch
},
{
"type": "mrkdwn",
"text": $duration
},
{
"type": "mrkdwn",
"text": $no_commits
},
{
"type": "mrkdwn",
"text": $commit
},
{
"type": "mrkdwn",
"text": $pr
}
]
}
]')
echo "Attaching SLACK_JOBS_FIELDS"
echo $SLACK_JOBS_FIELDS
SLACK_MSG_ATTACHMENT=$(echo "$SLACK_MSG_ATTACHMENT" | jq --argjson fields "$SLACK_JOBS_FIELDS" '.attachments[0].blocks += [
{
"type": "section",
"fields": $fields
}
]')
SLACK_MSG_USER_AVATAR=$(echo "$SLACK_MSG_USER" | jq -r ".avatar_url")
echo "Building SLACK_ELEMENTS_FIELDS"
SLACK_ELEMENTS_FIELDS=$(echo '[]' | jq .)
if [ -z ${SLACK_MSG_USER_AVATAR+x} ]; then
echo "No Avatar detected skipping..."
else
echo "SLACK_ELEMENTS_FIELDS ....1"
SLACK_ELEMENTS_FIELDS=$(echo "$SLACK_ELEMENTS_FIELDS" | jq --arg avatar "${SLACK_MSG_USER_AVATAR}" --arg author "${SLACK_MSG_AUTHOR}" '. += [
{
"type": "image",
"image_url": $avatar,
"alt_text": $author
}
]')
fi
echo "SLACK_ELEMENTS_FIELDS ....2"
SLACK_ELEMENTS_FIELDS=$(echo "$SLACK_ELEMENTS_FIELDS" | jq --arg commit "${GIT_COMMIT_DESC}" --arg author "Author: ${SLACK_MSG_AUTHOR}" '. += [
{
"type": "plain_text",
"text": $author,
"emoji": true
},
{
"type": "plain_text",
"text": $commit,
"emoji": true
}
]')
echo "Attaching $SLACK_ELEMENTS_FIELDS"
echo $SLACK_ELEMENTS_FIELDS
SLACK_MSG_ATTACHMENT=$(echo "$SLACK_MSG_ATTACHMENT" | jq --argjson elements "$SLACK_ELEMENTS_FIELDS" '.attachments[0].blocks += [
{
"type": "context",
"elements": $elements
}
]')
echo "$SLACK_MSG_ATTACHMENT"
_RES=$(curl -s -X POST -H 'Content-type: application/json' --no-keepalive --data "$SLACK_MSG_ATTACHMENT" ${SLACK_WEBHOOK})
echo "RESULT $_RES"
if [ ! "$_RES" = "ok" ]; then
exit 1
fi
echo "Slack status sent"