Deploy a Harper application from GitHub Actions, and give every pull request its own preview at https://<branch>.app.<cluster-domain>/.
The action authenticates with the job's OIDC identity token and nothing else. You store no username, password or token: the cluster trusts your workflow file, through a trust policy that a one-time setup command writes. The action talks only to your cluster and to GitHub.
Run this once from a checkout of the repository, signed in to the cluster as a super_user (harper login). It creates a deploy role and user and the trust policy github-actions-<app>, and sets the HARPER_CLI_TARGET repository variable:
harper deploy setup=true provider=github-actions project=shopThen commit .github/workflows/deploy.yaml, the file and the production environment that the policy pins:
name: Deploy
on:
push:
branches: [main]
permissions: {}
jobs:
deploy:
runs-on: ubuntu-latest
environment: production
permissions:
id-token: write
steps:
- uses: HarperFast/deploy-action@v1
with:
target: ${{ vars.HARPER_CLI_TARGET }}
app: shop
by_ref: trueWith by_ref: true the cluster fetches the commit itself, so the job needs no checkout. A private repository also needs credential: true, once harper deploy setup=true has sealed a credential.
An application that needs a build deploys a directory instead. Check out the code, build it, and name the output as path:
steps:
- uses: actions/checkout@v7
- run: npm ci && npm run build
- uses: HarperFast/deploy-action@v1
with:
target: ${{ vars.HARPER_CLI_TARGET }}
app: shop
path: distPreviews need Harper 5.4 on the cluster.
The workflow goes in .github/workflows/preview.yaml on your default branch. GitHub runs pull_request_target workflows from there, so a PR cannot change what runs with the preview identity. From Harper v5.4.3, harper preview setup offers to write it for you when the file doesn't exist (write_workflow=true where there is no terminal to ask in); its starter workflow is like this one, with the action pinned by commit. To write it yourself, commit this:
name: Preview
on:
# In a public repository, an Actions event policy has to allow pull_request_target for this file:
# https://github.com/HarperFast/deploy-action#allowing-pull_request_target
pull_request_target:
types: [opened, reopened, synchronize, closed]
branches: [main]
permissions: {}
jobs:
deploy:
if: vars.HARPER_PREVIEW_TARGET != '' && github.event.action != 'closed' && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
environment: preview
permissions:
id-token: write
pull-requests: write
deployments: write
concurrency:
group: harper-preview-${{ github.event.pull_request.number }}
steps:
- uses: HarperFast/deploy-action@v1
with:
target: ${{ vars.HARPER_PREVIEW_TARGET }}
app: shop
preview: true
cleanup:
if: vars.HARPER_PREVIEW_TARGET != '' && 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:
- uses: HarperFast/deploy-action@v1
with:
target: ${{ vars.HARPER_PREVIEW_TARGET }}
app: shop
preview: trueBoth jobs stay skipped while HARPER_PREVIEW_TARGET is unset or empty.
Run setup once from a checkout of the repository, signed in as a super_user:
harper preview setup app=shopWithout a terminal to ask in, add write_workflow=true for the starter and, in a public repository, actions_policy=true. Commit a starter setup wrote to the default branch when setup says to.
Setup reads the workflow and refuses one that would give a fork or a PR's code the preview identity. It then enables previews on the cluster, which creates the role and user <app>-ci-preview and the trust policies github-actions-<app>-preview (environment preview) and github-actions-<app>-preview-cleanup (environment preview-cleanup, drop only). It also sets the HARPER_PREVIEW_TARGET repository variable. To remove it all, previews included, run harper preview teardown app=shop. Teardown leaves the GitHub side as it is: the workflow, the variable, the environments and the Actions event policy. Remove what no other workflow or repository still uses.
Some setup options have a matching input:
harper preview setup |
Action input |
|---|---|
domain=<domain> |
domain: <domain> |
dataMode=shared |
branched_databases: false |
credential=true |
credential: true |
An application served under a path, such as a Next.js app with a basePath, sets url_path (url_path: /shop) so that the comment and the Deployment link to it rather than to the host's root.
In a public repository, an Actions event policy has to allow pull_request_target for the workflow. Setup v5.4.3 and later create it for the starter workflow they write, after asking (actions_policy=true where there is no terminal to ask in), and report a policy that blocks the event instead. For any other workflow, create it yourself.
| Event | What the action does |
|---|---|
opened, reopened, synchronize |
Deploys the PR's head commit by reference, or its build, only while the PR is open at that commit. Keeps one PR comment with the URL, updated in place, and a GitHub Deployment with environment_url. If the PR closed during the deploy, it drops what it deployed. |
closed |
Drops the preview, marks its Deployments inactive and updates the comment. It retries for two minutes while a deploy still holds the preview, and a PR that was reopened keeps its preview. |
schedule, workflow_dispatch |
Sweeps the previews of closed PRs. |
Anything from a fork, or another pull_request action |
Nothing. The cluster is never called. |
The PR comment and the Deployment are best effort: if either fails, the run warns and the deploy still counts. Deployments use the GitHub environment <app>-preview (deployment_environment), kept apart from the preview environment that gates the job, so its required reviewers and its own deployment records stay out of it.
Required reviewers on the preview environment gate every deploy, which you want when previews share a cluster with production. They must not gate cleanup, or a closed PR's preview waits for an approval. So cleanup runs in preview-cleanup, which should have no required reviewers, under a policy that can only drop. It also stays out of the deploy's concurrency group, where a deploy waiting for approval would hold it up. Keep that group on the deploy job: two deploys of one PR must not overlap, or the older one can leave its commit on the cluster. A deploy that is still running when the PR closes drops its own preview when it finishes.
In Settings > Environments, allow your default branch under Deployment branches and tags on both environments.
The cluster expires every preview on its own: seven days after its last deploy by default, or the application's maxTtlDays. That already covers a missed close event. A sweep removes those previews sooner.
The policies harper preview setup writes pin event_name: pull_request_target, so a scheduled run cannot use them, and a sweep also has to list what is deployed. It needs its own workflow file and its own trust policy.
Commit .github/workflows/preview-sweep.yaml:
name: Preview sweep
on:
schedule:
- cron: '17 3 * * *'
workflow_dispatch:
permissions: {}
jobs:
sweep:
runs-on: ubuntu-latest
environment: preview-cleanup
permissions:
id-token: write
pull-requests: write
deployments: write
steps:
- uses: HarperFast/deploy-action@v1
with:
target: ${{ vars.HARPER_PREVIEW_TARGET }}
app: shop
preview: trueThen add the policy as a super_user, with your repository's numeric id (gh repo view --json databaseId), name, branch and target:
harper add_oidc_trust id=github-actions-shop-preview-sweep \
issuer=https://token.actions.githubusercontent.com \
audience=https://cluster.example.com:9925/ \
user=shop-ci-preview \
'operations=["get_components","drop_component"]' \
'claims={"repository_id":"123456789","workflow_ref":"acme/shop/.github/workflows/preview-sweep.yaml@refs/heads/main","event_name":["schedule","workflow_dispatch"],"environment":"preview-cleanup"}'harper preview teardown does not know about this policy, so drop it yourself: harper drop_oidc_trust id=github-actions-shop-preview-sweep.
The preview job must not build a PR: that would run the PR's code with the preview identity. Build each PR in a separate, unprivileged pull_request workflow, and have the preview job deploy what it built.
With build_workflow, the action does that. It waits for the build workflow's run of the PR's head commit, downloads the run's artifact through the Actions API, and deploys it as a payload. It takes only a pull_request run of the PR's branch in this repository, never a fork's run for the same commit, and the newest of those. The preview fails if that run does not succeed, or has not finished after build_timeout minutes. If the PR closes or moves on to a newer commit meanwhile, the run stops waiting and leaves that commit's run to report.
Commit the build workflow, here .github/workflows/preview-build.yaml:
name: Preview build
on:
pull_request:
types: [opened, reopened, synchronize]
branches: [main]
permissions:
contents: read
concurrency:
group: preview-build-${{ github.event.pull_request.number }}
cancel-in-progress: true
jobs:
build:
if: vars.HARPER_PREVIEW_TARGET != '' && github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
with:
# The PR's head commit, which the preview records, rather than the merge commit.
ref: ${{ github.event.pull_request.head.sha }}
persist-credentials: false
- run: npm ci && npm run build
- uses: actions/upload-artifact@v7
with:
name: preview-app
path: |
.
!.git/
!node_modules/
include-hidden-files: true
if-no-files-found: error
retention-days: 7Then give the preview workflow's deploy job actions: read and name the build workflow:
permissions:
actions: read
id-token: write
pull-requests: write
deployments: write
steps:
- uses: HarperFast/deploy-action@v1
with:
target: ${{ vars.HARPER_PREVIEW_TARGET }}
app: shop
preview: true
build_workflow: preview-build.yamlThe cleanup job stays as it is. The artifact is the PR's code's output, so the action runs nothing from it. It writes only regular files into a new directory, refuses symbolic links and paths that leave that directory, and leaves .git, .env* and .npmrc at its top out of the deploy, in any case. It downloads an archive of up to 2 GiB and unpacks up to 4 GiB, 1 GiB a file. With build_workflow, args cannot set package or deployment_id, which would deploy something else.
A PR can instead publish a package from its build workflow, and the preview deploy it:
with:
target: ${{ vars.HARPER_PREVIEW_TARGET }}
app: shop
preview: true
args: package=npm:@acme/shop@pr-${{ github.event.pull_request.number }}With a package the commit is still recorded on the preview.
GitHub's workflow execution protections add a default policy that blocks pull_request_target in public repositories on github.com that have no applicable event policy. It does not apply to private or internal repositories, or to GitHub Enterprise Server. GitHub first announced enforcement for November 2, 2026, and moved it to December 2, 2026. Once enforced, a blocked run fails with an error.
To keep previews working in a public repository, allow the event for the preview workflow. harper preview setup v5.4.3 and later does this for the starter workflow it writes: it reads the repository's policies, organization and enterprise ones included, and creates Allow Harper previews (preview.yaml) when none allows the event yet. It never changes a policy: when one blocks the event, or one for the workflow is turned off, setup says what to change instead, so read its output. The policy lets every job in the file run on pull_request_target, so for a workflow you wrote, first check that no job runs a PR's code with the repository's token or secrets, then create the policy as below.
A repository admin creates it in the repository's Settings > Actions > Policies, also for a repository a personal account owns. An organization owner can instead create one in the organization's Settings > Actions > Policies that covers several repositories. Through the REST API, POST /orgs/ORG/actions/policies takes the same rules, and its conditions have to name the repositories, with repository_name, repository_id or repository_property, beside workflow_path.
examples/preview-actions-policy.json is that repository-level policy. Create it with jq and the gh CLI, replacing OWNER/REPO:
curl --fail --silent --show-error --location https://raw.githubusercontent.com/HarperFast/deploy-action/main/examples/preview-actions-policy.json | jq '{name, enforcement, conditions, rules}' | gh api --method POST repos/OWNER/REPO/actions/policies --header 'X-GitHub-Api-Version: 2026-03-10' --input -If your preview workflow has another name, change the include path in a copy of the file, and run the same jq and gh api on that copy.
Download that file, then import it from the repository's Settings > Actions > Policies page. Check the form against the steps below, and click Create.
- Go to the repository's Settings > Actions > Policies, and create a new policy.
- Name it, and set Enforcement status to Active. A new policy can start out disabled, which allows nothing.
- Under Target workflows, use Add target to add the path
.github/workflows/preview.yaml. - Turn on Restrict events, and add
pull_request_targetto Allowed events. - Click Create.
The policy's event list applies to that workflow alone, and it becomes the only list of events that can start it. So if your preview workflow has other triggers, add them too. Other workflows are not affected.
Policy insights, under the same menu, shows which runs were or would have been blocked. An enterprise or organization policy can still block the event; see Control workflow execution.
| Input | Default | Description |
|---|---|---|
target |
required | The cluster's operations API URL. The trust policy's audience has to match it. Only a loopback address may use http. |
app |
required | The application's component name. |
preview |
false |
Run the preview lifecycle instead of a normal deploy. |
path |
. |
The directory to deploy as a payload, when not deploying by reference. |
by_ref |
see description | Have the cluster fetch the commit. Defaults to true for previews and when ref is set, otherwise false. |
ref |
The commit, branch or tag to deploy by reference. Previews always deploy the PR's head commit. | |
restart |
rolling |
How a normal deploy restarts Harper, as the CLI's restart= argument. |
credential |
false |
Use the sealed git credential to fetch a private repository. |
domain |
app.<target host> |
The preview domain. |
label |
A DNS label added to the preview host, to tell apart previews of several applications on one cluster. | |
ttl |
the CLI's, 7 days | Days until the cluster expires the preview, capped by the application's maxTtlDays. |
branched_databases |
true |
Give each preview its own database branch. false shares the cluster's data. |
comment |
true |
Keep one PR comment with the preview URL. |
github_deployment |
true |
Record each preview as a GitHub Deployment. |
deployment_environment |
<app>-preview |
The GitHub environment for those Deployments. |
args |
More CLI arguments for the deploy, one key=value per line. |
|
harper_version |
>=5.4.0-alpha.1 <6 |
The npm version or range of the harper CLI. |
timeout_minutes |
30 |
The longest each CLI command may run before the action stops it and fails. |
node_version |
24 |
The Node.js version for the CLI, left on PATH for later steps. Empty uses the runner's, which must be 22.18 or later. |
github_token |
github.token |
The token for PR comments and Deployments. |
build_workflow |
For previews, a workflow file in .github/workflows that builds each PR on pull_request. The preview deploys its build of the head commit instead of deploying by reference. Needs actions: read, and no path or by_ref: true. |
|
build_artifact |
preview-app |
The artifact the build_workflow run uploads with the application directory. |
build_timeout |
20 |
The longest to wait, in minutes, for the build_workflow run to finish. |
url_path |
A path, such as /shop, added to the preview URL in the PR comment, the Deployment and the url output. For an application served under a path. |
| Output | Description |
|---|---|
outcome |
deployed, dropped (the PR is closed), kept (the PR reopened, so its preview stays), swept, or skipped (a fork, a superseded commit, or an action with nothing to do) |
url |
The preview URL, with url_path, when one deployed. |
| Permission | Needed for |
|---|---|
id-token: write |
Authenticating to the cluster. Always required. |
pull-requests: write |
Reading the PR's state and keeping the PR comment. |
deployments: write |
GitHub Deployments. |
actions: read |
With build_workflow: finding the build's run and downloading its artifact. |
Start from permissions: {} at the top of the workflow, as the examples do. A normal deploy needs only id-token: write, plus contents: read if it checks out a private repository.
- The workflow is trusted, not the PR. Preview jobs run from the default branch and never check out PR code, and the action refuses
preview: trueonpull_request. The trust policy pins the workflow file, the branch,pull_request_targetand the environment. - A build is the PR's code's output. With
build_workflow, the preview deploys only an artifact of apull_requestrun of the PR's branch in this repository, at its head commit. It runs nothing from the artifact, and leaves out what could steer the CLI, which runs in that directory. - Forks never get the identity. The
if:on each job is the gate: no token claim names the head repository. The action also skips a fork itself. - The PR's code still runs on the cluster, with the preview role's rights. That role can deploy and drop only that application's
<app>-pr-<n>previews. Withoutbranched_databases: false, a preview's writes stay in its own database branch, except in thesystemdatabase (users and roles), which cannot be branched. - A preview is the same site as what production serves from the cluster's host name. Use
__Host-cookie names and CSRF orOriginchecks there. - OIDC only. The CLI would prefer any other credential to the identity token. So the action strips
HARPER_CLI_*andCLI_TARGET_*credentials from its environment and refuses them intargetandargs. It gives the CLI an empty home directory, where noharper loginis saved, and refuses a deployed directory whose.envsets one. Previews by reference start the CLI in an empty directory. - The CLI is installed fresh, into an empty directory, with install scripts off, so a project's
.npmrchas no say in it.
- Runs on Linux and macOS runners, not Windows.
.github/workflows/preview.yamlis the workflow name the default policies pin. Another name needsworkflow=onharper preview setup.- Each application on a cluster needs its own preview slug: when two applications preview the same branch name, set
labelon one of them.
- On the new release page, type the next tag (
v1.2.3) onmain, click "Generate release notes", and tick "Publish this Action to the GitHub Marketplace". GitHub has no API for that checkbox. - Publish. The Release workflow then points
v1at the newestv1.x.yrelease, once the latest CI and E2E runs for its commit onmainhave passed. If they haven't, get them green and re-run the Release workflow. A patch to an older minor version leavesv1on the newer release, and promoting a prerelease counts as publishing it.