Zenn Tech Blog

iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😊

Deleting Old Artifact Registry Images During Build with gcr-cleaner

に公開
2023/04/06
Inside Zennの記事です

In my daily development, I build Ruby on Rails applications as container images and deploy them to Cloud Run. Since these images are tagged and generated anew with every deployment, the sheer number of images causes storage costs to become non-negligible.


Artifact Registry Pricing - Storage https://cloud.google.com/artifact-registry/pricing?hl=ja#storage

I am currently using around 100GB, which costs about $10. It is not an emergency, but it is always good to cut unnecessary costs where possible.

Running gcr-cleaner in Cloud Build

There are two main options for deleting container images in Artifact Registry:

  • Set a lifecycle policy using the repository's cleanup policies.
  • Use gcr-cleaner to delete them.

This is already well-documented (and essentially resolved) in the following scrap:

https://zenn.dev/catnose99/scraps/0d18ca2fe55241

Since I want to achieve "keep only the 10 most recent images and delete the rest," I decided to use gcr-cleaner.

Integrating with the Image Build Trigger

When deciding whether to integrate gcr-cleaner into an existing build or create a new trigger, I recommend mixing it into the existing build process. In our operations, we build container images and push them to Artifact Registry to deploy to Cloud Run. This approach offers several advantages:

  • You only need to add one set of service account permissions.
  • The image name is easy to specify.
  • Since it runs in tandem with the push of the build image, you can ensure the cleanup happens only after the image has been successfully pushed.

It requires minimal effort to implement. Please add the "Artifact Registry Administrator" role to your Cloud Build service account.

When the Artifact Registry Repository and Cloud Build Project Differ

As a supplementary note, depending on the project structure, you might manage Artifact Registry repositories in a central project. In that case, you will need to configure IAM permissions on the Artifact Registry repository. Here is an example using Terraform:

# Artifact Registry Repository
resource "google_artifact_registry_repository" "app" {
  provider   = google-beta
  project       = var.gcp_project_id
  location      = var.artifact_registry_location
  repository_id = "app"
  description   = "Application"
  format        = "DOCKER"
}

# Allow access from Cloud Build in a separate project.
resource "google_artifact_registry_repository_iam_binding" "binding_builder" {
  provider   = google-beta
  project    = var.gcp_project_id
  location   = var.artifact_registry_location
  repository = google_artifact_registry_repository.app.name
  role       = "roles/artifactregistry.repoAdmin"   # Set to repoAdmin here to allow deletion from Cloud Build
  members = [
    # Specify the Cloud Build service account
    "serviceAccount:xxxxxxxxxxxxxxx@cloudbuild.gserviceaccount.com",
  ]
}

Trying it out with -dry-run

If you are worried about images being deleted, you can specify the -dry-run option to only list the targets for deletion without actually removing them. Let's try it out.

cloudbuild.yml
- id: build-app # Build container image
- id: push-app  # Push container image
- id: deploy-app # Deploy to Cloud Run
- id: clean-backend-images
    name: asia-docker.pkg.dev/gcr-cleaner/gcr-cleaner/gcr-cleaner-cli:latest
    args:
      - -repo
      - $_ARTIFACT_REPOSITORY_IMAGE_NAME
      - -keep
      - "10"
      - -tag-filter-any
      - "^v.*" # Only images with tags starting with 'v', such as v1.1.234
      - -dry-run # Specify this
    waitFor: ["deploy-app"]
substitutions:
  _ARTIFACT_REPOSITORY_IMAGE_NAME: us-central1-docker.pkg.dev/${PROJECT_ID}/apps/backend-app

Here is how it looks when the trigger is executed:

It may appear that images not starting with v are also being deleted, but this is simply because the hashes corresponding to v1.101 were detected. Since both point to the same image, only those specified by -tag-filter-any will be deleted without issue.

Actually Deleting Images

Once you have confirmed that the targets are correct, you can remove the -dry-run option to proceed with deleting the images.

Summary

Running gcr-cleaner in Cloud Build offers the following benefits:

  • It allows for practical specifications, such as "keep 10 images and delete the rest," rather than relying on lifecycle policies.
  • By integrating it into the process that already builds and pushes images in Cloud Build, you save the effort of configuring permissions and specifying image names again.

Adding this step is a great way to save on storage costs, so please give it a try.

Zenn Tech Blog
Zenn Tech Blog

Discussion