-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcancel-redundant-builds.sh
executable file
·86 lines (76 loc) · 2.06 KB
/
cancel-redundant-builds.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
#!/usr/bin/env bash
echo-error () {
echo "ERROR: $*"
}
var-set () {
local val
val=$(eval "echo \$$1")
[ -n "$val" ]
}
require-var () {
local var
for var in "$@";do
if ! var-set "$var";then
echo-error "$var not defined"
exit 1
fi
done
}
abort-on-error () {
if [ $? -ne 0 ]; then
echo-error "$@"
exit 1
fi
}
require-var CIRCLE_BUILD_NUM
if [ -n "$CIRCLECI" ];then
PROJECT_VCS_TYPE=${PROJECT_VCS_TYPE:-'github'}
require-var PROJECT_VCS_TYPE CIRCLE_PROJECT_USERNAME CIRCLE_PROJECT_REPONAME CIRCLE_BRANCH
BASE_API_URL="https://circleci.com/api/v1.1/project/$PROJECT_VCS_TYPE/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/tree/$CIRCLE_BRANCH"
else
require-var BASE_API_URL
CIRCLE_TOKEN=''
fi
circle-get () {
if [[ "$BASE_API_URL" != https://circleci.com/* ]];then
cat "$BASE_API_URL.json"
else
require-var CIRCLE_TOKEN
curl --silent \
-H "Content-Type: application/json" \
"${BASE_API_URL}?circle-token=${CIRCLE_TOKEN}"
fi
}
node-date() {
local date="$1"
if [ -n "$date" ] && [ "$date" != 'null' ];then
node -e "console.log(new Date(\"${date}\"))"
fi
}
v=$(circle-get)
abort-on-error "getting builds from CircleCI: $v"
require-var v
if [ "$(echo "$v" | jq)" == '[]' ];then
echo 'No builds found. Got empty response from CircleCI'
exit 0
fi
current_build=$(echo "$v" | jq --raw-output ".[] | select(.build_num==$CIRCLE_BUILD_NUM)")
abort-on-error "finding current build: $current_build"
if [ -z "$current_build" ];then
echo 'Could not find current build'
exit 0
fi
require-var current_build
current_workflow_id=$(echo "${current_build}" | jq --raw-output ".workflows.workflow_id")
require-var current_workflow_id
current_author_date=$(node-date "$(echo "${current_build}" | jq --raw-output ".author_date")")
matches=$(echo "${v}" | jq --raw-output ".[] | select(.workflows.workflow_id!=\"$current_workflow_id\") | .author_date")
if [ -n "$current_author_date" ];then
for date in ${matches};do
date=$(node-date "$date")
if [[ "$date" > "$current_author_date" ]]; then
echo "Newer builds found $date > $current_author_date"
exit 1
fi
done
fi