This can be done via Gcloud which means it can be done from the CLI or in a code pipeline (say at the end of CD).
As documented by Google, you can collect a list of all untagged images with:
gcloud container images list-tags [HOSTNAME]/[PROJECT-ID]/[IMAGE] --filter='-tags:*' --format="get(digest)" --limit=$BIG_NUMBER
And then delete an image with:
gcloud container images delete [HOSTNAME]/[PROJECT-ID]/[IMAGE]@DIGEST --quiet
where the above command is run for each output (DIGEST) from the first command.
A rough scripted example would be running the following post gcloud auth:
gcloud container images list-tags gcr.io/myProject/myApp --filter='-tags:*' --format="get(digest)" --limit=10 > tags && while read p; do gcloud container images delete "gcr.io/myProject/myApp@$p" --quiet; done < tags
A Github actions post CD image cleanup task would look like:
needs: [CI, Build_myApp]
runs-on: ubuntu-latest
steps:
- name: 'Authenticate to Gcloud'
uses: google-github-actions/setup-gcloud@master
with:
project_id: myProject
service_account_email: myServiceAccount@myProject.iam.gserviceaccount.com
service_account_key: ${{ secrets.CONTAINER_ADMIN_NP_SA }}
export_default_credentials: true
- name: 'Cleanup untagged images in nonprod'
run: gcloud container images list-tags gcr.io/myProject/myApp --filter='-tags:*' --format="get(digest)" --limit=10 > tags && while read p; do gcloud container images delete "gcr.io/myProject/myApp@$p" --quiet; done < tags