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

Add Clean docker hub tags workflow #754

Merged
merged 1 commit into from
Nov 13, 2020
Merged
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
19 changes: 19 additions & 0 deletions .github/workflows/clean-dockerhub-tag.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
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: scripts/clean-dockerhub-tag
57 changes: 57 additions & 0 deletions ci/clean-dockerhub-tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import os
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": subprocess.check_call(["gopass", "gs/ci/dockerhub/username"]),
"password": subprocess.check_call(["gopass", "gs/ci/dockerhub/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()