Skip to content
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

feat: check if gcloud is authenticated otherwise fail (15s) #222

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
30 changes: 27 additions & 3 deletions templates/.gitlab-ci-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,44 @@ 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
Comment on lines +98 to +119
Copy link
Contributor

@Monska85 Monska85 Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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
if [ "${DISABLE_GCP_SERVICE_ACCOUNT_CHECK:-0}" != "1" ]; then
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
fi

I would prefer to have a method to skip this check. What do you think?


gcloud version
gcloud auth configure-docker --quiet
gcloud auth configure-docker europe-west1-docker.pkg.dev --quiet
if command -v jq &> /dev/null; then
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

Expand All @@ -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
Expand Down
Loading