Previews for a monorepo
Run every service of a monorepo together as one preview per pull request, reachable at its own address, and know what Wardn does not filter.
The situation
One repository holds web and api. A reviewer should open a pull request and see both running together at one address, whichever service the change touched. When the pull request closes, everything should go away.
Two facts about previews shape the setup:
- A preview is a copy of a deployment, not of a directory. Wardn does not look at which files a pull request changed. Every deployment with previews on, whose app reads the repository, gets a preview for every pull request on it.
- Every preview reports on GitHub as the same commit status,
wardn/preview, and the same GitHub environment,preview/pr-<number>. Two preview deployments on one repository overwrite each other's status, so a pull request would show whichever finished last.
So give the monorepo one preview deployment that runs every service: a single Kustomize app that includes them all. That makes one preview, one status, one namespace where the services reach each other by name, and one address.
If your services are independent and you want a preview of each, make one deployment per service with previews on, and switch githubStatus off on all but one of them. Each costs a preview per pull request, since nothing filters by changed path.
Previews need the GitHub App on the repository and an operator that was not limited with --namespaces, because the operator makes and removes each preview's namespace.
Repository layout
Settings
The manifests
Each service directory is a small Kustomization of its own, so the preview can include it as a base:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment.yaml
- service.yamlservices/api/deploy/kustomization.yaml is the same. The preview directory assembles them and adds one route:
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../services/web/deploy
- ../../services/api/deploy
- ingress.yamlapiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: shop
annotations:
wardn.dev/preview-host: "true"
spec:
ingressClassName: nginx
rules:
- host: shop.example.com
http:
paths:
- path: /api
pathType: Prefix
backend:
service: { name: api, port: { number: 8080 } }
- path: /
pathType: Prefix
backend:
service: { name: web, port: { number: 8080 } }The annotation wardn.dev/preview-host: "true" is what makes Wardn write the preview's host into the object. Without it the preview runs but has no route. In a preview the host on every rule becomes the preview address; outside a preview the host stays shop.example.com. Neither the services nor the kustomizations name a namespace.
A preview runs the images the pull request's manifests name. A pull request that changes service code only shows it if the manifests name an image built from that code, so have your CI push an image per commit and write its tag into the manifest on the pull request branch. Wardn pins each tag to a digest when it renders, and the registry must be readable without a login.
The app and the parent deployment
The parent is a deployment that follows main and is where the preview settings live. Its previews copy it.
Open /apps/new. Under Source choose A new repository, Git repository, the https:// URL, Path deploy/preview and Credentials github-app:<id>. Under App set Name shop-preview and Rendered with Kustomize. On the app's page choose New deployment: Name shop-preview, Runs on Environment and the environment dev, Namespace shop-preview, Snapshots arrive Track a branch, Branch main.
Open the deployment's Settings. Under Pull request previews switch Previews on, set At most to 10, Removed after to 48 hours and Domain to *.preview.example.com. Choose Save delivery settings.
The parent's namespace, shop-preview, needs the wardn-deployer service account and a role, as for any deployment (Connect a cluster). A preview's namespace does not: the operator makes shop-preview-pr-<number> with its own account and removes it with the preview.
The address
Wardn only writes the host. You provide the rest: a wildcard DNS record for *.preview.example.com pointing at your ingress controller, and a certificate for the wildcard if you serve TLS. The address of a preview is pr-<number>.<deployment>.<domain suffix>, here pr-42.shop-preview.preview.example.com.
Check that it worked
Open a pull request on the repository. Within seconds:
- GitHub shows a
wardn/previewstatus, and once the preview is live a View deployment button that opens the address. - Under the deployment's Settings, the table of previews lists the Pull request, its Branch, the commit it Runs and when it is Removed.
kubectl -n shop-preview-pr-42 get pods,svc,ingress
curl -fsS https://pr-42.shop-preview.preview.example.com/
api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/previews" | jq '.items[] | {prNumber, headRef, url, expiresAt}'Push to the pull request: the preview renders the new head and the expiry moves forward. Close it: Wardn applies an empty snapshot with pruning, then deletes the namespace. At the cap of 10, a new pull request gets no preview and its commit status says so.
A pull request from a fork gets no preview until a maintainer adds the label wardn:preview. The label is a decision to trust that code (Pull requests from forks).
Next
Pull request previews
Caps, expiry, removal, namespaces and forks.
GitHub
The App's permissions and what shows on the pull request.
Kustomize
Bases, overlays and what the sandbox refuses.
Related: Connect a source, Deployment settings, From a push to production.
Kustomize overlays per environment
Keep a Kustomize base and one overlay per environment in git, and give each overlay its own app and deployment.
One app on every cluster in an environment
Deploy a cluster add-on once, to an environment that selects its clusters by label, and have it follow clusters as they come and go.