diff --git a/src/airgap/upload_all_bundles.sh b/src/airgap/upload_all_bundles.sh index c85d21eff..b5e3c744e 100755 --- a/src/airgap/upload_all_bundles.sh +++ b/src/airgap/upload_all_bundles.sh @@ -1,47 +1,96 @@ #!/bin/bash +set -euo pipefail + +# ======================== +# Functions +# ======================== upload_to_bucket() { - export GOOGLE_APPLICATION_CREDENTIALS=$1 - gsutil_command=("gsutil" "cp" "$2" "$3") + local cred_file="$1" + local source_file="$2" + local dest_path="$3" + + export GOOGLE_APPLICATION_CREDENTIALS="$cred_file" - if ! "${gsutil_command[@]}"; then - echo "An error occurred while uploading the file: $?" + echo "📤 Uploading $source_file to $dest_path ..." + if ! gsutil cp "$source_file" "$dest_path"; then + echo "❌ Error uploading $source_file" exit 1 fi - - echo "File $2 uploaded to $3." + echo "✅ Uploaded: $source_file → $dest_path" } -# Define the bucket path -bucket_path="gs://smp-airgap-bundles" +# ======================== +# Input Parsing +# ======================== -# Parse command line arguments -if [[ $# -ne 2 ]]; then - echo "Usage: $0 " +if [[ $# -ne 3 ]]; then + echo "Usage: $0 " + echo "Example: $0 /tmp/wif_cred.json 0.19.1 gs://smp-airgap-bundles" exit 1 fi -service_account_file=$1 +credential_file=$1 release_number=$2 +bucket_path=$3 + +# ======================== +# Auth Setup +# ======================== + +echo "🔐 Authenticating using $credential_file ..." + +# Detect whether this is a WIF config or a Service Account key +if grep -q '"credential_source"' "$credential_file"; then + echo "Detected Workload Identity Federation credentials" + export GOOGLE_APPLICATION_CREDENTIALS="$credential_file" +else + echo "Detected Service Account key" + gcloud auth activate-service-account --key-file="$credential_file" >/dev/null 2>&1 || { + echo "❌ Failed to activate service account" + exit 1 + } +fi -# Array of files to upload +gcloud auth list + +# ======================== +# Collect files +# ======================== + +files=() for moduleImageZipFile in $MODULE_IMAGE_ZIP_FILES; do files+=("$moduleImageZipFile") done -if [ ${#files[@]} -le 2 ]; then # validation with 2 to make sure multiple elements are in list +if [[ ${#files[@]} -le 2 ]]; then echo "Error: No module image zip files provided. Files: ${files[@]}" >&2 exit 1 fi -# Create an empty file and upload it to the destination bucket path +# ======================== +# Prepare bucket path +# ======================== + +release_path="${bucket_path%/}/$release_number" + +echo "📁 Preparing release folder: $release_path" touch empty_file -gsutil cp empty_file "$bucket_path/$release_number/" +gsutil cp empty_file "$release_path/" >/dev/null +rm -f empty_file + +# ======================== +# Upload files +# ======================== -# Iterate over the files and upload each one for file in "${files[@]}"; do - upload_to_bucket "$service_account_file" "$file" "$bucket_path/$release_number/$file" + upload_to_bucket "$credential_file" "$file" "$release_path/$(basename "$file")" done -# Remove the empty file from the release number directory -gsutil rm "$bucket_path/$release_number/empty_file" +# ======================== +# Cleanup +# ======================== + +gsutil rm "$release_path/empty_file" >/dev/null || true + +echo "🎉 Uploaded all bundles successfully: https://console.cloud.google.com/storage/browser/${bucket_path#gs://}/$release_number"