Repository navigation
ci(previews): deploy a live Studio preview for each PR #2054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dawsontoth
wants to merge
6
commits into
stage
Choose a base branch
from
claude/studio-pr-previews
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+373
β0
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9ecdf6
ci(previews): deploy a Studio preview for each PR with deploy-action
dawsontoth 9cabc25
ci(previews): match the build by branch, and tighten the fork-guard test
dawsontoth da14a84
ci(previews): find the build by its title, and refuse grouped guards
dawsontoth 7ee4967
ci(previews): only listed users get previews, and previews share data
dawsontoth 7465406
ci(previews): quiet cleanup when previews are off, and name dataMode=β¦
dawsontoth 5813c0c
test(previews): read a reflowed guard, and fail on shapes the guard tβ¦
dawsontoth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| name: Build PR Preview | ||
|
|
||
| # Runs the PR's code, so it gets a read-only token and no identity token; preview.yaml deploys its | ||
| # artifact. Keep `branches` the same as there: its deploy job fails when no run of this one starts. | ||
|
|
||
| # preview.yaml finds this run by its title: keep the two the same. | ||
| run-name: 'Build PR Preview #${{ github.event.pull_request.number }}' | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| branches: [stage] | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| # A newer push cancels the build of the commit it replaces; that commit's preview run then stops | ||
| # waiting and deploys nothing. | ||
| concurrency: | ||
| group: preview-build-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| build: | ||
| name: Build PR Preview | ||
| # The same users as preview.yaml's deploy job: nobody else's build would be used. | ||
| if: >- | ||
| github.event.pull_request.head.repo.full_name == github.repository | ||
| && contains(fromJSON(vars.HARPER_PREVIEW_AUTHORS || '[]'), github.event.pull_request.user.login) | ||
| && contains(fromJSON(vars.HARPER_PREVIEW_AUTHORS || '[]'), github.actor) | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 20 | ||
| steps: | ||
| - name: Checkout PR code | ||
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | ||
| with: | ||
| # The head commit, not the merge commit: the preview records this SHA. | ||
| ref: ${{ github.event.pull_request.head.sha }} | ||
| persist-credentials: false | ||
| - name: Set up pnpm | ||
| uses: pnpm/action-setup@ea17c68df8912ef543352723c149a84f56e3d413 # v6.1.0 | ||
| - name: Set up Node.js | ||
| uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 | ||
| with: | ||
| cache: 'pnpm' | ||
| cache-dependency-path: 'pnpm-lock.yaml' | ||
| node-version-file: '.nvmrc' | ||
| - name: Disable Husky | ||
| run: echo "HUSKY=0" >> "$GITHUB_ENV" | ||
| - name: Install dependencies | ||
| run: pnpm install | ||
| # Dev mode points the bundle at the dev central manager. `VITE_TELEMETRY_ENABLED` stays unset so | ||
| # `isDeployedBuild` is false: a preview must not report to Datadog RUM or GTM. | ||
| - name: Build | ||
| env: | ||
| PR: ${{ github.event.pull_request.number }} | ||
| SHA: ${{ github.event.pull_request.head.sha }} | ||
| run: VITE_STUDIO_VERSION="pr-${PR}_${SHA:0:7}" pnpm build --mode dev | ||
| # Dev mode writes hidden sourcemaps for Datadog, which a preview never uploads. | ||
| - name: Drop sourcemaps | ||
| run: find web -name '*.js.map' -delete | ||
| - name: Upload build artifact | ||
| uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1 | ||
| with: | ||
| name: pr-${{ github.event.pull_request.number }}-build | ||
| path: web/ | ||
| if-no-files-found: error | ||
| retention-days: 7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| name: Preview | ||
|
|
||
| # Runs from stage with the cluster's preview identity, so nothing here may check out or run a PR's | ||
| # code: preview-build.yaml builds it unprivileged, and this deploys only its `web/`. The cluster | ||
| # trusts this file by path, branch, event and environment (`harper preview setup app=hdbms | ||
| # dataMode=shared`, which `branched_databases: false` below needs). See | ||
| # .github/deploying.md and https://github.com/HarperFast/deploy-action#pull-request-previews | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, reopened, synchronize, closed] | ||
| # Keep the same as in preview-build.yaml. | ||
| branches: [stage] | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| deploy: | ||
| # A preview's JavaScript runs with its visitor's dev CM session, so it deploys only for a PR a | ||
| # listed user opened, on an event a listed user caused. HARPER_PREVIEW_AUTHORS is a JSON array of | ||
| # GitHub logins; unset, nobody gets a preview. | ||
| if: >- | ||
| github.event.action != 'closed' | ||
| && github.event.pull_request.head.repo.full_name == github.repository | ||
| && contains(fromJSON(vars.HARPER_PREVIEW_AUTHORS || '[]'), github.event.pull_request.user.login) | ||
| && contains(fromJSON(vars.HARPER_PREVIEW_AUTHORS || '[]'), github.actor) | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 60 | ||
| environment: preview | ||
| permissions: | ||
| id-token: write | ||
| pull-requests: write | ||
| deployments: write | ||
| # Reading preview-build.yaml's runs and downloading their artifacts. | ||
| actions: read | ||
| concurrency: | ||
| group: harper-preview-${{ github.event.pull_request.number }} | ||
| steps: | ||
| # GitHub starts both workflows at once, and the trust policy matches only pull_request_target | ||
| # runs, so a workflow_run trigger cannot deploy: this job waits for the build itself. Another | ||
| # PR can have a run at the same commit, so the run's title has to name this one. | ||
| - name: Wait for the build of this commit | ||
| id: build | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| PR: ${{ github.event.pull_request.number }} | ||
| SHA: ${{ github.event.pull_request.head.sha }} | ||
| # preview-build.yaml's run-name. | ||
| BUILD_TITLE: 'Build PR Preview #${{ github.event.pull_request.number }}' | ||
| BUILD_WORKFLOW: preview-build.yaml | ||
| run: | | ||
| short=${SHA:0:7} | ||
| superseded() { | ||
| local pr state head | ||
| pr=$(gh api "repos/$GITHUB_REPOSITORY/pulls/$PR" --jq '.state + " " + .head.sha') || return 1 | ||
| read -r state head <<<"$pr" | ||
| [ "$state" = open ] && [ "$head" = "$SHA" ] && return 1 | ||
| echo "::notice::Not deploying $short: PR #$PR is $state at ${head:0:7}." | ||
| } | ||
| start=$SECONDS | ||
| while :; do | ||
| if superseded; then exit 0; fi | ||
| listed=1 | ||
| run=$(gh api "repos/$GITHUB_REPOSITORY/actions/workflows/$BUILD_WORKFLOW/runs?event=pull_request&head_sha=$SHA" \ | ||
| --jq '[.workflow_runs[] | select(.head_repository.full_name == env.GITHUB_REPOSITORY and .display_title == env.BUILD_TITLE)] | max_by(.run_number) | select(.) | "\(.id) \(.status) \(.conclusion)"') || { listed=0; run=; } | ||
| elapsed=$((SECONDS - start)) | ||
| if [ -n "$run" ]; then | ||
| read -r id status conclusion <<<"$run" | ||
| if [ "$status" = completed ]; then | ||
| if [ "$conclusion" = success ]; then | ||
| echo "Deploying the build from $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$id" | ||
| echo "run-id=$id" >>"$GITHUB_OUTPUT" | ||
| exit 0 | ||
| fi | ||
| if superseded; then exit 0; fi | ||
| echo "::error::The build of $short ended $conclusion: $GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$id" | ||
| exit 1 | ||
| fi | ||
| # A failed listing is not evidence that no run started. | ||
| elif [ "$listed" = 1 ] && [ "$elapsed" -ge 600 ]; then | ||
| echo "::error::No $BUILD_WORKFLOW run for $short started in 10 minutes. GitHub starts no pull_request run while a PR has merge conflicts." | ||
| exit 1 | ||
| fi | ||
| if [ "$elapsed" -ge 1800 ]; then | ||
| echo "::error::The build of $short did not finish in 30 minutes." | ||
| exit 1 | ||
| fi | ||
| sleep 20 | ||
| done | ||
|
|
||
| - name: Download the build | ||
| if: steps.build.outputs.run-id != '' | ||
| uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 | ||
| with: | ||
| name: pr-${{ github.event.pull_request.number }}-build | ||
| path: site/web | ||
| run-id: ${{ steps.build.outputs.run-id }} | ||
| github-token: ${{ github.token }} | ||
|
|
||
| # Written here, outside the artifact, so the PR controls only files the static plugin serves. | ||
| - name: Serve the build as a Harper application | ||
| if: steps.build.outputs.run-id != '' | ||
| run: | | ||
| cat > site/config.yaml << 'EOF' | ||
| static: | ||
| files: 'web/**' | ||
| urlPath: '/' | ||
| index: true | ||
| EOF | ||
|
|
||
| - if: steps.build.outputs.run-id != '' | ||
| uses: HarperFast/deploy-action@45fa59b6157eabb72b7a74922309238671f2a37d # v1.0.0 | ||
| with: | ||
| target: ${{ vars.HARPER_PREVIEW_TARGET }} | ||
| app: hdbms | ||
| preview: true | ||
| by_ref: false | ||
| path: site | ||
| # Branching forks every database on the node but `system`, CM's own included. The payload | ||
| # runs no server code, so it touches no data. | ||
| branched_databases: false | ||
|
|
||
| cleanup: | ||
| if: github.event.action == 'closed' && github.event.pull_request.head.repo.full_name == github.repository | ||
| runs-on: ubuntu-latest | ||
| environment: preview-cleanup | ||
| permissions: | ||
| id-token: write | ||
| pull-requests: write | ||
| deployments: write | ||
| steps: | ||
| # Gated here, not on the job: `harper preview setup` can only verify that cleanup runs on close | ||
| # when the job's `if:` has nothing else in it. The author alone, so whoever merges, cleans up. | ||
| - if: >- | ||
| vars.HARPER_PREVIEW_TARGET != '' | ||
| && contains(fromJSON(vars.HARPER_PREVIEW_AUTHORS || '[]'), github.event.pull_request.user.login) | ||
| uses: HarperFast/deploy-action@45fa59b6157eabb72b7a74922309238671f2a37d # v1.0.0 | ||
| with: | ||
| target: ${{ vars.HARPER_PREVIEW_TARGET }} | ||
| app: hdbms | ||
| preview: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.