From 1b22539d6e27e64a5c1c7d546283ace430f45b68 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Sat, 24 Oct 2026 01:20:10 -0400 Subject: [PATCH 1/4] Rename github-pages.yml to litedown-site.yml and use the shared site workflow Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/{github-pages.yml => litedown-site.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{github-pages.yml => litedown-site.yml} (100%) diff --git a/.github/workflows/github-pages.yml b/.github/workflows/litedown-site.yml similarity index 100% rename from .github/workflows/github-pages.yml rename to .github/workflows/litedown-site.yml From d0b3ef32b085fe74f0cd9d61c4621f00cd574602 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Sat, 24 Oct 2026 01:59:34 -0400 Subject: [PATCH 2/4] Force-switch to gh-pages when deploying the site Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/litedown-site.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/litedown-site.yml b/.github/workflows/litedown-site.yml index aa185ac..d447326 100644 --- a/.github/workflows/litedown-site.yml +++ b/.github/workflows/litedown-site.yml @@ -29,7 +29,7 @@ jobs: git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" if git fetch origin gh-pages; then - git switch gh-pages + git switch -f gh-pages else git switch --orphan gh-pages git rm -rf . 2>/dev/null || true From 4601d7ce78ab7a35b080b2aa28cd39cbb3b43644 Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Sat, 24 Oct 2026 02:01:20 -0400 Subject: [PATCH 3/4] Use the shared site workflow and add its badge to the README Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/litedown-site.yml | 89 +++++++++++++++++++++++++++-- README.md | 1 + 2 files changed, 85 insertions(+), 5 deletions(-) diff --git a/.github/workflows/litedown-site.yml b/.github/workflows/litedown-site.yml index d447326..9464433 100644 --- a/.github/workflows/litedown-site.yml +++ b/.github/workflows/litedown-site.yml @@ -1,28 +1,88 @@ -name: Build and deploy package site +name: Package site on: push: - branches: ["main"] + branches: ["main", "master"] + pull_request: + types: [opened, reopened, synchronize, closed] + delete: permissions: contents: write + pull-requests: write # comment the preview URL + +env: + SURGE_TOKEN: ${{ secrets.SURGE_TOKEN }} jobs: + # The domain of the PR preview (--pr-.surge.sh), derived once + # and shared by the jobs below. + preview: + runs-on: ubuntu-latest + outputs: + domain: ${{ steps.domain.outputs.domain }} + steps: + - id: domain + env: + GH_TOKEN: ${{ github.token }} + REF: ${{ github.event.ref }} + run: | + if [ "${{ github.event_name }}" = "pull_request" ]; then + num="${{ github.event.pull_request.number }}" + elif [ "${{ github.event_name }}" = "delete" ]; then + num=$(gh pr list --repo "${{ github.repository }}" --state all \ + --head "$REF" --json number --jq '.[0].number // empty') + fi + if [ -n "$num" ]; then + name=$(echo "${{ github.repository }}-pr-$num" | + tr 'A-Z' 'a-z' | tr -c 'a-z0-9\n' '-') + echo "domain=$name.surge.sh" >> "$GITHUB_OUTPUT" + fi + deploy: + needs: preview + # Skip closed PRs (see teardown) and fork PRs, which cannot read the secret + # or write to the repo. + if: >- + github.event_name == 'push' || ( + github.event_name == 'pull_request' && + github.event.action != 'closed' && + github.event.pull_request.head.repo.full_name == github.repository) + # One run per ref; a new push supersedes the previous one. + concurrency: + group: site-${{ github.ref }} + cancel-in-progress: true runs-on: ubuntu-latest + env: + PREVIEW_DOMAIN: ${{ needs.preview.outputs.domain }} steps: - uses: actions/checkout@v7 - - uses: r-lib/actions/setup-r@HEAD + - uses: r-lib/actions/setup-r@v2 with: use-public-rspm: true - - uses: yihui/actions/setup-r-dependencies@HEAD + - uses: yihui/actions/setup-r-dependencies@v1.4.0 with: - extra-packages: litedown xfun . + extra-packages: litedown . r-universe: yihui - uses: yihui/actions/litedown-site@HEAD with: site-dir: 'site' + - name: Deploy PR preview to surge + if: github.event_name == 'pull_request' && env.SURGE_TOKEN != '' + run: | + npm install -g surge + surge ./site "$PREVIEW_DOMAIN" + - name: Comment preview URL + if: github.event_name == 'pull_request' && env.SURGE_TOKEN != '' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: surge-preview + message: | + Site preview: https://${{ env.PREVIEW_DOMAIN }} + + Updates on every push; removed when this PR closes. - name: Deploy to gh-pages + if: github.event_name == 'push' run: | tmp=$(mktemp -d) cp -r site/. "$tmp" @@ -38,3 +98,22 @@ jobs: cp -r "$tmp"/. . git add -A git diff --cached --quiet || (git commit -m "deploy site" && git push origin gh-pages) + + # Remove the preview of a PR. A deleted branch is the primary trigger, since + # deleting the branch at merge time cancels `closed` runs; `closed` covers PRs + # whose branch is kept. Teardown is idempotent. + teardown: + needs: preview + # Ignore tag deletions; only branches map to a preview. + if: >- + github.event.action == 'closed' || + (github.event_name == 'delete' && github.event.ref_type == 'branch') + runs-on: ubuntu-latest + env: + PREVIEW_DOMAIN: ${{ needs.preview.outputs.domain }} + steps: + - name: Teardown preview + if: env.SURGE_TOKEN != '' && env.PREVIEW_DOMAIN != '' + run: | + npm install -g surge + surge teardown "$PREVIEW_DOMAIN" || true diff --git a/README.md b/README.md index 5e7a03f..ae1858f 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![R-CMD-check](https://github.com/yihui/servr/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/yihui/servr/actions/workflows/R-CMD-check.yaml) [![CRAN release](https://www.r-pkg.org/badges/version/servr)](https://cran.r-project.org/package=servr) +[![Build site](https://github.com/yihui/servr/actions/workflows/litedown-site.yml/badge.svg)](https://github.com/yihui/servr/actions/workflows/litedown-site.yml) From 7b042b7a60f1761dd40b8c0fa4561c8ac8fb9caf Mon Sep 17 00:00:00 2001 From: Yihui Xie Date: Sun, 25 Oct 2026 23:50:04 -0400 Subject: [PATCH 4/4] Build the site on macOS Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/litedown-site.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/litedown-site.yml b/.github/workflows/litedown-site.yml index 9464433..f658e80 100644 --- a/.github/workflows/litedown-site.yml +++ b/.github/workflows/litedown-site.yml @@ -52,7 +52,7 @@ jobs: concurrency: group: site-${{ github.ref }} cancel-in-progress: true - runs-on: ubuntu-latest + runs-on: macos-latest env: PREVIEW_DOMAIN: ${{ needs.preview.outputs.domain }} steps: