|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | + |
| 16 | +substitutions: |
| 17 | + _REGION: "us-central1" |
| 18 | + _ZONE: "us-central1-a" |
| 19 | + _SHORT_BUILD_ID: ${BUILD_ID:0:8} |
| 20 | + _VM_NAME: "py-sdk-sys-test-${_SHORT_BUILD_ID}" |
| 21 | + _ULIMIT: "10000" # 10k, for gRPC bidi streams |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +steps: |
| 26 | + # Step 0: Generate a persistent SSH key for this build run. |
| 27 | + # This prevents gcloud from adding a new key to the OS Login profile on every ssh/scp command. |
| 28 | + - name: "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 29 | + id: "generate-ssh-key" |
| 30 | + entrypoint: "bash" |
| 31 | + args: |
| 32 | + - "-c" |
| 33 | + - | |
| 34 | + mkdir -p /workspace/.ssh |
| 35 | + # Generate the SSH key |
| 36 | + ssh-keygen -t rsa -f /workspace/.ssh/google_compute_engine -N '' -C gcb |
| 37 | + # Save the public key content to a file for the cleanup step |
| 38 | + cat /workspace/.ssh/google_compute_engine.pub > /workspace/gcb_ssh_key.pub |
| 39 | + waitFor: ["-"] |
| 40 | + |
| 41 | + - name: "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 42 | + id: "cleanup-old-keys" |
| 43 | + entrypoint: "bash" |
| 44 | + args: |
| 45 | + - "-c" |
| 46 | + - | |
| 47 | + #!/bin/bash |
| 48 | + set -e |
| 49 | +
|
| 50 | + echo "Fetching OS Login SSH keys..." |
| 51 | + echo "Removing all keys." |
| 52 | + echo "---------------------------------------------------------------------" |
| 53 | +
|
| 54 | + FINGERPRINTS_TO_DELETE=$$(gcloud compute os-login ssh-keys list \ |
| 55 | + --format="value(fingerprint)") |
| 56 | +
|
| 57 | + echo "Keys to delete: $$FINGERPRINTS_TO_DELETE" |
| 58 | +
|
| 59 | + if [ -z "$$FINGERPRINTS_TO_DELETE" ]; then |
| 60 | + echo "No keys found to delete. Nothing to do." |
| 61 | + exit 0 |
| 62 | + fi |
| 63 | +
|
| 64 | + while IFS= read -r FINGERPRINT; do |
| 65 | + if [ -n "$$FINGERPRINT" ]; then |
| 66 | + echo "Deleting key with fingerprint: $$FINGERPRINT" |
| 67 | + gcloud compute os-login ssh-keys remove \ |
| 68 | + --key="$$FINGERPRINT" \ |
| 69 | + --quiet || true |
| 70 | + fi |
| 71 | + done <<< "$$FINGERPRINTS_TO_DELETE" |
| 72 | +
|
| 73 | + echo "---------------------------------------------------------------------" |
| 74 | + echo "Cleanup complete." |
| 75 | +
|
| 76 | + # Step 1 Create a GCE VM to run the tests. |
| 77 | + # The VM is created in the same zone as the buckets to test rapid storage features. |
| 78 | + # It's given the 'cloud-platform' scope to allow it to access GCS and other services. |
| 79 | + - name: "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 80 | + id: "create-vm" |
| 81 | + entrypoint: "gcloud" |
| 82 | + args: |
| 83 | + - "compute" |
| 84 | + - "instances" |
| 85 | + - "create" |
| 86 | + - "${_VM_NAME}" |
| 87 | + - "--project=${PROJECT_ID}" |
| 88 | + - "--zone=${_ZONE}" |
| 89 | + - "--machine-type=e2-medium" |
| 90 | + - "--image-family=debian-13" |
| 91 | + - "--image-project=debian-cloud" |
| 92 | + - "--service-account=${_ZONAL_VM_SERVICE_ACCOUNT}" |
| 93 | + - "--scopes=https://www.googleapis.com/auth/devstorage.full_control,https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/devstorage.read_write" |
| 94 | + - "--metadata=enable-oslogin=TRUE" |
| 95 | + waitFor: ["-"] |
| 96 | + |
| 97 | + # Step 2: Run the integration tests inside the newly created VM and cleanup. |
| 98 | + # This step uses 'gcloud compute ssh' to execute a remote script. |
| 99 | + # The VM is deleted after tests are run, regardless of success. |
| 100 | + - name: "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 101 | + id: "run-tests-and-delete-vm" |
| 102 | + entrypoint: "bash" |
| 103 | + args: |
| 104 | + - "-c" |
| 105 | + - | |
| 106 | + set -e |
| 107 | + # Wait for the VM to be fully initialized and SSH to be ready. |
| 108 | + for i in {1..10}; do |
| 109 | + if gcloud compute ssh ${_VM_NAME} --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine --command="echo VM is ready"; then |
| 110 | + break |
| 111 | + fi |
| 112 | + echo "Waiting for VM to become available... (attempt $i/10)" |
| 113 | + sleep 15 |
| 114 | + done |
| 115 | + # copy the script to the VM |
| 116 | + gcloud compute scp storage/cloudbuild/run_zonal_tests.sh ${_VM_NAME}:~ --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine |
| 117 | +
|
| 118 | + # Execute the script on the VM via SSH. |
| 119 | + # Capture the exit code to ensure cleanup happens before the build fails. |
| 120 | + set +e |
| 121 | + gcloud compute ssh ${_VM_NAME} --zone=${_ZONE} --internal-ip --ssh-key-file=/workspace/.ssh/google_compute_engine --command="ulimit -n ${_ULIMIT}; COMMIT_SHA=${COMMIT_SHA} _ZONAL_BUCKET=${_ZONAL_BUCKET} CROSS_REGION_BUCKET=${_CROSS_REGION_BUCKET} _PR_NUMBER=${_PR_NUMBER} bash run_zonal_tests.sh" |
| 122 | + EXIT_CODE=$? |
| 123 | + set -e |
| 124 | +
|
| 125 | + echo "--- Deleting GCE VM ---" |
| 126 | + gcloud compute instances delete "${_VM_NAME}" --zone=${_ZONE} --quiet |
| 127 | +
|
| 128 | + # Exit with the original exit code from the test script. |
| 129 | + exit $$EXIT_CODE |
| 130 | + waitFor: |
| 131 | + - "create-vm" |
| 132 | + - "generate-ssh-key" |
| 133 | + - "cleanup-old-keys" |
| 134 | + |
| 135 | + - name: "gcr.io/google.com/cloudsdktool/cloud-sdk" |
| 136 | + id: "cleanup-ssh-key" |
| 137 | + entrypoint: "bash" |
| 138 | + args: |
| 139 | + - "-c" |
| 140 | + - | |
| 141 | + echo "--- Removing SSH key from OS Login profile to prevent accumulation ---" |
| 142 | + gcloud compute os-login ssh-keys remove \ |
| 143 | + --key-file=/workspace/gcb_ssh_key.pub || true |
| 144 | + waitFor: |
| 145 | + - "run-tests-and-delete-vm" |
| 146 | + |
| 147 | +timeout: "3600s" # 60 minutes |
| 148 | + |
| 149 | +options: |
| 150 | + logging: CLOUD_LOGGING_ONLY |
| 151 | + pool: |
| 152 | + name: "projects/${PROJECT_ID}/locations/us-central1/workerPools/cloud-build-worker-pool" |
0 commit comments