diff --git a/README.md b/README.md index fbd48f5..201f92e 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,40 @@ jobs: token: ${{ secrets.PAT }} ``` +An example showing 2 different retention policies based on image tags format: + +```yaml +name: Delete old container images + +on: + schedule: + - cron: '0 0 0 * *' # the first day of the month + +jobs: + clean-ghcr: + name: Delete old unused container images + runs-on: ubuntu-latest + steps: + - name: Delete old released images + uses: snok/container-retention-policy@v1 + with: + image-names: dev/* + cut-off: One month ago UTC + keep-at-least: 5 + filter-tags: "v*.*.*" + account-type: personal + token: ${{ secrets.PAT }} + - name: Delete old pre-release images + uses: snok/container-retention-policy@v1 + with: + image-names: dev/* + cut-off: One week ago UTC + keep-at-least: 1 + filter-tags: "rc*", "dev*" + account-type: personal + token: ${{ secrets.PAT }} +``` + # Parameters ## image-names @@ -183,8 +217,9 @@ with access to the container registry. Specifically, you need to grant it the fo How many versions to keep no matter what. Defaults to 0, meaning all versions older than the `cut-off` date may be deleted. -Setting this to a larger value ensures that the specified number of recent versions are always retained, regardless of -their age. Useful for images that are not updated very often. +Setting this to a larger value ensures that the specified number of recent versions are always retained, regardless of their age. Useful for images that are not updated very often. + +If used together with `filter-tags` parameter, `keep-at-least` number of image tags will be skipped from the resulting filtered set, which makes it possible to apply different retention policies based on image tag format. ## untagged-only diff --git a/main.py b/main.py index a86db10..f1af2a8 100644 --- a/main.py +++ b/main.py @@ -328,10 +328,6 @@ async def get_and_delete_old_versions(image_name: str, inputs: Inputs, http_clie account_type=inputs.account_type, org_name=inputs.org_name, image_name=image_name, http_client=http_client ) - # Trim the version list to the n'th element we want to keep - if inputs.keep_at_least > 0: - versions = versions[inputs.keep_at_least :] - # Define list of deletion-tasks to append to tasks = [] @@ -400,6 +396,12 @@ async def get_and_delete_old_versions(image_name: str, inputs: Inputs, http_clie ) ) + # Trim the version list to the n'th element we want to keep + if inputs.keep_at_least > 0: + for _i in range(0, min(inputs.keep_at_least, len(tasks))): + tasks[0].cancel() + tasks.remove(tasks[0]) + if not tasks: print(f'No more versions to delete for {image_name}')