From 7e95d614527dbc1d70980f91cb5067376cfd9efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Brunner?= Date: Thu, 12 Nov 2020 17:46:22 +0100 Subject: [PATCH] Add Clean Docker hub tags workflow --- .github/workflows/clean-dockerhub-tag.yaml | 23 ++++++++++ ci/clean-dockerhub-tag | 53 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 .github/workflows/clean-dockerhub-tag.yaml create mode 100755 ci/clean-dockerhub-tag diff --git a/.github/workflows/clean-dockerhub-tag.yaml b/.github/workflows/clean-dockerhub-tag.yaml new file mode 100644 index 000000000..2f8a91205 --- /dev/null +++ b/.github/workflows/clean-dockerhub-tag.yaml @@ -0,0 +1,23 @@ +--- +name: Clean docker hub tags + +on: delete + +jobs: + clean: + runs-on: ubuntu-18.04 + name: Clean docker hub tags + env: + SUMMON_PROVIDER: /usr/local/bin/gopass + steps: + - uses: actions/checkout@v1 + - uses: camptocamp/initialise-gopass-summon-action@v1 + with: + ci-gpg-private-key: ${{secrets.CI_GPG_PRIVATE_KEY}} + github-gopass-ci-token: ${{secrets.GITHUB_GOPASS_CI_TOKEN}} + - name: Clean docker hub tags + run: | + summon --yaml ' + USERNAME: !var gs/ci/dockerhub/username + PASSWORD: !var gs/ci/dockerhub/password + ' scripts/clean-dockerhub-tag diff --git a/ci/clean-dockerhub-tag b/ci/clean-dockerhub-tag new file mode 100755 index 000000000..24b569d04 --- /dev/null +++ b/ci/clean-dockerhub-tag @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import json +import os +import re +import subprocess +import sys + +import requests + + +def clean(image): + token = requests.post( + "https://hub.docker.com/v2/users/login/", + headers={"Content-Type": "application/json"}, + data=json.dumps({"username": os.environ["USERNAME"], "password": os.environ["PASSWORD"]}), + ).json()["token"] + + with open(os.environ["GITHUB_EVENT_PATH"]) as event_file: + ref = json.loads(event_file.read())["ref"] + + ref = ref.replace("/", "_") + + print("Delete image '{image}:{tag}'.".format(image=image, tag=ref)) + + response = requests.head( + "https://hub.docker.com/v2/repositories/{image}/tags/{tag}/".format(image=image, tag=ref), + headers={"Authorization": "JWT " + token}, + ) + if response.status_code == 404: + return + if not response.ok: + print("Error checking image '{image}:{tag}' status.".format(image=image, tag=ref)) + print(response.text) + sys.exit(2) + + response = requests.delete( + "https://hub.docker.com/v2/repositories/{image}/tags/{tag}/".format(image=image, tag=ref), + headers={"Authorization": "JWT " + token}, + ) + if not response.ok: + print("Error on deleting tag: " + ref) + print(response.text) + sys.exit(2) + + +def main(): + clean("camptocamp/c2cwsgiutils") + + +if __name__ == "__main__": + main()