|
1 | | -name: PR Preview |
| 1 | +name: PR Deploy Preview |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request_target: |
5 | | - types: [opened, synchronize, reopened] |
6 | | - branches: [main, develop] |
7 | | - workflow_dispatch: |
8 | | - inputs: |
9 | | - pr_number: |
10 | | - description: 'Pull Request number to deploy preview for' |
11 | | - required: true |
| 4 | + workflow_run: |
| 5 | + workflows: ["PR Build"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: ${{ github.workflow }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +permissions: |
| 14 | + pull-requests: write |
| 15 | + actions: read |
12 | 16 |
|
13 | 17 | jobs: |
14 | | - build: |
| 18 | + deploy: |
15 | 19 | runs-on: ubuntu-latest |
16 | 20 |
|
17 | | - permissions: |
18 | | - contents: write |
19 | | - deployments: write |
20 | | - pull-requests: write |
| 21 | + if: > |
| 22 | + github.event.workflow_run.event == 'pull_request' && |
| 23 | + github.event.workflow_run.conclusion == 'success' |
21 | 24 |
|
22 | 25 | steps: |
23 | | - # First, check out the workflow file (from the base) so secrets are available. |
24 | | - - name: Checkout base branch |
25 | | - uses: actions/checkout@v4 |
| 26 | + - name: Download Artifact |
| 27 | + uses: actions/download-artifact@v8 |
| 28 | + with: |
| 29 | + name: preview-dist |
| 30 | + path: dist/ |
| 31 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 32 | + run-id: ${{ github.event.workflow_run.id }} |
26 | 33 |
|
27 | | - # Determine PR info based on the event type. |
28 | | - - name: Set PR info |
| 34 | + - name: Read PR Number |
29 | 35 | id: pr-info |
30 | 36 | run: | |
31 | | - if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
32 | | - echo "PR_NUMBER=${{ github.event.inputs.pr_number }}" >> $GITHUB_ENV |
33 | | - else |
34 | | - echo "PR_NUMBER=${{ github.event.pull_request.number }}" >> $GITHUB_ENV |
| 37 | + PR_NUMBER=$(cat dist/pr_number.txt | tr -d '\n\r') |
| 38 | + if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ ]]; then |
| 39 | + echo "Error: Invalid PR number format: $PR_NUMBER" |
| 40 | + exit 1 |
35 | 41 | fi |
36 | | -
|
37 | | - # Now check out the PR’s head branch (whether from a PR event or supplied manually) |
38 | | - - name: Checkout PR branch |
39 | | - uses: actions/checkout@v4 |
40 | | - with: |
41 | | - ref: "refs/pull/${{ env.PR_NUMBER }}/merge" |
42 | | - |
43 | | - - name: Setup Node.js |
44 | | - uses: actions/setup-node@v4 |
45 | | - with: |
46 | | - node-version: 20 |
47 | | - cache: 'yarn' |
48 | | - |
49 | | - - name: Build |
50 | | - run: | |
51 | | - yarn install --production --frozen-lockfile |
52 | | - yarn build-preview |
53 | | - rm -rf dist/web.tgz |
| 42 | + echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV |
| 43 | + rm dist/pr_number.txt |
54 | 44 |
|
55 | 45 | - name: Deploy to Cloudflare Pages |
56 | 46 | uses: cloudflare/wrangler-action@v3 |
57 | 47 | with: |
58 | 48 | apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} |
59 | 49 | accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} |
60 | | - gitHubToken: ${{ secrets.GITHUB_TOKEN }} |
61 | 50 | command: pages deploy dist --project-name=data-preview --branch=pr-${{ env.PR_NUMBER }} |
62 | 51 |
|
63 | 52 | - name: Pull request comment |
64 | | - uses: actions/github-script@v6 |
| 53 | + uses: actions/github-script@v9 |
| 54 | + env: |
| 55 | + PR_NUMBER: ${{ env.PR_NUMBER }} |
65 | 56 | with: |
66 | 57 | script: | |
67 | | - const prNumber = "${{ env.PR_NUMBER }}"; |
| 58 | + const prNumber = parseInt(process.env.PR_NUMBER, 10); |
68 | 59 | const now = new Date().toISOString().substring(0, 19).replace('T', ' '); |
69 | 60 | const reviewBody = `🐱 感谢贡献!\n\n部署了预览,在这里哦: https://pr-${prNumber}.data-preview.pages.dev\n\n🕒 最后更新: ${now} (UTC)`; |
70 | 61 | |
|
0 commit comments