|
| 1 | +name: PR Docker Cleanup |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [closed] |
| 6 | + branches: [ "main" ] |
| 7 | + |
| 8 | +jobs: |
| 9 | + cleanup: |
| 10 | + # Only run for PRs from the same repository (security measure) |
| 11 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 12 | + runs-on: ubuntu-latest |
| 13 | + permissions: |
| 14 | + contents: read |
| 15 | + packages: write |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Log in to GitHub Container Registry |
| 19 | + uses: docker/login-action@v3 |
| 20 | + with: |
| 21 | + registry: ghcr.io |
| 22 | + username: ${{ github.actor }} |
| 23 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + |
| 25 | + - name: Delete PR Docker image |
| 26 | + continue-on-error: true |
| 27 | + run: | |
| 28 | + # Convert repository name to lowercase for Docker registry |
| 29 | + REPO_LOWER=$(echo "${{ github.repository }}" | \ |
| 30 | + tr '[:upper:]' '[:lower:]') |
| 31 | + PACKAGE_NAME=$(basename ${REPO_LOWER}) |
| 32 | + TAG_NAME="pr-${{ github.event.number }}" |
| 33 | +
|
| 34 | + echo "Attempting to delete tag: ${TAG_NAME} for package: ${PACKAGE_NAME}" |
| 35 | +
|
| 36 | + # Determine the correct API base path based on repository owner type |
| 37 | + OWNER_TYPE="${{ github.repository_owner_type }}" |
| 38 | + OWNER="${{ github.repository_owner }}" |
| 39 | + if [ "$OWNER_TYPE" = "Organization" ]; then |
| 40 | + API_BASE="orgs/${OWNER}" |
| 41 | + else |
| 42 | + API_BASE="users/${OWNER}" |
| 43 | + fi |
| 44 | +
|
| 45 | + echo "Using API base path: ${API_BASE}" |
| 46 | + |
| 47 | + # Get all versions of the package with error handling |
| 48 | + API_URL="https://api.github.com/${API_BASE}/packages/container/${PACKAGE_NAME}/versions" |
| 49 | + RESPONSE=$(curl -sSf \ |
| 50 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 51 | + -H "Accept: application/vnd.github+json" \ |
| 52 | + "${API_URL}" 2>&1) |
| 53 | + CURL_EXIT_CODE=$? |
| 54 | + if [ $CURL_EXIT_CODE -ne 0 ]; then |
| 55 | + echo "Error: Failed to fetch package versions from GitHub API. Response:" |
| 56 | + echo "$RESPONSE" |
| 57 | + exit $CURL_EXIT_CODE |
| 58 | + fi |
| 59 | + VERSIONS=$(echo "$RESPONSE" | \ |
| 60 | + jq -r '.[] | select(.metadata.container.tags[]? == "'${TAG_NAME}'") | .id') |
| 61 | +
|
| 62 | + if [ -n "$VERSIONS" ]; then |
| 63 | + for VERSION_ID in $VERSIONS; do |
| 64 | + echo "Deleting version ID: $VERSION_ID with tag: ${TAG_NAME}" |
| 65 | + DELETE_URL="${API_URL}/${VERSION_ID}" |
| 66 | + DELETE_RESPONSE=$(curl -sSf -X DELETE \ |
| 67 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 68 | + -H "Accept: application/vnd.github+json" \ |
| 69 | + "${DELETE_URL}" 2>&1) |
| 70 | + DELETE_EXIT_CODE=$? |
| 71 | + if [ $DELETE_EXIT_CODE -eq 0 ]; then |
| 72 | + echo "Successfully deleted Docker image version: ${VERSION_ID}" |
| 73 | + else |
| 74 | + echo "Warning: Failed to delete version ID: $VERSION_ID. Response:" |
| 75 | + echo "$DELETE_RESPONSE" |
| 76 | + fi |
| 77 | + done |
| 78 | + else |
| 79 | + echo "No Docker image found for tag: ${TAG_NAME}, nothing to clean up" |
| 80 | + fi |
0 commit comments