diff --git a/.github/workflows/pr-deploy.yaml b/.github/workflows/pr-deploy.yaml new file mode 100644 index 00000000..23acf71a --- /dev/null +++ b/.github/workflows/pr-deploy.yaml @@ -0,0 +1,80 @@ +name: Pull Request Deploy + +on: + pull_request: + types: [opened, synchronize, reopened] +permissions: + contents: read + pull-requests: write +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout repo + uses: actions/checkout@v6 + - name: Deploy + id: deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + command: | + pages deploy . --project-name=win12-online --branch=pr-preview/${{ github.event.pull_request.number }} --commit-hash=${{ github.event.pull_request.head.sha }} + - name: Post comment + if: ${{ steps.deploy.outputs.deployment-url != '' }} + uses: actions/github-script@v9 + with: + script: | + // Check if there is already a comment + let commentId = null; + await github.paginate( + 'GET /repos/{owner}/{repo}/issues/{issue_number}/comments', + { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: ${{ github.event.pull_request.number }}, + }, + (response) => { + for (const comment of response) { + if ( + comment.user.login === 'github-actions[bot]' && + comment.body.includes('')) { + commentId = comment.id; + return true; + } + } + return false; + } + ); + + console.log('commentId: ', commentId); + const comment = ` + + ## ⚡ 已自动部署到 Cloudflare Pages + + 预览地址: <${{ steps.deploy.outputs.deployment-url }}> + 提交hash: ${{ github.event.pull_request.head.sha }} + + PR 更新时会自动进行部署并更新此评论 + + ###### 本评论由 GitHub Actions 工作流自动生成~ + `; + console.log('comment: ', comment); + + if (!commentId) { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: ${{ github.event.pull_request.number }}, + body: comment, + }); + } + else { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: commentId, + body: comment, + }); + } +