|
4 | 4 | push: |
5 | 5 | branches: [master] |
6 | 6 | pull_request: |
7 | | - types: |
8 | | - - opened |
9 | | - - reopened |
10 | | - - synchronize |
11 | | - - closed |
| 7 | + types: [opened, reopened, synchronize, closed] |
| 8 | + workflow_dispatch: |
| 9 | + inputs: |
| 10 | + pr_number: |
| 11 | + description: 'PR Number' |
| 12 | + required: true |
| 13 | + run_id: |
| 14 | + description: 'Run ID of the build artifact' |
| 15 | + required: true |
12 | 16 |
|
13 | 17 | permissions: |
14 | 18 | contents: write |
| 19 | + pull-requests: write |
| 20 | + actions: read |
15 | 21 |
|
16 | | -concurrency: preview-${{ github.ref }} |
17 | | - |
18 | | -env: |
19 | | - PREVIEW_BRANCH: gh-pages |
| 22 | +concurrency: ci-${{ github.ref }} |
20 | 23 |
|
21 | 24 | jobs: |
22 | | - deploy-preview: |
23 | | - runs-on: ubuntu-latest |
| 25 | + # 1. Build Preview Artifact (Runs on PR) |
| 26 | + build-preview: |
24 | 27 | if: github.event_name == 'pull_request' |
| 28 | + runs-on: ubuntu-latest |
25 | 29 | steps: |
26 | | - - uses: actions/checkout@v4 |
27 | | - - uses: actions/setup-node@v4 |
| 30 | + - name: Checkout |
| 31 | + uses: actions/checkout@v4 |
| 32 | + |
| 33 | + - name: Setup Node.js |
| 34 | + uses: actions/setup-node@v4 |
28 | 35 | with: |
29 | 36 | node-version: 20 |
30 | 37 |
|
31 | 38 | - name: Calculate BASE_URL |
32 | | - run: echo "BASE_URL=/pr-preview/pr-${{ github.event.pull_request.number }}" >> $GITHUB_ENV |
| 39 | + run: | |
| 40 | + echo "BASE_URL=/pr-preview/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_ENV |
33 | 41 |
|
34 | | - - run: npm ci && npm run build |
| 42 | + - name: Install and Build |
| 43 | + run: npm ci && npm run build |
35 | 44 | env: |
36 | 45 | BASE_URL: ${{ env.BASE_URL }} |
37 | 46 |
|
38 | | - - uses: rossjrw/pr-preview-action@v1 |
39 | | - id: preview-step |
| 47 | + - name: Upload Preview Artifact |
| 48 | + uses: actions/upload-artifact@v4 |
40 | 49 | with: |
41 | | - source-dir: ./build/ |
42 | | - preview-branch: ${{ env.PREVIEW_BRANCH }} |
43 | | - comment: false |
| 50 | + name: preview-build |
| 51 | + path: build/ |
44 | 52 |
|
45 | | - - uses: marocchino/sticky-pull-request-comment@v2 |
46 | | - if: steps.preview-step.outputs.deployment-action == 'deploy' && env.deployment_status == 'success' |
| 53 | + - name: Comment Build Info |
| 54 | + uses: marocchino/sticky-pull-request-comment@v2 |
47 | 55 | with: |
48 | | - header: pr-preview |
| 56 | + header: pr-build-info |
49 | 57 | message: | |
50 | | - [PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }} |
51 | | - :---: |
52 | | - | <p></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }} <br><br> |
53 | | - | <h6>Built to branch [`${{ env.PREVIEW_BRANCH }}`](${{ github.server_url }}/${{ github.repository }}/tree/${{ env.PREVIEW_BRANCH }}) at ${{ steps.preview-step.outputs.action-start-time }}. <br> Preview will be ready when the [GitHub Pages deployment](${{ github.server_url }}/${{ github.repository }}/deployments) is complete. <br><br> </h6> |
| 58 | + ### ✅ Build Successful |
| 59 | + To deploy this preview, a maintainer must run the **Deploy Preview** workflow. |
| 60 | + |
| 61 | + **Run ID**: `${{ github.run_id }}` |
| 62 | + **PR Number**: `${{ github.event.pull_request.number }}` |
| 63 | +
|
| 64 | + # 2. Deploy Preview (Manual Trigger) |
| 65 | + deploy-preview: |
| 66 | + if: github.event_name == 'workflow_dispatch' |
| 67 | + runs-on: ubuntu-latest |
| 68 | + steps: |
| 69 | + - name: Checkout |
| 70 | + uses: actions/checkout@v4 |
54 | 71 |
|
55 | | - - uses: marocchino/sticky-pull-request-comment@v2 |
56 | | - if: steps.preview-step.outputs.deployment-action == 'remove' && env.deployment_status == 'success' |
| 72 | + - name: Download Artifact |
| 73 | + uses: actions/download-artifact@v4 |
57 | 74 | with: |
58 | | - header: pr-preview |
59 | | - message: | |
60 | | - [PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }} |
61 | | - :---: |
62 | | - Preview removed because the pull request was closed. |
63 | | - ${{ steps.preview-step.outputs.action-start-time }} |
| 75 | + name: preview-build |
| 76 | + run-id: ${{ inputs.run_id }} |
| 77 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + path: site |
| 79 | + |
| 80 | + - name: Deploy to GitHub Pages |
| 81 | + uses: JamesIves/github-pages-deploy-action@v4 |
| 82 | + with: |
| 83 | + branch: gh-pages |
| 84 | + folder: site |
| 85 | + target-folder: pr-preview/pr-${{ inputs.pr_number }} |
64 | 86 |
|
| 87 | + - name: Comment Preview Link |
| 88 | + uses: actions/github-script@v6 |
| 89 | + with: |
| 90 | + script: | |
| 91 | + const prNumber = ${{ inputs.pr_number }}; |
| 92 | + const owner = context.repo.owner; |
| 93 | + const repo = context.repo.repo; |
| 94 | + const url = `https://${owner}.github.io/${repo}/pr-preview/pr-${prNumber}/`; |
| 95 | + |
| 96 | + await github.rest.issues.createComment({ |
| 97 | + owner, |
| 98 | + repo, |
| 99 | + issue_number: prNumber, |
| 100 | + body: `🚀 **Preview Deployed!**\n\n[View Preview](${url})` |
| 101 | + }); |
| 102 | +
|
| 103 | + # 3. Production Build & Deploy (Runs on Master Push) |
65 | 104 | build-production: |
66 | | - runs-on: ubuntu-latest |
67 | 105 | if: github.event_name == 'push' && github.ref == 'refs/heads/master' |
| 106 | + runs-on: ubuntu-latest |
68 | 107 | steps: |
69 | 108 | - uses: actions/checkout@v4 |
70 | 109 | - uses: actions/setup-node@v4 |
|
75 | 114 | with: { name: site, path: build/ } |
76 | 115 |
|
77 | 116 | deploy-production: |
78 | | - runs-on: ubuntu-latest |
79 | 117 | needs: build-production |
80 | 118 | if: github.event_name == 'push' && github.ref == 'refs/heads/master' |
| 119 | + runs-on: ubuntu-latest |
81 | 120 | steps: |
82 | 121 | - uses: actions/download-artifact@v4 |
83 | 122 | with: { name: site, path: site } |
|
0 commit comments