diff --git a/.github/workflows/pelican-gh-pages.yml b/.github/workflows/pelican-gh-pages.yml index 98b244e..450415e 100644 --- a/.github/workflows/pelican-gh-pages.yml +++ b/.github/workflows/pelican-gh-pages.yml @@ -3,16 +3,16 @@ name: Deploy to GitHub Pages on: push: branches: [master] + pull_request: + branches: [master] workflow_dispatch: concurrency: - group: pages + group: pages-${{ github.head_ref || github.ref }} cancel-in-progress: true permissions: contents: read - pages: write - id-token: write env: PYTHON_VERSION: '3.11' @@ -20,12 +20,22 @@ env: jobs: build: runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write steps: - uses: actions/checkout@v5 - name: Set up Python uses: actions/setup-python@v6 with: python-version: ${{ env.PYTHON_VERSION }} + - name: Cache pip + uses: actions/cache@v4 + with: + path: ~/.cache/pip + key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} + restore-keys: | + ${{ runner.os }}-pip- - name: Install dependencies run: | python -m pip install --upgrade pip @@ -33,14 +43,42 @@ jobs: - name: Build site for production run: | make prod + - name: Remove custom domain for PR previews + if: github.event_name == 'pull_request' + run: rm -f output/CNAME + - name: Upload preview artifact + if: github.event_name == 'pull_request' + uses: actions/upload-artifact@v4 + with: + name: site-preview + path: output + - name: Comment preview link + if: github.event_name == 'pull_request' + uses: actions/github-script@v7 + env: + RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} + with: + script: | + const body = `Preview build ready: [download site-preview artifact](${process.env.RUN_URL})`; + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.issue.number, + body + }); - name: Upload Pages artifact - uses: actions/upload-pages-artifact@v3 + uses: actions/upload-pages-artifact@v4 with: path: output deploy: needs: build + if: github.event_name == 'push' && github.ref == 'refs/heads/master' runs-on: ubuntu-latest + permissions: + contents: read + pages: write + id-token: write environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} @@ -48,3 +86,24 @@ jobs: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4 + + deploy_preview: + needs: build + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == false + runs-on: ubuntu-latest + permissions: + contents: read + pages: write + id-token: write + environment: + name: pr-${{ github.event.number }} + url: ${{ steps.deployment.outputs.page_url }} + steps: + - name: Deploy PR preview to GitHub Pages + id: deployment + uses: actions/deploy-pages@v4 + with: + preview: true + - name: Publish preview URL + run: | + echo "Preview URL: ${{ steps.deployment.outputs.page_url }}" >> $GITHUB_STEP_SUMMARY