Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCR support + --order-by-date now supports tag filtering #105

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/workflows/docker-push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Create and publish a Docker image

on:
push:
branches:
- 'master'
tags:
- 'v*'
pull_request:
branches:
- 'master'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Log in to the Container registry
uses: docker/login-action@v1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ In the following case, all tags beginning with "snapshot-" will be deleted, save
```
The last parameter is also available as regexp option with `--keep-tags-like`.

You can also use `--order-by-date` options in order to sort images and tags by date instead of tag name, useful if your tag names are not in a fixed order.

```
registry.py -l user:pass -r https://example.com:5000 --tags-like "snapshot-" --order-by-date
```

Delete tag starting with `snapshot-` keeping the latest 5, ordered by date
```
registry.py -l user:pass -r https://example.com:5000 --delete --tags-like "snapshot-" --order-by-date --num 5
```

Delete all tags for particular image (e.g. delete all ubuntu tags):
```
Expand Down
22 changes: 19 additions & 3 deletions registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ def delete_tag(self, image_name, tag, dry_run, tag_digests_to_ignore):
if tag_digest is None:
return False

# delete tag reference
delete_result = self.send("/v2/{0}/manifests/{1}".format(
image_name, tag), method="DELETE")

if delete_result is None:
print("failed, error: {0}".format(self.last_error))
print(get_error_explanation("delete_tag", self.last_error))
return False

# delete tag digest
delete_result = self.send("/v2/{0}/manifests/{1}".format(
image_name, tag_digest), method="DELETE")

Expand Down Expand Up @@ -797,10 +807,16 @@ def main_loop(args):
print(" no tags!")
continue

if args.order_by_date:
tags_list = get_ordered_tags(registry, image_name, all_tags_list, args.order_by_date)
else:

if args.tags_like:
tags_list = get_tags(all_tags_list, image_name, args.tags_like)
else:
tags_list = all_tags_list

if args.order_by_date:
tags_list = get_ordered_tags(registry, image_name, tags_list, args.order_by_date)
#else:
# tags_list = get_tags(all_tags_list, image_name, args.tags_like)

# print(tags and optionally layers
for tag in tags_list:
Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def test_delete_tag_ok(self):
response = self.registry.delete_tag(
'image1', 'test_tag', False, keep_tag_digests)
self.assertEqual(response, True)
self.assertEqual(self.registry.http.request.call_count, 2)
self.assertEqual(self.registry.http.request.call_count, 3)
self.registry.http.request.assert_called_with(
"DELETE",
"http://testdomain.com/v2/image1/manifests/MOCK_DIGEST_HEADER",
Expand Down