Skip to content

Commit

Permalink
Merge pull request #48 from artazar/keep_at_least_filtered
Browse files Browse the repository at this point in the history
limit number of versions to keep only after filtering
  • Loading branch information
sondrelg authored Feb 12, 2023
2 parents 3863241 + 2d1b1bd commit 02b1c7a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down Expand Up @@ -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}')

Expand Down

0 comments on commit 02b1c7a

Please sign in to comment.