# deploy-pr-app.yaml - deploy IntegrityAnalysis_PR_ automatically # for every pull request (deploy trio, part 2). # # PROVENANCE: written by Claude Code (model Claude Fable 5), 2026-08-19. # Automates the PR-app convention from AGENTS.md: every PR gets a test # app that IDENTIFIES ITSELF in the UI (Steve's rule, 2026-08-16) - the # banner names the PR and its title, so triage never requires opening # GitHub. The shim is generated with deparse() so titles containing # quotes cannot break out of the R string. Redeploys on every push to # the PR branch; the companion cleanup workflow purges the app when the # PR closes. Same-repository PRs only (fork PRs get no secrets, by # GitHub's own rules - and this repository takes no fork PRs). # # 2026-11-28 (Claude Code, Claude Opus 5.5): a second job, # notify-devin-qa, wakes the Devin QA Engineer bot once the app is live. # It runs none of the PR's code (no checkout, no Action) and holds only # the webhook's URL and secret (DEVIN_QA_WEBHOOK_URL, # DEVIN_QA_WEBHOOK_SECRET), which the deploy job never sees: that job # runs the PR's code and its dependencies' install scripts. name: deploy-pr-app on: pull_request: types: [opened, synchronize, reopened] permissions: contents: read pull-requests: write # to post the app link as a PR comment concurrency: group: pr-app-${{ github.event.pull_request.number }} cancel-in-progress: true # only the newest push per PR deploys jobs: deploy: runs-on: ubuntu-latest timeout-minutes: 120 outputs: url: ${{ steps.link.outputs.url }} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 with: r-version: '4.5.3' # matches renv.lock use-public-rspm: true # renv.lock supplies the runtime library; deploy tooling floats - name: System libraries (ubuntu) # setup-r-dependencies used to install these automatically from # SystemRequirements via pak; setup-renv does not, and RSPM # binaries install fine but fail to LOAD without them (found: # pdftools "required" errors = libpoppler-cpp missing). run: | sudo apt-get update -qq sudo apt-get install -y --no-install-recommends libpoppler-cpp-dev libcurl4-openssl-dev libssl-dev libxml2-dev libfontconfig1-dev libfreetype6-dev libharfbuzz-dev libfribidi-dev libpng-dev libjpeg-dev libtiff5-dev libcairo2-dev libtesseract-dev libleptonica-dev tesseract-ocr-eng - uses: r-lib/actions/setup-renv@v2 - name: Rebuild Rfast from source (RSPM binary/TBB ABI mismatch) # The prebuilt Linux Rfast binary fails to load - undefined TBB # symbol against the restored RcppParallel. A source build links # RcppParallel's own TBB correctly. renv caches the result, so # this compiles once per lockfile, not once per run. run: | Rscript -e 'options(pkgType = "source"); renv::install("Rfast@2.1.5.2", rebuild = TRUE, prompt = FALSE)' - name: Deploy tooling (floating) # install.packages() only WARNS on failure (see cleanup-pr-app); # verify the namespaces or stop run: | Rscript -e ' pk <- c("rsconnect", "remotes") install.packages(pk) miss <- pk[!vapply(pk, requireNamespace, logical(1), quietly = TRUE)] if (length(miss)) install.packages(miss, repos = "https://cloud.r-project.org") stopifnot(all(vapply(pk, requireNamespace, logical(1), quietly = TRUE)))' - name: Install the PR head from GitHub and deploy the banner app env: SHINY_TOKEN: ${{ secrets.SHINY_TOKEN }} SHINY_SECRET: ${{ secrets.SHINY_SECRET }} SHINY_ACCOUNT: ${{ vars.SHINY_ACCOUNT }} SHINY_APP_NAME: ${{ vars.SHINY_APP_NAME }} PR_NUMBER: ${{ github.event.pull_request.number }} PR_TITLE: ${{ github.event.pull_request.title }} PR_SHA: ${{ github.event.pull_request.head.sha }} run: | Rscript -e ' # First deploys from a fresh runner manifest force shinyapps to # build a new package image, which can exceed rsconnect'"'"'s # default per-request timeout while the client polls the build options(rsconnect.http.timeout = 900) remotes::install_github("StevenLShafer/IntegrityAnalysis", ref = Sys.getenv("PR_SHA"), upgrade = "never", force = TRUE) note <- paste0("PR #", Sys.getenv("PR_NUMBER"), ": ", Sys.getenv("PR_TITLE"), " - see the PR description for what to test.") appDir <- file.path(tempdir(), "prapp") dir.create(appDir) writeLines(sprintf("IntegrityAnalysis::run_app(testNote = %s)", deparse(note)), file.path(appDir, "app.R")) rsconnect::setAccountInfo( name = Sys.getenv("SHINY_ACCOUNT"), token = Sys.getenv("SHINY_TOKEN"), secret = Sys.getenv("SHINY_SECRET")) # Same transient-collision retry as deploy-production.yaml - # see the long note there. shinyapps.io serializes deploys # per ACCOUNT, so this workflow and the production one # collide whenever they overlap; the API says to try again, # so this tries again. Deliberately NOT a shared concurrency # group, which would let PR traffic cancel a pending # production deploy. deployWithRetry <- function(tries = 4, wait = 90, ...) { for (k in seq_len(tries)) { done <- tryCatch({ rsconnect::deployApp(...); TRUE }, error = function(e) { msg <- conditionMessage(e) if (!grepl("in progress|try again", msg, ignore.case = TRUE) || k == tries) stop(e) cat("attempt", k, "hit a transient deploy collision;", "retrying in", wait, "seconds\n") Sys.sleep(wait) FALSE }) if (isTRUE(done)) return(invisible(TRUE)) } } deployWithRetry( appDir = appDir, appName = paste0(Sys.getenv("SHINY_APP_NAME"), "_PR_", Sys.getenv("PR_NUMBER")), appFiles = "app.R", account = Sys.getenv("SHINY_ACCOUNT"), server = "shinyapps.io", forceUpdate = TRUE, launch.browser = FALSE)' - name: Post the test-app link on the PR (once) id: link env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_NUMBER: ${{ github.event.pull_request.number }} SHINY_ACCOUNT: ${{ vars.SHINY_ACCOUNT }} SHINY_APP_NAME: ${{ vars.SHINY_APP_NAME }} run: | url="https://${SHINY_ACCOUNT}.shinyapps.io/${SHINY_APP_NAME}_PR_${PR_NUMBER}/" echo "url=$url" >> "$GITHUB_OUTPUT" # for notify-devin-qa if ! gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" \ --json comments -q '.comments[].body' | grep -qF "$url"; then gh pr comment "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" \ --body "Test app deployed: $url (banner names this PR; redeployed on every push; purged automatically when the PR closes)" fi # Wakes the Devin QA Engineer bot, which tests the live app in a browser # and reports on the PR. It runs none of the PR's code (no checkout, no # Action) and is the only job that holds the webhook's URL and secret. # Everything reaches the script as environment variables, never # interpolated into it, and the address, which comes from the job the # PR's code ran in, must be a shinyapps.io app named for this PR. # Nothing here can fail the run: an outage is a warning, and with no # DEVIN_QA_WEBHOOK_URL the step does nothing. A notice for a commit the # PR has already moved past is not sent: that commit's own run sends one. notify-devin-qa: needs: deploy runs-on: ubuntu-24.04 timeout-minutes: 5 permissions: pull-requests: read steps: - name: Tell Devin QA the test app is live continue-on-error: true env: DEVIN_QA_WEBHOOK_URL: ${{ secrets.DEVIN_QA_WEBHOOK_URL }} DEVIN_QA_WEBHOOK_SECRET: ${{ secrets.DEVIN_QA_WEBHOOK_SECRET }} PREVIEW_URL: ${{ needs.deploy.outputs.url }} PR_NUMBER: ${{ github.event.pull_request.number }} HEAD_SHA: ${{ github.event.pull_request.head.sha }} REPO: ${{ github.repository }} GH_TOKEN: ${{ github.token }} run: | set -euo pipefail if [ -z "$DEVIN_QA_WEBHOOK_URL" ]; then echo "DEVIN_QA_WEBHOOK_URL is not set: Devin QA not notified." exit 0 fi case "$DEVIN_QA_WEBHOOK_URL" in https://*) ;; *) echo "::warning::DEVIN_QA_WEBHOOK_URL is not https: the secret was not sent." exit 1 ;; esac if ! [[ "$PR_NUMBER" =~ ^[0-9]+$ && "$HEAD_SHA" =~ ^[0-9a-f]{40}$ ]]; then echo "::warning::Unexpected PR number or head SHA: Devin QA not notified." exit 1 fi # the same check as stanpumpR's comment-pr, and the app must be this PR's address='^https://[A-Za-z0-9-]+\.shinyapps\.io/[A-Za-z0-9_]+/$' if ! [[ "$PREVIEW_URL" =~ $address && "$PREVIEW_URL" == *"_PR_${PR_NUMBER}/" ]]; then echo "::warning::Unexpected test-app address $(jq -n --arg u "$PREVIEW_URL" '$u'): Devin QA not notified." exit 1 fi head=$(gh api "repos/$REPO/pulls/$PR_NUMBER" --jq .head.sha 2>/dev/null || true) if [ -n "$head" ] && [ "$head" != "$HEAD_SHA" ]; then echo "The PR has moved on to $head; that commit's run will notify Devin QA." exit 0 fi body=$(jq -cn --arg repo "$REPO" --argjson pr "$PR_NUMBER" \ --arg sha "$HEAD_SHA" --arg url "$PREVIEW_URL" \ '{repo: $repo, pr: $pr, sha: $sha, url: $url}') if ! curl --fail --silent --show-error --max-time 30 \ --header 'Content-Type: application/json' \ --header "X-Webhook-Secret: $DEVIN_QA_WEBHOOK_SECRET" \ --data "$body" --output /dev/null "$DEVIN_QA_WEBHOOK_URL"; then echo "::warning::The Devin QA webhook failed: the test app is live, but Devin QA was not told." exit 1 fi echo "Devin QA notified: PR #$PR_NUMBER at ${HEAD_SHA:0:7}, $PREVIEW_URL"