v3/.github/workflows/gar-cleanup.yml

44 lines
1.5 KiB
YAML

name: GAR Cleanup
on:
schedule:
- cron: '30 6 * * *'
workflow_dispatch: {}
workflow_run:
workflows: ['Deploy']
types:
- completed
jobs:
docker_cleanup:
name: Cleanup Deployed Images
runs-on: ubuntu-latest
strategy:
matrix:
region: [us, europe, asia]
steps:
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GCS_PROJECT_ID }}
service_account_key: ${{ secrets.GCS_TF_KEY }}
export_default_credentials: true
- name: Delete stale artifacts
run: |
flags="--format=json --location=${{matrix.region}} --repository=roleypoly"
packagesToPrune=$(gcloud artifacts packages list $flags | jq -r '.[].name')
for package in $packagesToPrune; do
tagsToKeep=$(gcloud artifacts tags list --package=$package $flags | jq -r '.[].version')
versionsToCheck=$(gcloud artifacts versions list --package=$package $flags | jq -r '.[].name')
for version in $versionsToCheck; do
if [[ "$tagsToKeep" =~ .*"$version".* ]]; then
continue
fi
gcloud artifacts versions delete $version --package=$package $flags
done
done