From a push to production
Set up dev, staging and production environments, get a preview for every pull request, let production take snapshots only from a protected branch, and roll back a release that was wrong.
This tutorial builds the path a change takes from a pull request to production, using what Wardn does today. A pull request gets its own running copy. A merge reaches dev. Moving the same commit to staging and then to production is a deliberate step you take in git. Production accepts snapshots only from one branch, so a commit nobody released cannot reach it by accident. At the end you ship a release that is wrong and go back.
It takes about an hour and ends with:
- three environments,
dev,stagingandproduction, each with a cluster and a deployment of the same app; - a preview deployment for every open pull request;
- production guarded by the protected-branch rule, with a blocked deploy you have seen;
- a restore of the previous good snapshot.
You need:
- A running Wardn and three connected clusters named
dev,stagingandprod(Connect a cluster). For a trial, make three kind clusters and expose the gateway ofwardn-tinyto the two that do not run it: see operators in other clusters. - A GitHub repository you can push to, for example
example/shop, with the Wardn GitHub App installed on it (Connect a source). Previews need the App's pull request webhooks. - The Owner role, or the permissions each step names. The API tabs also need
WARDN_API_URL,WARDN_TOKENand this helper, as in Connect a cluster:
api() { curl -fsS -H "Authorization: Bearer $WARDN_TOKEN" -H 'Content-Type: application/json' "$@"; }What you will build
This tutorial keeps it simple: each environment follows its own branch, and you move a change forward by moving the branch. Wardn can also carry one snapshot along a pipeline instead, with a soak, tests and a sign-off in between; the recipe A QA stage with sign-off shows that route.
| Environment | Class | Cluster | Deployment | Follows branch | Moves forward when |
|---|---|---|---|---|---|
dev | development | dev | web-dev | main | a pull request is merged |
staging | staging | staging | web-staging | staging | you fast-forward staging to main |
production | production | prod | web-production | release | you fast-forward release to staging |
Prepare a namespace on each cluster
Wardn applies every deployment as the service account wardn-deployer in the deployment's namespace, so that namespace and account must exist on every cluster the deployment reaches. Make them on all three (why):
for ctx in kind-dev kind-staging kind-prod; do
kubectl --context "$ctx" create namespace shop
kubectl --context "$ctx" -n shop create serviceaccount wardn-deployer
kubectl --context "$ctx" -n shop create rolebinding wardn-deployer \
--clusterrole=admin --serviceaccount=shop:wardn-deployer
donePull request previews need no preparation: the operator makes and removes their namespaces itself.
Put the app in git, on three branches
Use the three manifests from Deploy your first app: a ConfigMap, a Deployment and a Service in deploy/web. Commit them to main, then make the two branches that staging and production follow:
git push origin main
git push origin main:staging main:releaseEach environment follows one branch, so the three branches start level.
Create the three environments
An environment is a named group of clusters. Its class is what Wardn reads to know that production is production, so spell it exactly. Environments has every field.
For each environment, open Environments and choose New environment. Give it a Name and Class, then choose Create environment:
| Name | Class |
|---|---|
dev | development |
staging | staging |
production | production |
Back on Environments, the section Clusters not in an environment lists your three clusters. Use Assign to an environment on each to put dev in dev, staging in staging and prod in production.
Tell production which branch it takes
Every environment whose class is production is covered by the built-in guardrail Protected branch for production. It lets a snapshot through only if it came from a branch the git host reported as protected. Do this before any deployment exists, or production would hold its first snapshot: GitHub has not been told that release is protected.
Name the branch on the environment. That also works for git hosts that cannot report protection:
api -X PUT "$WARDN_API_URL/api/v1/environments/$PROD_ENV/branch-guard" \
-d '{"enforced": true, "protectedBranch": "release"}'
api "$WARDN_API_URL/api/v1/environments/$PROD_ENV/branch-guard"The second call answers "isDefault": false: this environment now has a rule of its own, and the built-in one leaves it out. It needs environment:manage, and the web app has no field for it yet. On GitHub, also protect release in the repository's branch settings, so that only reviewed changes can move it.
Create the app and the three deployments
An app says where manifests come from. A deployment puts the app on an environment and says which branch it follows. One app, three deployments.
Open /apps/new. Under Source choose A new repository and Git repository, paste https://github.com/example/shop, set Path to deploy/web and Credentials to your installation's github-app:<id>. Under App set Name to web and Rendered with to Plain manifests. Choose Create app.
On the app's page choose New deployment three times:
| Name | Runs on | Environment | Namespace | Snapshots arrive | Branch |
|---|---|---|---|---|---|
web-dev | Environment | dev | shop | Track a branch | main |
web-staging | Environment | staging | shop | Track a branch | staging |
web-production | Environment | production | shop | Track a branch | release |
Leave Prune on Organization default and Observe only off.
Within a minute each deployment renders its branch and shows Healthy. A deployment on an environment is applied to every cluster in it, here one. Check each cluster:
for ctx in kind-dev kind-staging kind-prod; do
kubectl --context "$ctx" -n shop get deploy web
doneDeploy your first app explains what Wardn does between the push and the apply.
Turn on previews, and open a pull request
A preview is a copy of a deployment for one pull request: its own namespace, its own address if you give it a domain, and a status on the pull request. Turn them on for web-dev, the deployment that follows main.
Open Deployments, then web-dev, then Settings. Under Pull request previews switch Previews on, keep At most at 5 and Removed after at 72 hours, leave Domain empty, and choose Save delivery settings.
Now change something on a branch and open a pull request. Make the greeting louder:
data:
GREETING: hello from a previewgit switch -c louder-greeting
git commit -am "Louder greeting"
git push -u origin louder-greeting
gh pr create --fillWithin seconds the pull request shows a wardn/preview status, and a View deployment button once the preview is live (GitHub). Under web-dev › Settings, the table below the form lists the preview with the pull request, its branch and when it expires. On the dev cluster:
kubectl --context kind-dev get ns | grep shop-pr
kubectl --context kind-dev -n shop-pr-1 get cm web-config -o jsonpath='{.data.GREETING}'The namespace is shop-pr-<number>, and it holds your branch's version of the app. Nothing else changed: dev, staging and production still run the old greeting. Without a Domain the preview has no address; the monorepo recipe adds one.
Merge the pull request. The preview is removed, and web-dev renders the new main.
Move the change forward
Dev already has it. Fast-forward the next branch to reach staging, and the one after for production:
git switch main && git pull
git push origin main:staging
# check staging, then
git push origin main:releaseEach push becomes a new snapshot of the deployment that follows that branch. Open Deployments › web-staging › Snapshots: the newest snapshot has the same short content id as the one on web-dev, because it holds the same objects. Compare snapshots shows the diff between this snapshot and the one before.
api "$WARDN_API_URL/api/v1/deployments/$PROD_DEP/state" \
| jq '{sync, health, snapshot: .snapshotContentId, branch: .provenance.branch, commit: .provenance.commit}'The snapshot's provenance says "branch": "release". That is what the production rule judged.
See production refuse a commit nobody released
Merge one more change to main and do not release it. Then point production at main, as if someone mistyped the branch:
- Open Deployments ›
web-production› Settings. - Change Branch from
releasetomainand choose Save.
Within a minute Wardn renders main for production. The snapshot is stored, but it is not applied. Production keeps running what it ran.
The decision names the rule and the branch it wants, in the form Guardrail "Branch rule for production" holds this snapshot: Snapshot from release. (This is the environment's own rule from the previous step; without it the built-in Protected branch for production would speak.) Find the decision in Audit › Activity: search for branch_blocked, or
api "$WARDN_API_URL/api/v1/activity?action=guardrail.branch_blocked&limit=5"Set Branch back to release and save. A person with release:bypass can let one such snapshot through, once, with a reason, for a hotfix that cannot wait for release (One-time bypass). It is a duty that the Admin role does not hold, so nobody has it by default except the Owner.
The rule is checked on every push, on promotion and when a held snapshot is released. It is not checked on a restore, which is the way back from a bad deploy.
Ship a wrong release and roll it back
A change can render, apply and be healthy, and still be the wrong change. Make one:
data:
GREETING: hello from the wrong releaseCommit it to main, then git push origin main:staging main:release. Production shows the wrong greeting:
kubectl --context kind-prod -n shop get cm web-config -o jsonpath='{.data.GREETING}'Go back to the snapshot before it:
Open Deployments › web-production › Snapshots, and open the snapshot that ran before the newest one. Choose Back to an earlier snapshot. The dialog shows What changes (one app), Where it runs and Before you restore, the preflight: the cluster still exists, every image is still in its registry, secrets still match. Write a Why restore such as "wrong greeting in the last release", then choose Restore 1 app on production. The toast says "Restore queued".
Production runs the earlier greeting again, applied through the same path as any deploy. The snapshot a restore makes is marked Rolled back in the history, and Activity has deployment.restore with your reason.
A restore does not stop the branch
release still holds the wrong commit. The next push to it makes a new snapshot and moves production forward again. Fix the branch now: revert the commit on main, then move it forward as before. If you need time, set the deployment to Observe only first.
Clean up
Delete the three deployments from their Settings tabs (Delete deployment), the app from its page, and the three environments once they hold no clusters, then the namespaces:
for ctx in kind-dev kind-staging kind-prod; do kubectl --context "$ctx" delete namespace shop; doneWhat you built, and what is missing
| Part | Where it lives |
|---|---|
| A deployment per environment | Environments, Deployment settings |
| A running copy of every pull request | Pull request previews |
| Production takes one branch | Guardrails |
| The way back | Restore and rollback |
Here the branch you move is the gate, so protect release on your git host and restrict who may push to it. To move one exact snapshot through the environments instead, with a soak, tests and a sign-off in between, put the three environments in a pipeline: see A QA stage with sign-off and Stage rules.
Next
Recover from a bad change
A held snapshot, drift and a restore, start to finish.
A QA stage with sign-off
Carry one snapshot dev to QA to prod, with a sign-off in between.
Protect production
Add a freeze and an exception to the production environment.
Previews for a monorepo
Give previews an address and run several services.
Related: Sync and arrival, Snapshots, Access model.
Cookbook
Tutorials that take you from nothing to a working setup, and recipes you can copy for common ones.
Recover from a bad change
Meet the three ways a deployment goes wrong, a render that Wardn holds, a change made by hand in the cluster, and a release that applied and should not have, and fix each with the right tool.