generated from jonico/pscale-workflow-helper-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwait-for-branch-readiness.sh
executable file
·50 lines (46 loc) · 1.65 KB
/
wait-for-branch-readiness.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
function wait_for_branch_readiness {
local retries=$1
local db=$2
local branch=${3,,}
local org=$4
# check whether fifth parameter is set, otherwise use default value
if [ -z "$5" ]; then
local max_timeout=60
else
local max_timeout=$5
fi
local count=0
local wait=1
echo "Checking if branch $branch is ready for use..."
while true; do
local raw_output=`pscale branch list $db --org $org --format json`
# check return code, if not 0 then error
if [ $? -ne 0 ]; then
echo "Error: pscale branch list returned non-zero exit code $?: $raw_output"
return 1
fi
local output=`echo $raw_output | jq ".[] | select(.name == \"$branch\") | .ready"`
# test whether output is false, if so, increase wait timeout exponentially
if [ "$output" == "false" ]; then
# increase wait variable exponentially but only if it is less than max_timeout
if [ $((wait * 2)) -le $max_timeout ]; then
wait=$((wait * 2))
else
wait=$max_timeout
fi
count=$((count+1))
if [ $count -ge $retries ]; then
echo "Branch $branch is not ready after $retries retries. Exiting..."
return 2
fi
echo "Branch $branch is not ready yet. Retrying in $wait seconds..."
sleep $wait
elif [ "$output" == "true" ]; then
echo "Branch $branch is ready for use."
return 0
else
echo "Branch $branch in unknown status: $raw_output"
return 3
fi
done
}