Skip to content

Commit f914a1f

Browse files
committed
Add retry for terraform apply
1 parent 59467cc commit f914a1f

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

internal/servicedeployer/_static/terraform_deployer_run.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,25 @@ cleanup() {
2121
}
2222
trap cleanup EXIT INT TERM
2323

24+
retry() {
25+
local retries=$1
26+
shift
27+
local count=0
28+
until "$@"; do
29+
exit=$?
30+
wait=$((2 ** count))
31+
count=$((count + 1))
32+
if [ $count -lt "$retries" ]; then
33+
>&2 echo "Retry $count/$retries exited $exit, retrying in $wait seconds..."
34+
sleep $wait
35+
else
36+
>&2 echo "Retry $count/$retries exited $exit, no more retries left."
37+
return $exit
38+
fi
39+
done
40+
return 0
41+
}
42+
2443
terraform init
2544
terraform plan
2645

@@ -29,7 +48,7 @@ echo "Before Terraform Apply command"
2948
aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\t' '\n'
3049

3150
export TF_LOG="DEBUG"
32-
terraform apply -auto-approve
51+
retry 3 terraform apply -auto-approve
3352

3453
echo "After Terraform Apply command"
3554
aws s3api list-buckets --query "Buckets[].Name" --output text | tr '\t' '\n'

0 commit comments

Comments
 (0)