diff --git a/solutions/project/main.tf b/solutions/project/main.tf index 9f0bef4..624621a 100644 --- a/solutions/project/main.tf +++ b/solutions/project/main.tf @@ -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" diff --git a/solutions/project/scripts/build-run.sh b/solutions/project/scripts/build-run.sh index a9bc947..1c0646b 100755 --- a/solutions/project/scripts/build-run.sh +++ b/solutions/project/scripts/build-run.sh @@ -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 @@ -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 diff --git a/solutions/project/version.tf b/solutions/project/version.tf index 365303a..ade5ce9 100644 --- a/solutions/project/version.tf +++ b/solutions/project/version.tf @@ -10,9 +10,5 @@ terraform { source = "hashicorp/external" version = "2.3.5" } - null = { - source = "hashicorp/null" - version = "3.2.4" - } } }