CI: consolidate docs deployment workflows and add PR previews #15
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish Docs | |
| on: | |
| push: | |
| branches: [master] | |
| pull_request: | |
| types: | |
| - opened | |
| - reopened | |
| - synchronize | |
| - closed | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| concurrency: preview-${{ github.ref }} | |
| env: | |
| PREVIEW_BRANCH: gh-pages | |
| jobs: | |
| deploy-preview: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'pull_request' }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Calculate URLs | |
| run: | | |
| REPO="${{ github.event.repository.name }}" | |
| OWNER="${{ github.repository_owner }}" | |
| if [[ "$REPO" == "$OWNER.github.io" ]]; then | |
| # User/org GitHub Pages repo | |
| echo "BASE_URL=/pr-preview/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_ENV | |
| echo "URL=https://$OWNER.github.io" >> $GITHUB_ENV | |
| else | |
| # Normal project repo (fork or upstream) | |
| echo "BASE_URL=/$REPO/pr-preview/pr-${{ github.event.pull_request.number }}/" >> $GITHUB_ENV | |
| echo "URL=https://$OWNER.github.io/$REPO" >> $GITHUB_ENV | |
| fi | |
| - name: Install and Build | |
| run: npm ci && npm run build | |
| env: | |
| BASE_URL: ${{ env.BASE_URL }} | |
| URL: ${{ env.URL }} | |
| - name: Deploy Preview | |
| uses: rossjrw/pr-preview-action@v1 | |
| id: preview-step | |
| with: | |
| source-dir: build/ | |
| preview-branch: ${{ env.PREVIEW_BRANCH }} | |
| comment: false | |
| - name: Comment Preview URL (Success) | |
| uses: marocchino/sticky-pull-request-comment@v2 | |
| if: steps.preview-step.outputs.deployment-action == 'deploy' && env.deployment_status == 'success' | |
| with: | |
| header: pr-preview | |
| message: | | |
| [PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }} | |
| :---: | |
| | <p></p> :rocket: View preview at <br> ${{ steps.preview-step.outputs.preview-url }} <br><br> | |
| | <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> | |
| - name: Comment Preview Removed | |
| uses: marocchino/sticky-pull-request-comment@v2 | |
| if: steps.preview-step.outputs.deployment-action == 'remove' && env.deployment_status == 'success' | |
| with: | |
| header: pr-preview | |
| message: | | |
| [PR Preview Action](https://github.com/rossjrw/pr-preview-action) ${{ steps.preview-step.outputs.action-version }} | |
| :---: | |
| Preview removed because the pull request was closed. | |
| ${{ steps.preview-step.outputs.action-start-time }} | |
| build-production: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: 20 | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build | |
| run: npm run build | |
| - name: Upload build artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: site | |
| path: build/ | |
| deploy-production: | |
| runs-on: ubuntu-latest | |
| needs: build-production | |
| if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }} | |
| steps: | |
| - name: Download build artifact | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: site | |
| path: site | |
| - name: Deploy to GitHub Pages | |
| uses: JamesIves/github-pages-deploy-action@v4 | |
| with: | |
| branch: gh-pages | |
| folder: site | |
| clean-exclude: pr-preview |