Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/cleanup-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ on:
description: "🗑️ Clean up AWS AMIs (deregister + delete snapshots)"
default: true
type: boolean
cleanup_gce:
description: "🗑️ Clean up GCE images and GCS tarballs"
default: false
type: boolean
aws_ami_regions:
description: "AWS regions to clean up AMIs from (comma-separated)"
type: string
Expand All @@ -38,6 +42,10 @@ on:
default: false
type: boolean

permissions:
id-token: write
contents: read

jobs:
cleanup:
name: Cleanup images for ${{ inputs.image_suffix }}
Expand All @@ -54,8 +62,17 @@ jobs:
x64_image_name="postgres-ubuntu-2204-x64-${{ inputs.image_suffix }}"
arm64_image_name="postgres-ubuntu-2204-arm64-${{ inputs.image_suffix }}"

# GCE image names cannot contain dots; build workflow sanitizes
# them the same way. Mirror that here so cleanup matches what
# was actually created.
suffix="${{ inputs.image_suffix }}"
x64_gce_image_name="postgres-ubuntu-2204-x64-${suffix//./-}"
arm64_gce_image_name="postgres-ubuntu-2204-arm64-${suffix//./-}"

echo "x64_image_name=${x64_image_name}" >> $GITHUB_OUTPUT
echo "arm64_image_name=${arm64_image_name}" >> $GITHUB_OUTPUT
echo "x64_gce_image_name=${x64_gce_image_name}" >> $GITHUB_OUTPUT
echo "arm64_gce_image_name=${arm64_gce_image_name}" >> $GITHUB_OUTPUT

echo "### Target Images" >> $GITHUB_STEP_SUMMARY
if [ "${{ inputs.architecture }}" = "x64" ] || [ "${{ inputs.architecture }}" = "both" ]; then
Expand Down Expand Up @@ -287,6 +304,65 @@ jobs:
fi
done

- name: Authenticate to GCP
if: ${{ inputs.cleanup_gce }}
id: gcp_auth
uses: google-github-actions/auth@v2
with:
workload_identity_provider: ${{ vars.WIF_PROVIDER }}
service_account: ${{ vars.WIF_SERVICE_ACCOUNT }}

- name: Set up Cloud SDK
if: ${{ inputs.cleanup_gce }}
uses: google-github-actions/setup-gcloud@v2

- name: Cleanup GCE images
if: ${{ inputs.cleanup_gce }}
run: |
project="${{ steps.gcp_auth.outputs.project_id }}"
bucket="${{ secrets.GCS_BUCKET }}"

echo "### GCE Cleanup" >> $GITHUB_STEP_SUMMARY

cleanup_gce_image() {
local image_name=$1
local arch=$2

if gcloud compute images describe "${image_name}" --project="${project}" &>/dev/null; then
if [ "${{ inputs.dry_run }}" = "true" ]; then
echo "[DRY RUN] Would delete GCE image: ${image_name}"
echo "- [DRY RUN] Would delete ${arch} image: ${image_name}" >> $GITHUB_STEP_SUMMARY
else
gcloud compute images delete "${image_name}" --project="${project}" --quiet
echo "Deleted GCE image: ${image_name}"
echo "- Deleted ${arch} image: ${image_name}" >> $GITHUB_STEP_SUMMARY
fi
else
echo "GCE image not found: ${image_name}"
echo "- ${arch} image not found: ${image_name}" >> $GITHUB_STEP_SUMMARY
fi

local tar_file="${image_name}.tar.gz"
if gcloud storage ls "gs://${bucket}/${tar_file}" &>/dev/null; then
if [ "${{ inputs.dry_run }}" = "true" ]; then
echo "[DRY RUN] Would delete tarball: gs://${bucket}/${tar_file}"
echo "- [DRY RUN] Would delete ${arch} tarball: ${tar_file}" >> $GITHUB_STEP_SUMMARY
else
gcloud storage rm "gs://${bucket}/${tar_file}"
echo "Deleted tarball: gs://${bucket}/${tar_file}"
echo "- Deleted ${arch} tarball: ${tar_file}" >> $GITHUB_STEP_SUMMARY
fi
fi
}

if [ "${{ inputs.architecture }}" = "x64" ] || [ "${{ inputs.architecture }}" = "both" ]; then
cleanup_gce_image "${{ steps.set_image_names.outputs.x64_gce_image_name }}" "x64"
fi

if [ "${{ inputs.architecture }}" = "arm64" ] || [ "${{ inputs.architecture }}" = "both" ]; then
cleanup_gce_image "${{ steps.set_image_names.outputs.arm64_gce_image_name }}" "arm64"
fi

- name: Summary
run: |
echo "" >> $GITHUB_STEP_SUMMARY
Expand Down
Loading
Loading