|
| 1 | +name: build-preview |
| 2 | +run-name: build pull request preview |
| 3 | + |
| 4 | +on: |
| 5 | + # run when called from another workflow |
| 6 | + workflow_call: |
| 7 | + |
| 8 | + # run if user manually requests it |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +# variables |
| 12 | +env: |
| 13 | + PREVIEWS_FOLDER: preview |
| 14 | + |
| 15 | +permissions: |
| 16 | + contents: write |
| 17 | + pull-requests: write |
| 18 | + |
| 19 | +jobs: |
| 20 | + build-preview: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + |
| 23 | + steps: |
| 24 | + - name: Debug dump |
| 25 | + if: runner.debug == '1' |
| 26 | + uses: crazy-max/ghaction-dump-context@v2 |
| 27 | + |
| 28 | + - name: Checkout branch contents |
| 29 | + if: github.event.action != 'closed' |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + repository: ${{ github.event.pull_request.head.repo.full_name }} |
| 33 | + ref: ${{ github.head_ref }} |
| 34 | + |
| 35 | + - name: Install Ruby packages |
| 36 | + if: github.event.action != 'closed' |
| 37 | + uses: ruby/setup-ruby@v1 |
| 38 | + with: |
| 39 | + ruby-version: "3.1" |
| 40 | + bundler-cache: true |
| 41 | + |
| 42 | + - name: Get Pages url |
| 43 | + if: github.event.action != 'closed' |
| 44 | + id: pages |
| 45 | + uses: actions/configure-pages@v4 |
| 46 | + |
| 47 | + - name: SSH debug |
| 48 | + if: runner.debug == '1' |
| 49 | + uses: mxschmitt/action-tmate@v3 |
| 50 | + |
| 51 | + - name: Build preview version of site |
| 52 | + if: github.event.action != 'closed' |
| 53 | + run: | |
| 54 | + JEKYLL_ENV=production bundle exec jekyll build --baseurl "${{ steps.pages.outputs.base_path || '' }}/${{ env.PREVIEWS_FOLDER }}/pr-${{ github.event.number }}" |
| 55 | +
|
| 56 | + - name: Commit preview to Pages branch |
| 57 | + if: github.event.action != 'closed' |
| 58 | + uses: rossjrw/pr-preview-action@v1.4.7 |
| 59 | + with: |
| 60 | + source-dir: _site |
| 61 | + umbrella-dir: ${{ env.PREVIEWS_FOLDER }} |
| 62 | + |
| 63 | + - name: Checkout Pages branch |
| 64 | + uses: actions/checkout@v4 |
| 65 | + with: |
| 66 | + ref: gh-pages |
| 67 | + |
| 68 | + - name: Clean up preview folders |
| 69 | + uses: actions/github-script@v7 |
| 70 | + with: |
| 71 | + script: | |
| 72 | + const { rm } = require("fs").promises; |
| 73 | + const { PREVIEWS_FOLDER } = process.env; |
| 74 | + const { owner, repo } = context.repo; |
| 75 | + const prs = (await github.rest.pulls.list({ owner, repo, state: "closed" })).data; |
| 76 | + for (const { number } of prs) |
| 77 | + await rm(`${PREVIEWS_FOLDER}/pr-${number}`, { recursive: true, force: true }); |
| 78 | +
|
| 79 | + - name: Commit changed files |
| 80 | + uses: stefanzweifel/git-auto-commit-action@v5 |
| 81 | + with: |
| 82 | + branch: gh-pages |
| 83 | + commit_message: "Clean up previews" |
0 commit comments