I’ve created GitHub Actions workflow that build and push docker images to Artifact Registry with docker/build-push-action and deploy to Cloud Run.
This is my Artifact Registry. The tagged package is deployed to Cloud Run.
Does anyone have a good idea to delete all unused or unreferred images in Artifact Registry in GitHub Actions?
I can delete them by hand on console or GCP web console, but I wanna automate it.
My idea was to list all images except those tagged “${GITHUB_SHA}”, sorted by updated and delete them after tagged image appear.
gcloud container images list-tags "${BASE_IMAGE}"
--filter="NOT tags:${GITHUB_SHA}" --format="table(digest, tags)" |
tail -n +2 |
while read row
do
arr=($row)
if [ ${#arr[@]} -eq 2 ]; then
start="true"
fi
if [[ -v start ]]; then
digest=${arr[0]}
gcloud container images delete -q --force-delete-tags "${BASE_IMAGE}@sha256:$digest"
fi
done
But I couldn’t list sorted by updated because all layer-cache images’ timestamp are UNIX epoch (1970/01/01 00:00:00).
Docker Image Timestamp Issue
So sometimes gcloud container images delete
try to delete not-unreferred images and GitHub Actions workflow fails with this error.
Manifest is still referenced by one or more parent images: ocarry-api/manifests/sha256:93eec65fc56910c8c8012a385e7769fae87885f6459bc8e1613855730ef07007: None
this is my part of GitHub Actions yaml
- name: Build and push
id: docker-build
uses: docker/build-push-action@v6
with:
platforms: linux/amd64
context: .
push: true
tags: ${{ env.IMAGE }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
service: /* my service id */
image: ${{ env.IMAGE }}
region: ${{ env.GCP_REGION }}
- name: Clean up Container images
run: |
gcloud container images list-tags "${BASE_IMAGE}"
/* the shell script above continues */