forked from otto-de/github-actions-wait
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait.sh
executable file
·118 lines (89 loc) · 4.05 KB
/
wait.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
#! /usr/bin/env bash
set -exo pipefail
if [ -z "$*" ] || [ "$#" != 1 ]; then
echo "Only 1 input \"the step name\" is required as argument to the script. Number of arguments present are $#"
echo "Arguments received are $*"
exit 1
fi
step_name=$1
echo "DEBUGGING: step_name is $step_name"
SCRIPT_DIR="$(cd "$(dirname "$0")" ; pwd -P)"
echo "Script dir is $SCRIPT_DIR"
echo "DEBUGGING: beginning"
GITHUB_WORKFLOW_URL=$(curl -sH "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" | \
jq -r '.workflow_url')
# Show the current run_number of the workflow run
echo "CURRENT_RUN_NUMBER is ${GITHUB_RUN_NUMBER}"
run_while=true
while [ $run_while ];
do
# If the number of runs is 1 then no further checking is needed
IN_PROGRESS_RUNNING=$(curl -sH "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_WORKFLOW_URL}/runs" | \
jq -r ".workflow_runs[] | select(.status == \"in_progress\")| .id" | wc -l)
echo "IN_PROGRESS_RUNNING is $IN_PROGRESS_RUNNING"
if [[ $IN_PROGRESS_RUNNING -eq 1 ]]; then
echo "No further checking is required. break"
run_while=false
break
else
# Get the number of workflows which are started before the current run
IN_PROGRESS_PREVIOUS=$(curl -sH "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_WORKFLOW_URL}/runs" | \
jq -r ".workflow_runs[] | select((.status == \"in_progress\") and (.run_number < ${GITHUB_RUN_NUMBER}))| .id" | wc -l)
echo "IN_PROGRESS_PREVIOUS is $IN_PROGRESS_PREVIOUS"
if [[ $IN_PROGRESS_PREVIOUS -gt 0 ]]; then
echo "Previous runs are still running. Waiting for 10 seconds"
sleep 10
else
echo "Validate for rerun. Better to use != than > since != would also \
consider any new previous run as well"
IN_PROGRESS_OTHER=$(curl -sH "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_WORKFLOW_URL}/runs" | \
jq -r ".workflow_runs[] | select((.status == \"in_progress\") and (.run_number != ${GITHUB_RUN_NUMBER}))| .id")
echo "IN_PROGRESS_OTHER is $IN_PROGRESS_OTHER"
IN_PROGRESS_OTHER_COUNT="${#IN_PROGRESS_OTHER[@]}"
if [[ $IN_PROGRESS_OTHER_COUNT -gt 0 ]]; then
echo "Wait till all other runs which have crossed wait action to complete"
for id in $IN_PROGRESS_OTHER ; do
# Get the run id of the run which is currently running terraform commands
IN_PROGRESS_ID_RUNNING=$(curl -sH "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/$id/jobs" | \
jq -r ".jobs[].steps[] | select((.status == \"completed\") and (.name == \"$step_name\"))| .name" | wc -l)
if [[ $IN_PROGRESS_ID_RUNNING -gt 0 ]]; then
echo "DEBUGGING: Have to wait till the run status of the run with id $id is completed"
echo "DEBUGGING: Get the status of the run with id $id"
run_inner_while=true
while [ $run_inner_while ]; do
IN_PROGRESS_ID_STATUS=$(curl -sH "Authorization: Bearer ${GITHUB_TOKEN}" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/actions/runs/$id" | \
jq -r ". | \
select(.status == \"completed\")| .id" | wc -l)
echo "verify if the already running github run is completed"
if [[ IN_PROGRESS_ID_STATUS -eq 1 ]]; then
echo "Workflow run with id $id is completed. break"
run_inner_while=false
break
else
echo "Workflow run with id $id is not yet completed. Waiting for 10 seconds"
sleep 10
fi
done
else
echo "For the run with id $id, the $step_name step is not completed. Continue \
probing other run ids which are in progress."
continue
fi
done
echo "There is no other run running which has crossed waitforpipeline step. break"
run_while=false
break
else
echo "No other run is in progress. break"
run_while=false
break
fi
fi
fi
done