Skip to content

fix: wait till build run is finished before next resource is executed #206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions solutions/project/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ module "build" {
timeout = each.value.timeout
}

resource "null_resource" "run_build" {
count = length(local.updated_builds) > 0 ? 1 : 0
resource "terraform_data" "run_build" {
depends_on = [module.build]

provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
command = "${path.module}/scripts/build-run.sh"
Expand Down
34 changes: 32 additions & 2 deletions solutions/project/scripts/build-run.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/bash
set -e

# max wait time = 60 × 10s = 10 minutes
MAX_RETRIES=60
RETRY_INTERVAL=10 # seconds

if [[ -z "${IBMCLOUD_API_KEY}" ]]; then
echo "IBMCLOUD_API_KEY is required" >&2
exit 1
Expand Down Expand Up @@ -32,7 +36,33 @@ ibmcloud login -r "${REGION}" -g "${RESOURCE_GROUP_ID}" --quiet
ibmcloud ce project select -n "${CE_PROJECT_NAME}"

# run a build for all builds
# we check for status build to be succeeded before we finish with script.
# This is needed in a case we deploy app, which needs build_run to be finished.
for build in $BUILDS; do
echo "$build"
ibmcloud ce buildrun submit --build "$build"
echo "Submitting build: $build"
run_build_name=$(ibmcloud ce buildrun submit --build "$build" --output json | jq -r '.name')

echo "Waiting for build run $run_build_name to complete..."
retries=0
while true; do
status=$(ibmcloud ce buildrun get --name "$run_build_name" --output json | jq -r '.status')
echo "Status: $status"
if [[ "$status" == "succeeded" ]]; then
echo "Build $build succeeded"
break

elif [[ "$status" == "Failed" || "$status" == "Error" ]]; then
echo "Build $build failed"
exit 1
fi

# if max time timeout then finish with error
if [[ $retries -ge $MAX_RETRIES ]]; then
echo "Build $build did not complete after $MAX_RETRIES retries. Timing out."
exit 1
fi

retries=$((retries + 1))
sleep "$RETRY_INTERVAL"
done
done
4 changes: 0 additions & 4 deletions solutions/project/version.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,5 @@ terraform {
source = "hashicorp/external"
version = "2.3.5"
}
null = {
source = "hashicorp/null"
version = "3.2.4"
}
}
}