|
| 1 | +name: Preview Documentation |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: |
| 6 | + - created |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + issues: write |
| 11 | + pull-requests: read |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: preview-pr-${{ github.event.issue.number }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + preview: |
| 19 | + name: Build and serve preview |
| 20 | + if: >- |
| 21 | + github.event.issue.pull_request && |
| 22 | + ( |
| 23 | + github.event.comment.body == 'preview' || |
| 24 | + github.event.comment.body == '/preview' |
| 25 | + ) && |
| 26 | + contains(fromJSON('["OWNER", "MEMBER", "COLLABORATOR"]'), github.event.comment.author_association) |
| 27 | + runs-on: ubuntu-latest |
| 28 | + timeout-minutes: 75 |
| 29 | + |
| 30 | + steps: |
| 31 | + - name: Get pull request |
| 32 | + id: pr |
| 33 | + uses: actions/github-script@v7 |
| 34 | + with: |
| 35 | + script: | |
| 36 | + const { owner, repo } = context.repo; |
| 37 | + const pull_number = context.payload.issue.number; |
| 38 | + const { data: pull } = await github.rest.pulls.get({ |
| 39 | + owner, |
| 40 | + repo, |
| 41 | + pull_number, |
| 42 | + }); |
| 43 | +
|
| 44 | + core.setOutput('head_repo', pull.head.repo.full_name); |
| 45 | + core.setOutput('head_ref', pull.head.ref); |
| 46 | + core.setOutput('head_sha', pull.head.sha); |
| 47 | +
|
| 48 | + - name: React to preview request |
| 49 | + uses: actions/github-script@v7 |
| 50 | + with: |
| 51 | + script: | |
| 52 | + await github.rest.reactions.createForIssueComment({ |
| 53 | + owner: context.repo.owner, |
| 54 | + repo: context.repo.repo, |
| 55 | + comment_id: context.payload.comment.id, |
| 56 | + content: 'rocket', |
| 57 | + }); |
| 58 | +
|
| 59 | + - name: Checkout pull request |
| 60 | + uses: actions/checkout@v4 |
| 61 | + with: |
| 62 | + repository: ${{ steps.pr.outputs.head_repo }} |
| 63 | + ref: ${{ steps.pr.outputs.head_sha }} |
| 64 | + persist-credentials: false |
| 65 | + |
| 66 | + - name: Setup Node.js |
| 67 | + uses: actions/setup-node@v4 |
| 68 | + with: |
| 69 | + node-version: "20" |
| 70 | + cache: "yarn" |
| 71 | + |
| 72 | + - name: Install dependencies |
| 73 | + run: yarn install --frozen-lockfile |
| 74 | + |
| 75 | + - name: Build documentation |
| 76 | + run: yarn build |
| 77 | + |
| 78 | + - name: Install cloudflared |
| 79 | + run: | |
| 80 | + curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb |
| 81 | + sudo dpkg -i cloudflared.deb |
| 82 | +
|
| 83 | + - name: Start Docusaurus server |
| 84 | + run: | |
| 85 | + nohup yarn serve --host 0.0.0.0 --port 3000 > docusaurus-preview.log 2>&1 & |
| 86 | +
|
| 87 | + - name: Start preview tunnel |
| 88 | + id: tunnel |
| 89 | + run: | |
| 90 | + nohup cloudflared tunnel --url http://localhost:3000 --no-autoupdate > cloudflared-preview.log 2>&1 & |
| 91 | +
|
| 92 | + for attempt in {1..30}; do |
| 93 | + preview_url="$(grep -o 'https://[^ ]*trycloudflare.com' cloudflared-preview.log | head -n 1 || true)" |
| 94 | + if [ -n "$preview_url" ]; then |
| 95 | + echo "url=$preview_url" >> "$GITHUB_OUTPUT" |
| 96 | + exit 0 |
| 97 | + fi |
| 98 | +
|
| 99 | + sleep 2 |
| 100 | + done |
| 101 | +
|
| 102 | + echo "Failed to start preview tunnel." |
| 103 | + cat cloudflared-preview.log |
| 104 | + exit 1 |
| 105 | +
|
| 106 | + - name: Comment preview URL |
| 107 | + uses: actions/github-script@v7 |
| 108 | + env: |
| 109 | + PREVIEW_URL: ${{ steps.tunnel.outputs.url }} |
| 110 | + with: |
| 111 | + script: | |
| 112 | + await github.rest.issues.createComment({ |
| 113 | + owner: context.repo.owner, |
| 114 | + repo: context.repo.repo, |
| 115 | + issue_number: context.payload.issue.number, |
| 116 | + body: [ |
| 117 | + `Preview: ${process.env.PREVIEW_URL}`, |
| 118 | + '', |
| 119 | + 'This preview will stay live for 1 hour. Comment `preview` again to replace it with a fresh build.', |
| 120 | + ].join('\n'), |
| 121 | + }); |
| 122 | +
|
| 123 | + - name: Keep preview live for one hour |
| 124 | + run: sleep 3600 |
0 commit comments