#!/usr/bin/env bash # ============================================================================= # 00-install-gcloud.sh (Linux) # Installs the Google Cloud CLI (gcloud) on Debian/Ubuntu. # Run this once on a new machine before doing anything else. # # Windows users: run GCR/scripts/00-install-gcloud.ps1 in PowerShell instead. # ============================================================================= set -euo pipefail OS="$(uname -s)" if [[ "$OS" != "Linux" ]]; then echo "ERROR: This script is for Linux only." echo "Windows users: run GCR/scripts/00-install-gcloud.ps1" exit 1 fi echo ">>> Installing Google Cloud CLI on Linux (Debian/Ubuntu)..." apt_update_with_retry() { local attempts=5 local i for ((i=1; i<=attempts; i++)); do if sudo apt-get clean && sudo rm -rf /var/lib/apt/lists/* && sudo apt-get update; then return 0 fi echo " apt-get update failed (attempt ${i}/${attempts})." if (( i < attempts )); then echo " Retrying apt metadata refresh..." fi done echo "ERROR: apt-get update failed after ${attempts} attempts." echo "This is often a temporary mirror sync issue. Please try again in a few minutes." exit 1 } apt_install_with_retry() { local attempts=3 local i for ((i=1; i<=attempts; i++)); do if sudo apt-get install -y --no-install-recommends "$@"; then return 0 fi echo " apt-get install failed (attempt ${i}/${attempts}) for: $*" if (( i < attempts )); then echo " Retrying package install..." fi done echo "ERROR: apt-get install failed after ${attempts} attempts for: $*" exit 1 } # ── Install dependencies ─────────────────────────────────────────────────── apt_update_with_retry apt_install_with_retry \ apt-transport-https \ ca-certificates \ curl \ gnupg # ── Import the Google Cloud signing key ─────────────────────────────────── # Key is downloaded to a file rather than piped straight into gpg so it can # be inspected or cached by CI systems if needed. curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg \ -o /tmp/cloud.google.gpg sudo gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg /tmp/cloud.google.gpg rm /tmp/cloud.google.gpg # ── Add the apt repository ──────────────────────────────────────────────── echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] \ https://packages.cloud.google.com/apt cloud-sdk main" \ | sudo tee /etc/apt/sources.list.d/google-cloud-sdk.list # ── Install gcloud ──────────────────────────────────────────────────────── apt_update_with_retry apt_install_with_retry google-cloud-cli echo "" echo ">>> gcloud installed successfully." gcloud version echo "" echo ">>> Next step: run GCR/scripts/01-login.sh"