Skip to content

Commit 3519489

Browse files
authored
fix: wait till build run is finished before next resource is executed (#206)
1 parent de8575f commit 3519489

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

solutions/project/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ module "build" {
8989
timeout = each.value.timeout
9090
}
9191

92-
resource "null_resource" "run_build" {
93-
count = length(local.updated_builds) > 0 ? 1 : 0
92+
resource "terraform_data" "run_build" {
9493
depends_on = [module.build]
94+
9595
provisioner "local-exec" {
9696
interpreter = ["/bin/bash", "-c"]
9797
command = "${path.module}/scripts/build-run.sh"

solutions/project/scripts/build-run.sh

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/bin/bash
22
set -e
33

4+
# max wait time = 60 × 10s = 10 minutes
5+
MAX_RETRIES=60
6+
RETRY_INTERVAL=10 # seconds
7+
48
if [[ -z "${IBMCLOUD_API_KEY}" ]]; then
59
echo "IBMCLOUD_API_KEY is required" >&2
610
exit 1
@@ -32,7 +36,33 @@ ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_ID}" --quiet
3236
ibmcloud ce project select -n "${CE_PROJECT_NAME}"
3337

3438
# run a build for all builds
39+
# we check for status build to be succeeded before we finish with script.
40+
# This is needed in a case we deploy app, which needs build_run to be finished.
3541
for build in $BUILDS; do
36-
echo "$build"
37-
ibmcloud ce buildrun submit --build "$build"
42+
echo "Submitting build: $build"
43+
run_build_name=$(ibmcloud ce buildrun submit --build "$build" --output json | jq -r '.name')
44+
45+
echo "Waiting for build run $run_build_name to complete..."
46+
retries=0
47+
while true; do
48+
status=$(ibmcloud ce buildrun get --name "$run_build_name" --output json | jq -r '.status')
49+
echo "Status: $status"
50+
if [[ "$status" == "succeeded" ]]; then
51+
echo "Build $build succeeded"
52+
break
53+
54+
elif [[ "$status" == "Failed" || "$status" == "Error" ]]; then
55+
echo "Build $build failed"
56+
exit 1
57+
fi
58+
59+
# if max time timeout then finish with error
60+
if [[ $retries -ge $MAX_RETRIES ]]; then
61+
echo "Build $build did not complete after $MAX_RETRIES retries. Timing out."
62+
exit 1
63+
fi
64+
65+
retries=$((retries + 1))
66+
sleep "$RETRY_INTERVAL"
67+
done
3868
done

solutions/project/version.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,5 @@ terraform {
1010
source = "hashicorp/external"
1111
version = "2.3.5"
1212
}
13-
null = {
14-
source = "hashicorp/null"
15-
version = "3.2.4"
16-
}
1713
}
1814
}

0 commit comments

Comments
 (0)