|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -ex |
| 4 | + |
| 5 | +# Clean up old nightly builds from DockerHub, keeping only the last 14 builds |
| 6 | +# This script uses DockerHub API to list and delete old tags with "nightly-" prefix |
| 7 | + |
| 8 | +# DockerHub API endpoint for vllm/vllm-openai repository |
| 9 | +REPO_API_URL="https://hub.docker.com/v2/repositories/vllm/vllm-openai/tags" |
| 10 | + |
| 11 | +# Get DockerHub token from environment |
| 12 | +if [ -z "$DOCKERHUB_TOKEN" ]; then |
| 13 | + echo "Error: DOCKERHUB_TOKEN environment variable is not set" |
| 14 | + exit 1 |
| 15 | +fi |
| 16 | + |
| 17 | +# Function to get all tags from DockerHub |
| 18 | +get_all_tags() { |
| 19 | + local page=1 |
| 20 | + local all_tags="" |
| 21 | + |
| 22 | + while true; do |
| 23 | + local response=$(curl -s -H "Authorization: Bearer $DOCKERHUB_TOKEN" \ |
| 24 | + "$REPO_API_URL?page=$page&page_size=100") |
| 25 | + |
| 26 | + # Get both last_updated timestamp and tag name, separated by | |
| 27 | + local tags=$(echo "$response" | jq -r '.results[] | select(.name | startswith("nightly-")) | "\(.last_updated)|\(.name)"') |
| 28 | + |
| 29 | + if [ -z "$tags" ]; then |
| 30 | + break |
| 31 | + fi |
| 32 | + |
| 33 | + all_tags="$all_tags$tags"$'\n' |
| 34 | + page=$((page + 1)) |
| 35 | + done |
| 36 | + |
| 37 | + # Sort by timestamp (newest first) and extract just the tag names |
| 38 | + echo "$all_tags" | sort -r | cut -d'|' -f2 |
| 39 | +} |
| 40 | + |
| 41 | +delete_tag() { |
| 42 | + local tag_name="$1" |
| 43 | + echo "Deleting tag: $tag_name" |
| 44 | + |
| 45 | + local delete_url="https://hub.docker.com/v2/repositories/vllm/vllm-openai/tags/$tag_name" |
| 46 | + local response=$(curl -s -X DELETE -H "Authorization: Bearer $DOCKERHUB_TOKEN" "$delete_url") |
| 47 | + |
| 48 | + if echo "$response" | jq -e '.detail' > /dev/null 2>&1; then |
| 49 | + echo "Warning: Failed to delete tag $tag_name: $(echo "$response" | jq -r '.detail')" |
| 50 | + else |
| 51 | + echo "Successfully deleted tag: $tag_name" |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +# Get all nightly- prefixed tags, sorted by last_updated timestamp (newest first) |
| 56 | +echo "Fetching all tags from DockerHub..." |
| 57 | +all_tags=$(get_all_tags) |
| 58 | + |
| 59 | +if [ -z "$all_tags" ]; then |
| 60 | + echo "No tags found to clean up" |
| 61 | + exit 0 |
| 62 | +fi |
| 63 | + |
| 64 | +# Count total tags |
| 65 | +total_tags=$(echo "$all_tags" | wc -l) |
| 66 | +echo "Found $total_tags tags" |
| 67 | + |
| 68 | +# Keep only the last 14 builds (including the current one) |
| 69 | +tags_to_keep=14 |
| 70 | +tags_to_delete=$((total_tags - tags_to_keep)) |
| 71 | + |
| 72 | +if [ $tags_to_delete -le 0 ]; then |
| 73 | + echo "No tags need to be deleted (only $total_tags tags found, keeping $tags_to_keep)" |
| 74 | + exit 0 |
| 75 | +fi |
| 76 | + |
| 77 | +echo "Will delete $tags_to_delete old tags, keeping the newest $tags_to_keep" |
| 78 | + |
| 79 | +# Get tags to delete (skip the first $tags_to_keep tags) |
| 80 | +tags_to_delete_list=$(echo "$all_tags" | tail -n +$((tags_to_keep + 1))) |
| 81 | + |
| 82 | +if [ -z "$tags_to_delete_list" ]; then |
| 83 | + echo "No tags to delete" |
| 84 | + exit 0 |
| 85 | +fi |
| 86 | + |
| 87 | +# Delete old tags |
| 88 | +echo "Deleting old tags..." |
| 89 | +while IFS= read -r tag; do |
| 90 | + if [ -n "$tag" ]; then |
| 91 | + delete_tag "$tag" |
| 92 | + # Add a small delay to avoid rate limiting |
| 93 | + sleep 1 |
| 94 | + fi |
| 95 | +done <<< "$tags_to_delete_list" |
| 96 | + |
| 97 | +echo "Cleanup completed successfully" |
0 commit comments