Gitlab container registry tag expiration policy not working - regex format? schedule?

Viewed 1645

I'm trying the Gitlab "CI/CD > Container Registry tag expiration policy" setting, and so far it's not deleting anything. We use semantic versioning (with a "v" prepended), and my goal is to automatically delete old "patch" releases:

  • Keep all major & minor tags: vM.0.0 and vM.m.0.
  • Delete all but a few recent tags matching vM.m.p (where p is not zero)

Given that I'm enabling this new setting on an old project, it's risky to find my answer by experimentation. Even on a new project, experimenting would take a lot of effort and calendar time. A dry-run or preview option would be really nice here, so I could try out the settings without fear of deleting important tags.

I tried the following "expire" regex: v[1-9][0-9]*[.][0-9][0-9]*[.][1-9][0-9]*. So far it hasn't had any effect. Which leaves me wondering:

  • How often does this run? Do I just need to wait longer?
  • Am I mis-understanding the way this setting works?
  • Is my regex bad?
  • What regex format is expected, even?
  • Is there any way to see feedback on this cleanup, like maybe in the project activity log?

My current approach is to tweak this setting once a day, then check my tags list the next day to see if it had any effect.

I'd appreciate general advice for verifying/troubleshooting this setting, and/or specific suggestions for how to match my particular version scheme.

Here's a screenshot of my current settings:

Gitlab CI/CD container expiration policy

1 Answers

I eventually gave up on this and took a different approach. Probably the most frustrating part was wondering when it runs. Is it once a day at a regular time? A random time each day? Once after every push to the registry? I was never sure how long to wait and see if my settings changes made a difference.

Instead I found an API method that exposes all of the same options. I actually like the API better than a project setting

  • I can see more clearly how & when it runs.
  • I can see error messages and results.
  • I can track the config in git, in .gitlab-ci.yml, rather than having to document a separate project setting.

https://docs.gitlab.com/ee/api/container_registry.html#delete-registry-repository-tags-in-bulk gives an overview and some example curls. Here's how I added it to my pipeline:

# In before_script:
- apk --update add curl

...

# In the job script:
    # Get registry id. Assumptions: valid response, "id" is first field, and project only has one registry.
    - REGISTRY_ID=`curl --header "PRIVATE-TOKEN:$API_TOKEN" "https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/registry/repositories" | cut -d, -f1 | cut -f2 -d:`
    - TAGS_URL=https://gitlab.com/api/v4/projects/$CI_PROJECT_ID/registry/repositories/$REGISTRY_ID/tags
    - curl --request DELETE --data 'keep_n=10' --data 'older_than=1week' --data "name_regex_delete=v[0-9][.].*" --data "name_regex_keep=.*[.]0" --header "PRIVATE-TOKEN:$API_TOKEN" "$TAGS_URL"

Using the API, I was able to quickly figure out which regex patterns worked. It's not immediate, but it seems to take effect within a minute. I'm assuming I could take those same regexes and use them in the project settings, but I'm happier sticking with the API for now.

Related