Simplify PR comments to show only beta release link #17
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Beta Release on PR | |
| on: | |
| pull_request: | |
| types: [opened, synchronize, reopened] | |
| # Remove branches restriction to allow beta releases for PRs targeting any branch | |
| workflow_dispatch: # Allow manual triggering | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| build-and-deploy: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| packages: write | |
| pull-requests: write | |
| issues: write | |
| deployments: write # needed to create deployments | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
| tags: | | |
| type=ref,event=branch | |
| type=ref,event=pr | |
| type=sha,prefix=sha- | |
| type=raw,value=beta-{{date 'YYYYMMDD-HHmmss'}} | |
| labels: | | |
| org.opencontainers.image.title=BioAnalyzer Beta | |
| org.opencontainers.image.description=BioAnalyzer Beta Release for PR Testing | |
| org.opencontainers.image.version=1.0.0-beta | |
| org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }} | |
| org.opencontainers.image.revision=${{ github.sha }} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ./Dockerfile | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| - name: Generate deployment URL | |
| id: url | |
| run: | | |
| PR_NUMBER=${{ github.event.number }} | |
| BETA_URL="https://bioanalyzer-beta-$PR_NUMBER.vercel.app" | |
| echo "beta_url=$BETA_URL" >> $GITHUB_OUTPUT | |
| echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT | |
| - name: Create deployment comment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| try { | |
| const prNumber = ${{ github.event.number }}; | |
| const betaUrl = '${{ steps.url.outputs.beta_url }}'; | |
| const tagsJson = '${{ steps.meta.outputs.json }}'; | |
| const tagsData = JSON.parse(tagsJson); | |
| const imageTag = tagsData.tags[0]; | |
| const commitSha = '${{ github.sha }}'; | |
| const shortSha = commitSha.substring(0, 7); | |
| const comment = `## 🚀 Beta Release Ready for Testing! | |
| ** [${betaUrl}](${betaUrl}) | |
| --- | |
| *This beta release will be automatically updated when you push new commits to this PR.*`; | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const existingComment = comments.data.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('🚀 Beta Release Ready for Testing!') | |
| ); | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body: comment | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: comment | |
| }); | |
| } | |
| } catch (error) { | |
| console.error('Error creating deployment comment:', error); | |
| throw error; | |
| } | |
| - name: Create deployment | |
| id: deployment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const deployment = await github.rest.repos.createDeployment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| ref: context.sha, | |
| environment: 'beta', | |
| description: 'Beta release deployment', | |
| auto_merge: false, | |
| required_contexts: [] // allow deployment without checks | |
| }); | |
| core.setOutput("id", deployment.data.id); | |
| - name: Create deployment status | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const deploymentId = ${{ steps.deployment.outputs.id }}; | |
| await github.rest.repos.createDeploymentStatus({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| deployment_id: deploymentId, | |
| state: 'success', | |
| environment: 'beta', | |
| description: 'Beta release deployed successfully', | |
| environment_url: '${{ steps.url.outputs.beta_url }}' | |
| }); | |
| cleanup: | |
| runs-on: ubuntu-latest | |
| if: github.event.action == 'closed' | |
| permissions: | |
| packages: write | |
| pull-requests: write | |
| deployments: write | |
| steps: | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Delete beta images | |
| run: | | |
| PR_NUMBER=${{ github.event.number }} | |
| echo "Cleaning up beta images for PR #$PR_NUMBER" | |
| docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \ | |
| -e REGISTRY=${{ env.REGISTRY }} \ | |
| -e IMAGE_NAME=${{ env.IMAGE_NAME }} \ | |
| -e PR_NUMBER=$PR_NUMBER \ | |
| alpine:latest sh -c ' | |
| apk add --no-cache curl jq | |
| echo "Beta images cleanup completed for PR #$PR_NUMBER" | |
| ' | |
| - name: Update PR comment | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const prNumber = ${{ github.event.number }}; | |
| const comment = "## 🧹 Beta Release Cleaned Up\\n\\n" + | |
| "The beta release for this PR has been cleaned up.\\n\\n" + | |
| "---\\n\\n" + | |
| "*Thank you for testing!*"; | |
| const comments = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| }); | |
| const existingComment = comments.data.find(comment => | |
| comment.user.type === 'Bot' && | |
| comment.body.includes('Beta Release Cleaned Up') | |
| ); | |
| if (existingComment) { | |
| await github.rest.issues.updateComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| comment_id: existingComment.id, | |
| body: comment | |
| }); | |
| } else { | |
| await github.rest.issues.createComment({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| body: comment | |
| }); | |
| } |