|
| 1 | +name: PR Docker Image Workflow |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [opened, synchronize, reopened, closed] |
| 6 | + |
| 7 | +jobs: |
| 8 | + # Build and push Docker image, then comment on PR |
| 9 | + build-and-comment: |
| 10 | + if: github.event.action != 'closed' |
| 11 | + runs-on: ubuntu-latest |
| 12 | + permissions: |
| 13 | + contents: read |
| 14 | + packages: write |
| 15 | + pull-requests: write |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Lowercase repository name |
| 22 | + id: repo-name |
| 23 | + run: | |
| 24 | + echo "REPO_NAME=$(echo '${{ github.repository }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT |
| 25 | + |
| 26 | + - name: Set up QEMU |
| 27 | + uses: docker/setup-qemu-action@v3 |
| 28 | + |
| 29 | + - name: Set up Docker Buildx |
| 30 | + uses: docker/setup-buildx-action@v3 |
| 31 | + |
| 32 | + - name: Login to GitHub Container Registry |
| 33 | + uses: docker/login-action@v3 |
| 34 | + with: |
| 35 | + registry: ghcr.io |
| 36 | + username: ${{ github.repository_owner }} |
| 37 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + |
| 39 | + - name: Build and push |
| 40 | + uses: docker/build-push-action@v5 |
| 41 | + with: |
| 42 | + context: . |
| 43 | + push: true |
| 44 | + platforms: linux/amd64,linux/arm64 |
| 45 | + tags: ghcr.io/${{ steps.repo-name.outputs.REPO_NAME }}/pr-${{ github.event.pull_request.number }}:latest |
| 46 | + |
| 47 | + - name: Comment on PR |
| 48 | + uses: actions/github-script@v7 |
| 49 | + with: |
| 50 | + script: | |
| 51 | + const imageName = `ghcr.io/${{ steps.repo-name.outputs.REPO_NAME }}/pr-${context.issue.number}:latest`; |
| 52 | + const commentBody = `📦 Docker image for this PR is available at: \`${imageName}\` |
| 53 | + |
| 54 | + You can pull it with: |
| 55 | + \`\`\` |
| 56 | + docker pull ${imageName} |
| 57 | + docker run ${imageName} |
| 58 | + \`\`\``; |
| 59 | +
|
| 60 | + // Get all comments on the PR |
| 61 | + const comments = await github.rest.issues.listComments({ |
| 62 | + owner: context.repo.owner, |
| 63 | + repo: context.repo.repo, |
| 64 | + issue_number: context.issue.number |
| 65 | + }); |
| 66 | +
|
| 67 | + // Look for an existing Docker image comment |
| 68 | + const dockerComment = comments.data.find(comment => |
| 69 | + comment.body.includes('📦 Docker image for this PR is available at:') |
| 70 | + ); |
| 71 | +
|
| 72 | + if (dockerComment) { |
| 73 | + // Update existing comment |
| 74 | + await github.rest.issues.updateComment({ |
| 75 | + owner: context.repo.owner, |
| 76 | + repo: context.repo.repo, |
| 77 | + comment_id: dockerComment.id, |
| 78 | + body: commentBody |
| 79 | + }); |
| 80 | + } else { |
| 81 | + // Create new comment |
| 82 | + await github.rest.issues.createComment({ |
| 83 | + owner: context.repo.owner, |
| 84 | + repo: context.repo.repo, |
| 85 | + issue_number: context.issue.number, |
| 86 | + body: commentBody |
| 87 | + }); |
| 88 | + } |
| 89 | +
|
| 90 | + # Delete Docker image when PR is closed/merged |
| 91 | + delete-image: |
| 92 | + if: github.event.action == 'closed' |
| 93 | + runs-on: ubuntu-latest |
| 94 | + permissions: |
| 95 | + packages: write |
| 96 | + |
| 97 | + steps: |
| 98 | + - name: Lowercase repository name |
| 99 | + id: repo-name |
| 100 | + run: | |
| 101 | + echo "OWNER=$(echo '${{ github.repository_owner }}' | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT |
| 102 | + echo "REPO=$(echo '${{ github.repository }}' | cut -d '/' -f 2 | tr '[:upper:]' '[:lower:]')" >> $GITHUB_OUTPUT |
| 103 | + |
| 104 | + - name: Delete Docker image |
| 105 | + uses: actions/github-script@v7 |
| 106 | + with: |
| 107 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 108 | + script: | |
| 109 | + const owner = '${{ steps.repo-name.outputs.OWNER }}'; |
| 110 | + const repo = '${{ steps.repo-name.outputs.REPO }}'; |
| 111 | + const package_name = `${repo}/pr-${context.issue.number}`; |
| 112 | + |
| 113 | + console.log(`Attempting to delete package: ${package_name}`); |
| 114 | + |
| 115 | + try { |
| 116 | + // For organization repos |
| 117 | + await github.rest.packages.deletePackageForOrg({ |
| 118 | + package_type: 'container', |
| 119 | + package_name: package_name, |
| 120 | + org: owner |
| 121 | + }); |
| 122 | + console.log('Package deleted successfully via org API'); |
| 123 | + } catch (orgError) { |
| 124 | + console.log(`Error deleting via org API: ${orgError.message}`); |
| 125 | + |
| 126 | + try { |
| 127 | + // For user repos |
| 128 | + await github.rest.packages.deletePackageForUser({ |
| 129 | + package_type: 'container', |
| 130 | + package_name: package_name, |
| 131 | + username: owner |
| 132 | + }); |
| 133 | + console.log('Package deleted successfully via user API'); |
| 134 | + } catch (userError) { |
| 135 | + console.log(`Error deleting via user API: ${userError.message}`); |
| 136 | + } |
| 137 | + } |
0 commit comments