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 workflow to delete old docker images #269

Merged
merged 1 commit into from
Sep 19, 2024
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
30 changes: 30 additions & 0 deletions .github/workflows/clean_dockerhub_images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Docker Cleanup

on:
schedule:
- cron: '0 12 * * 1' # Runs every Monday at noon (UTC)
workflow_dispatch:

jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Check out the repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: "3.x"

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install requests

- name: Run Docker image cleanup
run: make clean-old-temporary-docker-images
env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
DELETE_AFTER_DAYS: 30
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Makefile to run the Docker cleanup script

clean-old-temporary-docker-images:
@echo "Running Docker Hub image cleanup script..."
python ci/clean-old-temporary-docker-images.py
58 changes: 58 additions & 0 deletions ci/clean-old-temporary-docker-images.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import os
import requests
from datetime import datetime, timedelta

DOCKERHUB_USERNAME = os.environ["DOCKERHUB_USERNAME"]
DOCKERHUB_TOKEN = os.environ["DOCKERHUB_TOKEN"]
DELETE_AFTER_DAYS = os.environ["DELETE_AFTER_DAYS"]

def get_docker_token(username, password):
url = "https://hub.docker.com/v2/users/login/"
headers = {"Content-Type": "application/json"}
data = {"username": username, "password": password}

response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
return response.json()["token"]
else:
print(f"Failed to login to DockerHub: {response.status_code}")
return None

def get_repo_tags(token):
url = f"https://hub.docker.com/v2/repositories/scylladb/gocql-extended-ci/tags/"
headers = {"Authorization": f"Bearer {token}"}
response = requests.get(url, headers=headers)
if response.status_code != 200:
print(f"Failed to get tags, Status Code: {response.status_code}, {response.text}")
return None
return response.json()["results"]

def delete_tag(tag, token):
url = f"https://hub.docker.com/v2/repositories/scylladb/gocql-extended-ci/tags/{tag}/"
headers = {"Authorization": f"Bearer {token}"}
response = requests.delete(url, headers=headers)
if response.status_code > 200 and response.status_code < 300:
print(f"Deleted tag: {tag}")
return True
print(f"Failed to delete tag: {tag}, Status Code: {response.status_code}")
return False

def clean_old_images():
token = get_docker_token(DOCKERHUB_USERNAME, DOCKERHUB_TOKEN)
if token is None:
return False
tags = get_repo_tags(token)
if tags is None:
return False
threshold_date = datetime.now() - timedelta(days=int(DELETE_AFTER_DAYS))
status = True
for tag in tags:
last_updated = datetime.strptime(tag["last_updated"], "%Y-%m-%dT%H:%M:%S.%fZ")
if last_updated < threshold_date:
status = status and delete_tag(tag["name"], token)
return status

if __name__ == "__main__":
if not clean_old_images():
exit(1)
exit(0)
Loading