main
sh 132 lines 4.05 KB
Raw
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 specified prefix
7 # Tags starting with "nightly-dev" are always excluded from deletion
8 # Usage: cleanup-nightly-builds.sh [TAG_PREFIX] [REPO]
9 # Example: cleanup-nightly-builds.sh "nightly-"
10 # Example: cleanup-nightly-builds.sh "cu130-nightly-"
11 # Example: cleanup-nightly-builds.sh "nightly-" "vllm/vllm-openai-rocm"
12
13 # Get tag prefix and repo from arguments
14 TAG_PREFIX="${1:-nightly-}"
15 REPO="${2:-vllm/vllm-openai}"
16
17 echo "Cleaning up tags with prefix: $TAG_PREFIX in repository: $REPO"
18
19 # DockerHub API endpoint for the repository
20 REPO_API_URL="https://hub.docker.com/v2/repositories/${REPO}/tags"
21
22 # Get DockerHub credentials from environment
23 if [ -z "$DOCKERHUB_TOKEN" ]; then
24 echo "Error: DOCKERHUB_TOKEN environment variable is not set"
25 exit 1
26 fi
27
28 if [ -z "$DOCKERHUB_USERNAME" ]; then
29 echo "Error: DOCKERHUB_USERNAME environment variable is not set"
30 exit 1
31 fi
32
33 # Get DockerHub bearer token
34 echo "Getting DockerHub bearer token..."
35 set +x
36 BEARER_TOKEN=$(curl -s -X POST \
37 -H "Content-Type: application/json" \
38 -d "{\"username\": \"$DOCKERHUB_USERNAME\", \"password\": \"$DOCKERHUB_TOKEN\"}" \
39 "https://hub.docker.com/v2/users/login" | jq -r '.token')
40 set -x
41
42 if [ -z "$BEARER_TOKEN" ] || [ "$BEARER_TOKEN" = "null" ]; then
43 echo "Error: Failed to get DockerHub bearer token"
44 exit 1
45 fi
46
47 # Function to get all tags from DockerHub
48 get_all_tags() {
49 local page=1
50 local all_tags=""
51
52 while true; do
53 set +x
54 local response=$(curl -s -H "Authorization: Bearer $BEARER_TOKEN" \
55 "$REPO_API_URL?page=$page&page_size=100")
56 set -x
57
58 # Get both last_updated timestamp and tag name, separated by |
59 # Exclude mutable architecture tags and nightly-dev tags from cleanup
60 local tags=$(echo "$response" | jq -r --arg prefix "$TAG_PREFIX" '.results[] | select(.name | startswith($prefix)) | select(.name != ($prefix + "x86_64") and .name != ($prefix + "aarch64")) | select(.name | startswith("nightly-dev") | not) | "\(.last_updated)|\(.name)"')
61
62 if [ -z "$tags" ]; then
63 break
64 fi
65
66 all_tags="$all_tags$tags"$'\n'
67 page=$((page + 1))
68 done
69
70 # Sort by timestamp (newest first) and extract just the tag names
71 echo "$all_tags" | sort -r | cut -d'|' -f2
72 }
73
74 delete_tag() {
75 local tag_name="$1"
76 echo "Deleting tag: $tag_name"
77
78 local delete_url="https://hub.docker.com/v2/repositories/${REPO}/tags/$tag_name"
79 set +x
80 local response=$(curl -s -X DELETE -H "Authorization: Bearer $BEARER_TOKEN" "$delete_url")
81 set -x
82
83 if echo "$response" | jq -e '.detail' > /dev/null 2>&1; then
84 echo "Warning: Failed to delete tag $tag_name: $(echo "$response" | jq -r '.detail')"
85 else
86 echo "Successfully deleted tag: $tag_name"
87 fi
88 }
89
90 # Get all nightly- prefixed tags, sorted by last_updated timestamp (newest first)
91 echo "Fetching all tags from DockerHub..."
92 all_tags=$(get_all_tags)
93
94 if [ -z "$all_tags" ]; then
95 echo "No tags found to clean up"
96 exit 0
97 fi
98
99 # Count total tags
100 total_tags=$(echo "$all_tags" | wc -l)
101 echo "Found $total_tags tags"
102
103 # Keep only the last 14 builds (including the current one)
104 tags_to_keep=14
105 tags_to_delete=$((total_tags - tags_to_keep))
106
107 if [ $tags_to_delete -le 0 ]; then
108 echo "No tags need to be deleted (only $total_tags tags found, keeping $tags_to_keep)"
109 exit 0
110 fi
111
112 echo "Will delete $tags_to_delete old tags, keeping the newest $tags_to_keep"
113
114 # Get tags to delete (skip the first $tags_to_keep tags)
115 tags_to_delete_list=$(echo "$all_tags" | tail -n +$((tags_to_keep + 1)))
116
117 if [ -z "$tags_to_delete_list" ]; then
118 echo "No tags to delete"
119 exit 0
120 fi
121
122 # Delete old tags
123 echo "Deleting old tags..."
124 while IFS= read -r tag; do
125 if [ -n "$tag" ]; then
126 delete_tag "$tag"
127 # Add a small delay to avoid rate limiting
128 sleep 1
129 fi
130 done <<< "$tags_to_delete_list"
131
132 echo "Cleanup completed successfully"