|
1 | | -name: Test and Deploy Docusaurus |
| 1 | +name: Docs CI |
2 | 2 |
|
3 | 3 | on: |
4 | | - workflow_dispatch: |
5 | | - pull_request: |
6 | 4 | push: |
7 | | - branches: |
8 | | - - master |
| 5 | + branches: [master] |
| 6 | + pull_request: |
| 7 | + types: [opened, synchronize, reopened, closed] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pages: write |
| 12 | + id-token: write |
| 13 | + pull-requests: write |
9 | 14 |
|
10 | 15 | jobs: |
11 | 16 | build: |
12 | | - name: Build Docusaurus |
| 17 | + name: Build Site |
| 18 | + if: github.event.action != 'closed' |
13 | 19 | runs-on: ubuntu-latest |
| 20 | + |
14 | 21 | steps: |
15 | | - - uses: actions/checkout@v4 |
| 22 | + - name: Checkout |
| 23 | + uses: actions/checkout@v4 |
| 24 | + |
| 25 | + - name: Setup Node.js |
| 26 | + uses: actions/setup-node@v4 |
16 | 27 | with: |
17 | | - fetch-depth: 0 |
18 | | - - uses: actions/setup-node@v4 |
| 28 | + node-version: 22 |
| 29 | + |
| 30 | + - name: Cache npm dependencies |
| 31 | + uses: actions/cache@v4 |
19 | 32 | with: |
20 | | - node-version: 18 |
21 | | - cache: npm |
| 33 | + path: ~/.npm |
| 34 | + key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }} |
| 35 | + restore-keys: | |
| 36 | + ${{ runner.os }}-node- |
22 | 37 |
|
23 | 38 | - name: Install dependencies |
24 | | - run: npm ci |
25 | | - - name: Build website |
| 39 | + run: npm ci --prefer-offline |
| 40 | + |
| 41 | + - name: Lint Code (ESLint) |
| 42 | + run: npm run lint |
| 43 | + |
| 44 | + - name: Check Formatting (Prettier) |
| 45 | + run: npm run format |
| 46 | + |
| 47 | + - name: Inject correct baseUrl into docusaurus.config.js |
| 48 | + id: inject |
| 49 | + run: | |
| 50 | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 51 | + BASE_URL="/${{ github.event.repository.name }}/pr-${{ github.event.number }}/" |
| 52 | + else |
| 53 | + BASE_URL="/${{ github.event.repository.name }}/" |
| 54 | + fi |
| 55 | +
|
| 56 | + echo "Using baseUrl=$BASE_URL" |
| 57 | + echo "baseUrl=$BASE_URL" >> $GITHUB_OUTPUT |
| 58 | +
|
| 59 | + sed -i "s|baseUrl: '.*'|baseUrl: '$BASE_URL'|" docusaurus.config.js |
| 60 | +
|
| 61 | + - name: Build Docusaurus site |
26 | 62 | run: npm run build |
27 | 63 |
|
28 | | - - name: Upload Build Artifact |
29 | | - if: github.event_name != 'pull_request' |
30 | | - uses: actions/upload-pages-artifact@v3 |
| 64 | + - name: Upload site build as artifact |
| 65 | + uses: actions/upload-artifact@v4 |
31 | 66 | with: |
| 67 | + name: docusaurus-build |
32 | 68 | path: build |
33 | 69 |
|
34 | 70 | deploy: |
35 | | - name: Deploy to GitHub Pages |
| 71 | + name: Deploy Preview |
36 | 72 | needs: build |
37 | | - if: github.event_name != 'pull_request' |
| 73 | + if: github.event.action != 'closed' |
| 74 | + runs-on: ubuntu-latest |
38 | 75 |
|
39 | | - # Grant GITHUB_TOKEN the permissions required to make a Pages deployment |
40 | | - permissions: |
41 | | - pages: write # to deploy to Pages |
42 | | - id-token: write # to verify the deployment originates from an appropriate source |
| 76 | + steps: |
| 77 | + - name: Download build artifact |
| 78 | + uses: actions/download-artifact@v4 |
| 79 | + with: |
| 80 | + name: docusaurus-build |
| 81 | + path: build |
43 | 82 |
|
44 | | - # Deploy to the github-pages environment |
45 | | - environment: |
46 | | - name: github-pages |
47 | | - url: ${{ steps.deployment.outputs.page_url }} |
| 83 | + - name: Determine deploy target |
| 84 | + id: vars |
| 85 | + run: | |
| 86 | + if [[ "${{ github.event_name }}" == "pull_request" ]]; then |
| 87 | + echo "destination_dir=pr-${{ github.event.number }}" >> $GITHUB_OUTPUT |
| 88 | + echo "url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/pr-${{ github.event.number }}/" >> $GITHUB_OUTPUT |
| 89 | + else |
| 90 | + echo "destination_dir=." >> $GITHUB_OUTPUT |
| 91 | + echo "url=https://${{ github.repository_owner }}.github.io/${{ github.event.repository.name }}/" >> $GITHUB_OUTPUT |
| 92 | + fi |
48 | 93 |
|
| 94 | + - name: Deploy to GitHub Pages |
| 95 | + uses: peaceiris/actions-gh-pages@v4 |
| 96 | + with: |
| 97 | + github_token: ${{ secrets.GITHUB_TOKEN }} |
| 98 | + publish_dir: ./build |
| 99 | + publish_branch: gh-pages |
| 100 | + destination_dir: ${{ steps.vars.outputs.destination_dir }} |
| 101 | + keep_files: true |
| 102 | + |
| 103 | + - name: Comment preview link on PR |
| 104 | + if: github.event_name == 'pull_request' |
| 105 | + uses: marocchino/sticky-pull-request-comment@v2 |
| 106 | + with: |
| 107 | + header: docusaurus-preview |
| 108 | + message: | |
| 109 | + 🚀 **Preview Deployed** |
| 110 | + 🔗 [View Preview](${{ steps.vars.outputs.url }}) |
| 111 | +
|
| 112 | + cleanup: |
| 113 | + name: Cleanup Preview |
| 114 | + if: github.event_name == 'pull_request' && github.event.action == 'closed' |
49 | 115 | runs-on: ubuntu-latest |
| 116 | + |
50 | 117 | steps: |
51 | | - - name: Deploy to GitHub Pages |
52 | | - id: deployment |
53 | | - uses: actions/deploy-pages@v4 |
| 118 | + - name: Checkout gh-pages |
| 119 | + uses: actions/checkout@v4 |
| 120 | + with: |
| 121 | + ref: gh-pages |
| 122 | + |
| 123 | + - name: Remove PR preview directory |
| 124 | + run: | |
| 125 | + git config user.name "github-actions[bot]" |
| 126 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 127 | +
|
| 128 | + if [ -d "pr-${{ github.event.pull_request.number }}" ]; then |
| 129 | + git rm -rf pr-${{ github.event.pull_request.number }} |
| 130 | + git commit -m "🧹 Remove preview for PR #${{ github.event.pull_request.number }}" |
| 131 | + git push |
| 132 | + else |
| 133 | + echo "Directory pr-${{ github.event.pull_request.number }} not found, skipping cleanup" |
| 134 | + fi |
0 commit comments