Skip to content

Commit

Permalink
Add Clean Docker hub tags workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Nov 13, 2020
1 parent b001714 commit 8fd7a5d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/clean-dockerhub-tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Clean docker hub tags

on: delete

jobs:
clean:
runs-on: ubuntu-20.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
53 changes: 53 additions & 0 deletions ci/clean-dockerhub-tag
Original file line number Diff line number Diff line change
@@ -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: str, tag: str, token: str) -> None:
print("Delete image '{image}:{tag}'.".format(image=image, tag=tag))

response = requests.head(
"https://hub.docker.com/v2/repositories/{image}/tags/{tag}/".format(image=image, tag=tag),
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=tag))
print(response.text)
sys.exit(2)

response = requests.delete(
"https://hub.docker.com/v2/repositories/{image}/tags/{tag}/".format(image=image, tag=tag),
headers={"Authorization": "JWT " + token},
)
if not response.ok:
print("Error on deleting tag: " + tag)
print(response.text)
sys.exit(2)


def main() -> None:
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("/", "_")

clean("camptocamp/c2cwsgiutils", ref, token)


if __name__ == "__main__":
main()

0 comments on commit 8fd7a5d

Please sign in to comment.