name: Cleanup Old Nightly Docker Tags # Reusable workflow: deletes old nightly Docker Hub tags, keeping the most recent ones. # Can also be triggered manually to clean up tags on demand. on: workflow_call: inputs: tag_prefixes: description: 'JSON array of tag prefixes to clean up (e.g. ["nightly-dev", "nightly-dev-cu13"])' required: true type: string keep_count: description: "Number of most recent tags to keep per prefix" required: false type: number default: 14 image_repo: description: "Docker Hub repo to clean up (e.g. lmsysorg/sglang-staging for testing)" required: false type: string default: "lmsysorg/sglang" workflow_dispatch: inputs: tag_prefixes: description: 'JSON array of tag prefixes to clean up (e.g. ["nightly-dev", "nightly-dev-cu13"])' required: true type: string keep_count: description: "Number of most recent tags to keep per prefix" required: false type: number default: 14 image_repo: description: "Docker Hub repo to clean up (e.g. lmsysorg/sglang-staging for testing)" required: false type: string default: "lmsysorg/sglang" jobs: cleanup: if: github.repository == 'sgl-project/sglang' runs-on: ubuntu-latest steps: - name: Cleanup old nightly tags env: TAG_PREFIXES: ${{ inputs.tag_prefixes }} KEEP_COUNT: ${{ inputs.keep_count }} IMAGE_REPO: ${{ inputs.image_repo }} run: | TOKEN=$(curl -s -H "Content-Type: application/json" \ -X POST -d '{"username": "${{ secrets.DOCKERHUB_USERNAME }}", "password": "${{ secrets.DOCKERHUB_TOKEN }}"}' \ https://hub.docker.com/v2/users/login/ | jq -r .token) TAGS_RESPONSE=$(curl -s -H "Authorization: JWT $TOKEN" \ "https://hub.docker.com/v2/repositories/${IMAGE_REPO}/tags/?page_size=100") echo "${TAG_PREFIXES}" | jq -r '.[]' | while read -r PREFIX; do echo "--- Checking prefix: ${PREFIX} ---" TAGS=$(echo "$TAGS_RESPONSE" | jq -r \ --arg prefix "${PREFIX}" \ '.results[] | select(.name | test("^\($prefix)-[0-9]")) | "\(.last_updated)|\(.name)"' \ | sort -r | cut -d'|' -f2) TAG_COUNT=$(echo "$TAGS" | grep -c . || true) if [ "$TAG_COUNT" -gt "$KEEP_COUNT" ]; then echo "Found $TAG_COUNT tags, keeping only the $KEEP_COUNT most recent" TAGS_TO_DELETE=$(echo "$TAGS" | tail -n +$((KEEP_COUNT + 1))) for tag in $TAGS_TO_DELETE; do echo "Deleting tag: $tag" curl -X DELETE -H "Authorization: JWT $TOKEN" \ "https://hub.docker.com/v2/repositories/${IMAGE_REPO}/tags/$tag/" done else echo "Only $TAG_COUNT tags found, no cleanup needed" fi done