From ed4012266090712dadaf3f31f2bd51c8becaf768 Mon Sep 17 00:00:00 2001 From: Michele Cantelli Date: Fri, 13 Aug 2021 17:17:14 +0200 Subject: [PATCH 1/6] Updating readme --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index fd706e0..1a51ea6 100644 --- a/README.md +++ b/README.md @@ -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): ``` From a1fd50eed15d3f2ea98f1dfb77ab3f5eed9a3066 Mon Sep 17 00:00:00 2001 From: Michele Cantelli Date: Fri, 13 Aug 2021 17:17:29 +0200 Subject: [PATCH 2/6] --order-by-date now supports tags filtering --- registry.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/registry.py b/registry.py index 6214786..3358acc 100755 --- a/registry.py +++ b/registry.py @@ -797,10 +797,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: From 89a92db985d3efc15e9c56e4421ece1aa1cb3118 Mon Sep 17 00:00:00 2001 From: Michele Cantelli Date: Fri, 13 Aug 2021 17:26:23 +0200 Subject: [PATCH 3/6] Adding github action that pushes new releases on github registry --- .github/workflows/docker-push.yaml | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 .github/workflows/docker-push.yaml diff --git a/.github/workflows/docker-push.yaml b/.github/workflows/docker-push.yaml new file mode 100644 index 0000000..a423f8c --- /dev/null +++ b/.github/workflows/docker-push.yaml @@ -0,0 +1,41 @@ +name: Create and publish a Docker image + +on: + push: + branches: ['release'] + +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 }} \ No newline at end of file From cee1d9e103e44e57e3a1e8912b6c02eddedfcf40 Mon Sep 17 00:00:00 2001 From: Michele Cantelli Date: Fri, 13 Aug 2021 17:29:20 +0200 Subject: [PATCH 4/6] Changing docker push triggers --- .github/workflows/docker-push.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/docker-push.yaml b/.github/workflows/docker-push.yaml index a423f8c..8ea00c3 100644 --- a/.github/workflows/docker-push.yaml +++ b/.github/workflows/docker-push.yaml @@ -2,7 +2,13 @@ name: Create and publish a Docker image on: push: - branches: ['release'] + branches: + - 'master' + tags: + - 'v*' + pull_request: + branches: + - 'master' env: REGISTRY: ghcr.io From e4888c8f2e1d0642a9861e6f78f60042fbfc9ff3 Mon Sep 17 00:00:00 2001 From: Michele Cantelli Date: Fri, 13 Aug 2021 18:18:00 +0200 Subject: [PATCH 5/6] Deleting tag before deleting manifest In order to avoid errors on GCP like `{"errors":[{"code":"GOOGLE_MANIFEST_DANGLING_TAG","message":"Manifest is still referenced by tag: xxx"}]}` --- registry.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/registry.py b/registry.py index 3358acc..b593aa2 100755 --- a/registry.py +++ b/registry.py @@ -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") From d28098caee1b43ae1d720a401d405bc7b205bf9f Mon Sep 17 00:00:00 2001 From: Michele Cantelli Date: Sun, 15 Aug 2021 05:54:50 +0200 Subject: [PATCH 6/6] Fixing test reflecting changes on e4888c8f2e1d0642a9861e6f78f60042fbfc9ff3 --- test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.py b/test.py index 861b318..7821123 100644 --- a/test.py +++ b/test.py @@ -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",