forked from QuantiModo/quantimodo-android-chrome-ios-web-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do-exclusively
83 lines (66 loc) · 1.77 KB
/
do-exclusively
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
#!/usr/bin/env bash
# sets $branch, $tag, $rest
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
-b|--branch) branch="$2" ;;
-t|--tag) tag="$2" ;;
*) break ;;
esac
shift 2
done
rest=("$@")
}
# reads $branch, $tag, $commit_message
should_skip() {
if [[ "$branch" && "$CIRCLE_BRANCH" != "$branch" ]]; then
echo "Not on branch $branch. Skipping..."
return 0
fi
if [[ "$tag" && "$commit_message" != *\[$tag\]* ]]; then
echo "No [$tag] commit tag found. Skipping..."
return 0
fi
return 1
}
# reads $branch, $tag
# sets $jq_prog
make_jq_prog() {
local jq_filters=""
if [[ $branch ]]; then
jq_filters+=" and .branch == \"$branch\""
fi
if [[ $tag ]]; then
jq_filters+=" and (.subject | contains(\"[$tag]\"))"
fi
jq_prog=".[] | select(.build_num < $CIRCLE_BUILD_NUM and (.status | test(\"running|pending|queued\")) $jq_filters) | .build_num"
}
if [[ "$0" != *bats* ]]; then
set -e
set -u
set -o pipefail
branch=""
tag=""
rest=()
api_url="https://circleci.com/api/v1/project/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME?circle-token=$CIRCLE_TOKEN&limit=100"
parse_args "$@"
commit_message=$(git log -1 --pretty=%B)
if should_skip; then exit 0; fi
make_jq_prog
echo "Checking for running builds..."
while true; do
builds=$(curl -s -H "Accept: application/json" "$api_url" | jq "$jq_prog")
if [[ $builds ]]; then
echo "Waiting on builds:"
echo "$builds"
else
break
fi
echo "Retrying in 5 seconds..."
sleep 5
done
echo "Acquired lock"
if [[ "${#rest[@]}" -ne 0 ]]; then
"${rest[@]}"
fi
fi