From 6af0c9925e2d9e7ab33490193b762dc346eea223 Mon Sep 17 00:00:00 2001 From: VisruthSK Date: Fri, 9 Oct 2026 14:57:19 -0700 Subject: [PATCH] Add PR pkgdown workflow --- .github/workflows/pkgdown.yaml | 86 ++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 3dc20b10..c0238134 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -1,10 +1,10 @@ # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help on: - # build dev site on merged pushes push: branches: [main, master] - # build full site on releases + pull_request: + types: [opened, reopened, synchronize, labeled, closed] release: types: [published] workflow_dispatch: @@ -13,10 +13,16 @@ name: pkgdown.yaml jobs: pkgdown: + if: >- + github.event_name != 'pull_request' || + ( + github.event.action != 'closed' && + contains(github.event.pull_request.labels.*.name, 'build-website') && + (github.event.action != 'labeled' || github.event.label.name == 'build-website') + ) runs-on: ubuntu-latest - # Only restrict concurrency for non-PR jobs concurrency: - group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} + group: pkgdown-${{ github.event_name == 'pull_request' && github.event.pull_request.number || 'production' }} cancel-in-progress: true env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} @@ -55,20 +61,82 @@ jobs: stan-dev/pkgdown-config - name: Build site + env: + PR_NUMBER: ${{ github.event.pull_request.number }} run: | + pkg <- pkgdown::as_pkgdown(".") + override <- list() + + pr_number <- Sys.getenv("PR_NUMBER") + if (nzchar(pr_number)) { + if (pkg$development$mode == "devel") { + Sys.setenv(PKGDOWN_DEV_MODE = "unreleased") + } + + override$url <- paste0(pkg$meta$url, "/pr/", pr_number) + override$template$params$noindex <- TRUE + } + withr::with_envvar( - c("NOT_CRAN" = "true"), # this should already be set by setup-r@v2? keeping because vignettes don't build otherwise + c("NOT_CRAN" = "true"), pkgdown::build_site_github_pages( - lazy = FALSE, # change to TRUE if runner times out. + lazy = FALSE, run_dont_run = TRUE, - new_process = TRUE + new_process = TRUE, + override = override ) ) shell: Rscript {0} - - name: Deploy to GitHub pages 🚀 + - name: Deploy PR preview + if: > + github.event_name == 'pull_request' && + github.event.pull_request.head.repo.full_name == github.repository + uses: JamesIves/github-pages-deploy-action@v4 + with: + branch: gh-pages + folder: docs + target-folder: pr/${{ github.event.pull_request.number }} + clean: true + force: false + + - name: Deploy site + if: github.event_name != 'pull_request' uses: JamesIves/github-pages-deploy-action@v4 with: - clean: false branch: gh-pages folder: docs + clean: false + force: false + + cleanup-preview: + if: >- + github.event_name == 'pull_request' && github.event.action == 'closed' + && github.event.pull_request.head.repo.full_name == github.repository + runs-on: ubuntu-latest + concurrency: + group: pkgdown-${{ github.event.pull_request.number }} + cancel-in-progress: true + permissions: + contents: write + steps: + - uses: actions/checkout@v7 + with: + ref: gh-pages + + - name: Remove PR preview + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + run: | + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + + git rm -r --ignore-unmatch "pr/$PR_NUMBER" + + if git diff --cached --quiet; then + exit 0 + fi + + git commit -m "Remove pkgdown preview for PR #$PR_NUMBER" + git pull --rebase origin gh-pages + git push origin HEAD:gh-pages