Pull request previews

Run every open pull request as its own deployment, reach it at its own address, and have it removed when the pull request closes or expires.

A preview lets a reviewer see a change running before it is merged. With previews on for a deployment, Wardn makes a copy of that deployment for each open pull request, renders the pull request's head commit into it, applies it in a namespace of its own, reports the result on the pull request, and removes everything when the pull request closes. Every preview is a real deployment, so it is rendered, held, applied and observed through the same path as any other. Deployments explains what a deployment is.

To give a pull request a deployment you choose, instead of a copy of a parent for every pull request, link it to a deployment. Previews keep working as they are and can be used beside it.

Previews need pull request webhooks, so they need the GitHub App on the repository. A repository connected as plain git gets no previews.

Turn previews on

Open the deployment, then Settings, and find Pull request previews ("A deployment of each open pull request, removed when it closes").

FieldMeaningDefault
PreviewsDeploy each pull request as a previewoff
At mostPreviews that may exist at once, 1 to 505
Removed afterHours without a push before a preview is removed, 1 to 72072
DomainA wildcard such as *.preview.example.dev; empty for previews with no addressempty
ForksThe label that allows a preview of a pull request from a forkwardn:preview

Choose Save delivery settings. Below the form, a table lists the live previews: Pull request, Branch, Runs (the commit), Removed (when it expires, or Being removed), and Open when the preview has an address. The commit status and GitHub deployment switches are not in the web app; they are on by default and can be changed with the API.

Prop

Type

Turning previews off leaves existing previews alone until their pull request closes or they expire.

What happens to a pull request

Pull request eventWhat Wardn does
Opened, pushed to (synchronize) or reopenedMakes the preview if there is none and the cap allows, renders the head commit and applies it. Each push moves the expiry forward.
ClosedRemoves the preview.
Labelled or unlabelled (forks only)Allows or stops the preview of a fork's pull request.

A repository that has several apps in the deployment gets one preview per pull request, for the first app whose repository the pull request is on. The preview holds every app of the deployment: the one in the pull request at its head commit, and the others at the commits the parent deployment is running (or its newest snapshot), or at the head of their tracked branch if the parent has none.

The preview deployment

A preview is created from its parent, and it is listed with the other deployments (filter by previews, or ?preview=only in the API; previewOf names the parent).

  • Name and namespace: <parent name>-pr-<number> and <parent namespace>-pr-<number>. A name that would be too long, or has characters Kubernetes does not allow, is cleaned and given a short hash, so two parents never collide.
  • Same apps, same values, same place as the parent: the same cluster, or the same environment, in which case the preview is applied to each cluster of it.
  • Arrival is on request and prune is on, so only the pull request's own renders reach it, and removal empties the namespace.
  • Owners are the parent's first owner.

Reaching the preview

With a Domain set, each preview gets the address pr-<number>.<deployment>.<domain suffix>, for example pr-42.web.preview.example.dev. Wardn only writes the host into your routes: you provide the wildcard DNS record and the ingress controller or gateway that serve it, with a certificate if you need TLS.

It writes the host into objects that opt in. Add the annotation wardn.dev/preview-host: "true" to an Ingress or an HTTPRoute:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: web
  annotations:
    wardn.dev/preview-host: "true"
spec:
  rules:
    - host: web.example.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service: { name: web, port: { number: 80 } }

In a preview, an Ingress gets the preview host on every rule and every TLS entry, and an HTTPRoute gets it as its only hostname. The annotation stays on the object. Wardn does not guess at charts: an object without the annotation is not touched, and if none of the app's routes carries it, the render records a warning that the preview has no route to its host. Without a domain, previews run but have no address.

Addresses

A deployment of an app that can be reached gets an address on its cluster. Wardn works the address out when it renders the deployment, makes the Ingress that sends it to the app's Service, and shows the link wherever the deployment appears, https when it has a certificate.

Two places say how:

  • The app says whether it can be reached (Can get a deployment for a pull request under the app's settings), the public port of its Service, and the path it takes traffic on. With no port, Wardn uses the Service port named http, else the first, and it skips headless and ExternalName Services.
  • The cluster says how its deployments are addressed, under Cluster settings › Addresses: the address mode, the domain and the certificates.

A deployment's address is <app>-<deployment>.<domain>. If another deployment already has that name, Wardn adds a short tag to the name.

Address modes

Give a wildcard, such as *.prod-eu-1.example.com, and add a DNS record that sends the whole wildcard to the cluster's ingress address. A deployment pay of the app shop is then at shop-pay.prod-eu-1.example.com.

Check DNS looks up a name under the wildcard and compares it with the address the cluster's operator found. It says DNS points at 203.0.113.9, or what is wrong: the wildcard does not resolve, or it resolves somewhere else.

*.prod-eu-1.example.com.   300  IN  A      203.0.113.9

Use a CNAME instead when the load balancer has a host name.

A cluster that has no public address, such as a kind cluster without a load balancer, shows No public address, and its deployments have no link rather than a broken one. The note on the deployment says why.

HTTPS

  • Let's Encrypt asks cert-manager for a certificate. Wardn makes the ClusterIssuer wardn-letsencrypt, which solves HTTP-01 challenges through the cluster's ingress class, and puts cert-manager.io/cluster-issuer on the Ingress. A cluster without cert-manager gives its deployments no link, and says so, instead of a link with a broken certificate.
  • Your certificate serves a TLS Secret you name. It has to exist in the deployment's namespace, so ship it with the app's manifests or have your secret tooling copy it there.
  • Off serves plain HTTP.

Is the cluster ready

Cluster settings › Addresses shows what the cluster's operator found: the ingress controllers (IngressClasses) and the load balancer address in front of each, whether cert-manager is installed, and whether the Gateway API is. Check again reads the cluster again.

Install cert-manager installs one pinned release of cert-manager from the project's own manifest, after checking the manifest's digest, and makes the ClusterIssuer. It is there only while the cluster lets Wardn prepare namespaces, and installing is recorded in Activity. The operator also needs the rights for it, which wardn cluster connect leaves out unless you pass --allow-cert-manager-install (the Helm chart's allowCertManagerInstall), because cert-manager's manifest creates cluster roles and webhooks. Without them, install cert-manager yourself and press Check again.

What a preview shows on GitHub

Each switch is on by default and independent:

SettingWhere it showsBehaviour
githubStatusA commit status named wardn/preview on the head commitpending while building, success when live, failure if it fails or if no preview was made because the cap was reached. Its link goes to the preview's page in Wardn when the server has WARDN_PUBLIC_URL set, otherwise to the preview's address.
githubDeploymentA GitHub deployment per commit, in the transient environment preview/pr-<number>in_progress while building, success when live (the preview address is its environment URL, which gives the pull request a View deployment button, and the Wardn page is its log URL), failure if it fails, and inactive when the preview is removed.
githubCommentOne comment on the pull request, found again by a hidden marker and edited in placeBuilding, live, failed (with the reason and a log link) or removed, with the commit, the preview link, the Wardn link and when it last changed. Two quick pushes edit the same comment.

The deployment status needs the GitHub App permission Deployments: read and write. If the installation has not accepted it, Wardn logs that once and carries on with the commit status. With githubDeployment off, Wardn never calls the Deployments API. The comment needs Pull requests: read and write; without it Wardn logs the refusal once and carries on. A server error or rate limit from GitHub is retried with backoff and never makes a second comment. With githubComment off, Wardn never calls the comments API. In the preview list, url is the address and detailsUrl the page in Wardn.

Limits, expiry and removal

  • The cap. At maxPreviews, a new pull request gets no preview, and its commit status says No preview: this deployment already has N, its limit. Close or wait for an old one.
  • Expiry. ttlHours counts from the last push. An expired preview is removed, as if its pull request had closed.
  • Removal. Wardn applies an empty snapshot with pruning, which deletes everything the preview created (the prune limit does not hold this one), and then deletes the preview. A removal that failed is tried again after one, five and fifteen minutes and then every hour. Reopening a pull request whose preview is still being removed has to wait until that finishes.

Preview namespaces

The namespace <namespace>-pr-<number> is made by the cluster's operator when it first applies the preview, and removed with it.

  • Creating. If it does not exist, the operator creates it with the labels app.kubernetes.io/managed-by=wardn, wardn.dev/deployment=<deployment id> and wardn.dev/preview=true, then a wardn-deployer service account and a RoleBinding named wardn-preview-deployer to the ClusterRole of the same name. That role gives write access to the usual namespaced workload, config, networking and RBAC objects (like the built-in admin, without pod exec or port-forward) and nothing cluster-scoped.
  • A namespace you made. If it already exists, the operator uses it as it is and changes nothing, so you can create preview namespaces yourself with your own service account and RBAC.
  • Removing. After the empty snapshot has pruned everything, the operator deletes the namespace, but only if it carries all three labels with this deployment's id. A namespace someone else made, or that belongs to another deployment, is left in place.

wardn cluster connect gives the operator what this needs: create and delete on namespaces, create on service accounts and role bindings, and bind on the ClusterRole wardn-preview-deployer only. An operator limited with --namespaces has none of this and makes no previews. If you installed the operator some other way, add those rules before turning previews on.

Pull requests from forks

A pull request from a fork would run its author's manifests in your cluster, so a fork gets a preview only while a maintainer's label allows it. Create the label in the repository (the default is wardn:preview) and put it on the pull request.

  • Opening a fork's pull request never starts a preview, whoever opens it. A new preview starts when someone labels the pull request or reopens it with the label on.
  • Removing the label removes the preview.
  • The preview records who labelled it (labeledBy), and the label is recorded in Activity.

The label is a decision to trust that code. A fork's manifests are rendered in the same sandbox as any other and applied with the deployment's own service account, so review the pull request before you label it. Keep that service account's role narrow.

Next

Related: Connect a source, Connect a cluster, Apps.

On this page