diff --git a/templates/.gitlab-ci-template.yml b/templates/.gitlab-ci-template.yml index 70698be3..062b316b 100644 --- a/templates/.gitlab-ci-template.yml +++ b/templates/.gitlab-ci-template.yml @@ -89,12 +89,35 @@ variables: if command -v docker-compose &> /dev/null; then docker-compose version fi - + section_end "docker" fi if command -v gcloud &> /dev/null; then section_start "gcloud" "Gcloud authentication setup" + GCP_SERVICE_ACCOUNT=$(gcloud auth list --filter=status:ACTIVE --format="value(account)") + + # if service account is empty, wait for it to be set. + if [ -z "${GCP_SERVICE_ACCOUNT}" ]; then + MAX_ATTEMPTS=3 + i=0 + while [ $i -lt $MAX_ATTEMPTS ]; do + echo "Waiting for gcloud to authenticate..." + sleep 5s + GCP_SERVICE_ACCOUNT=$(gcloud auth list --filter=status:ACTIVE --format="value(account)") + if [ -n "${GCP_SERVICE_ACCOUNT}" ]; then + break + fi + i=$((i+1)) + done + fi + + # if still empty, fail. + if [ -z "${GCP_SERVICE_ACCOUNT}" ]; then + echo "Failed to authenticate with gcloud after multiple attempts." + exit 1 + fi + gcloud version gcloud auth configure-docker --quiet gcloud auth configure-docker europe-west1-docker.pkg.dev --quiet @@ -102,7 +125,8 @@ variables: echo "The following docker credHelpers are configured:" jq '.credHelpers' ~/.docker/config.json fi - printf "\n\n%-${PAD_LEN}s \e[1m%s\e[0m\n\n" "GCP Auth user (workload identity):" $(gcloud auth list --filter=status:ACTIVE --format="value(account)") + + printf "\n\n%-${PAD_LEN}s \e[1m%s\e[0m\n\n" "GCP Auth user (workload identity): ${GCP_SERVICE_ACCOUNT}" section_end "gcloud" fi @@ -125,7 +149,7 @@ variables: # Handle debug sleep. section_start "debug-sleep" "Print debug information" print_debug_sleep_help - + # Default to 1 hour if not set DEBUG_JOB_SLEEP_SECONDS="${DEBUG_JOB_SLEEP_SECONDS:-3600}" if [ "${DEBUG_JOB_SLEEP}" = "1" ] && [ "${CI_JOB_NAME_SLUG}" = "${DEBUG_JOB_SLEEP_JOB_NAME}" ]; then