2025-01-02 18:29:22 +08:00
|
|
|
|
name: PR Preview
|
|
|
|
|
|
|
|
|
|
on:
|
2025-02-10 05:20:51 +08:00
|
|
|
|
pull_request_target:
|
2025-01-02 18:29:22 +08:00
|
|
|
|
types: [opened, synchronize, reopened]
|
2025-02-10 05:29:03 +08:00
|
|
|
|
branches: [main, develop]
|
|
|
|
|
workflow_dispatch:
|
|
|
|
|
inputs:
|
|
|
|
|
pr_number:
|
|
|
|
|
description: 'Pull Request number to deploy preview for'
|
|
|
|
|
required: true
|
2025-01-02 18:29:22 +08:00
|
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
|
build:
|
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
|
|
|
|
|
|
permissions:
|
2025-01-02 19:01:06 +08:00
|
|
|
|
contents: write
|
2025-01-02 18:29:22 +08:00
|
|
|
|
deployments: write
|
2025-01-02 19:01:06 +08:00
|
|
|
|
pull-requests: write
|
2025-01-02 18:29:22 +08:00
|
|
|
|
|
|
|
|
|
steps:
|
2025-02-10 05:29:03 +08:00
|
|
|
|
# First, check out the workflow file (from the base) so secrets are available.
|
|
|
|
|
- name: Checkout base branch
|
|
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
|
|
|
|
|
# Determine PR info based on the event type.
|
|
|
|
|
- name: Set PR info
|
|
|
|
|
id: pr-info
|
|
|
|
|
run: |
|
|
|
|
|
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
|
|
|
|
|
echo "PR_NUMBER=${{ github.event.inputs.pr_number }}" >> $GITHUB_ENV
|
|
|
|
|
else
|
|
|
|
|
echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Now check out the PR’s head branch (whether from a PR event or supplied manually)
|
2025-02-10 05:20:51 +08:00
|
|
|
|
- name: Checkout PR branch
|
|
|
|
|
uses: actions/checkout@v4
|
|
|
|
|
with:
|
2025-02-10 06:00:21 +08:00
|
|
|
|
ref: "refs/pull/${{ env.PR_NUMBER }}/merge"
|
2025-02-10 05:20:51 +08:00
|
|
|
|
|
2025-02-10 05:29:03 +08:00
|
|
|
|
- name: Setup Node.js
|
|
|
|
|
uses: actions/setup-node@v4
|
2025-01-02 18:29:22 +08:00
|
|
|
|
with:
|
|
|
|
|
node-version: 20
|
|
|
|
|
cache: 'yarn'
|
|
|
|
|
|
|
|
|
|
- name: Build
|
|
|
|
|
run: |
|
|
|
|
|
yarn install --production --frozen-lockfile
|
|
|
|
|
yarn build-preview
|
2025-01-02 18:57:45 +08:00
|
|
|
|
rm -rf dist/web.tgz
|
2025-01-02 18:29:22 +08:00
|
|
|
|
|
|
|
|
|
- name: Deploy to Cloudflare Pages
|
|
|
|
|
uses: cloudflare/wrangler-action@v3
|
|
|
|
|
with:
|
|
|
|
|
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
|
2025-01-02 18:50:45 +08:00
|
|
|
|
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
|
2025-01-02 18:29:22 +08:00
|
|
|
|
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
|
2025-02-10 05:29:03 +08:00
|
|
|
|
command: pages deploy dist --project-name=data-preview --branch=pr-${{ env.PR_NUMBER }}
|
2025-02-10 05:20:51 +08:00
|
|
|
|
|
2025-01-02 18:29:22 +08:00
|
|
|
|
- name: Pull request comment
|
|
|
|
|
uses: actions/github-script@v6
|
|
|
|
|
with:
|
|
|
|
|
script: |
|
2025-02-10 05:29:03 +08:00
|
|
|
|
const prNumber = "${{ env.PR_NUMBER }}";
|
2025-01-02 19:08:29 +08:00
|
|
|
|
const now = new Date().toISOString().substring(0, 19).replace('T', ' ');
|
|
|
|
|
const reviewBody = `🐱 感谢贡献!\n\n部署了预览,在这里哦: https://pr-${prNumber}.data-preview.pages.dev\n\n🕒 最后更新: ${now} (UTC)`;
|
2025-01-02 18:29:22 +08:00
|
|
|
|
|
2025-02-10 05:29:03 +08:00
|
|
|
|
// List existing reviews on the PR.
|
2025-01-02 18:29:22 +08:00
|
|
|
|
const { data: reviews } = await github.rest.pulls.listReviews({
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
|
|
|
|
pull_number: prNumber,
|
|
|
|
|
});
|
2025-02-10 05:29:03 +08:00
|
|
|
|
const existingReview = reviews.find(review => review.body && review.body.includes('部署了预览'));
|
2025-01-02 18:29:22 +08:00
|
|
|
|
if (existingReview) {
|
2025-02-10 05:29:03 +08:00
|
|
|
|
// Update the existing review.
|
2025-01-02 18:29:22 +08:00
|
|
|
|
await github.rest.pulls.updateReview({
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
|
|
|
|
pull_number: prNumber,
|
|
|
|
|
review_id: existingReview.id,
|
|
|
|
|
body: reviewBody,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2025-02-10 05:20:51 +08:00
|
|
|
|
// Create a new review comment.
|
2025-01-02 18:29:22 +08:00
|
|
|
|
await github.rest.pulls.createReview({
|
|
|
|
|
owner: context.repo.owner,
|
|
|
|
|
repo: context.repo.repo,
|
|
|
|
|
pull_number: prNumber,
|
|
|
|
|
body: reviewBody,
|
|
|
|
|
event: "COMMENT",
|
|
|
|
|
});
|
|
|
|
|
}
|