Skip to content
landfall

Lifecycle hooks

Hooks run npm scripts at fixed points of a deploy, a rollback and a removal. They live in package.json, next to the scripts they run. landfall runs them with the project's package manager (from the lockfile): npm, pnpm, yarn or bun.

{
  "scripts": {
    "lint": "eslint .",
    "typecheck": "tsc --noEmit",
    "test": "vitest run",
    "db:migrate": "prisma migrate deploy",
    "smoke": "node scripts/smoke.mjs",
    "notify": "node scripts/notify.mjs",
    "backup": "node scripts/backup.mjs"
  },
  "landfall": {
    "hooks": {
      "prebuild": ["lint", "typecheck"],
      "predeploy": "test",
      "release": "db:migrate",
      "verify": "smoke",
      "postdeploy": "notify",
      "onfailure": "notify",
      "predestroy": "backup"
    }
  }
}

A hook is one script name or a list of them. A list runs in order and stops at the first script that fails. On the server, a hook's scripts share one container. landfall checks the section before it runs anything. An unknown hook or a script scripts does not have is an error. The same section can hold the project settings too.

The hooks

hookrunswherewhen it fails
prebuildbefore the buildthis machinethe deploy stops
postbuildafter the buildthis machinea warning
predeploybefore anything is sent to the serverthis machinethe deploy stops
releaseonce, after the new release is built, before it startsa one-off container of the new releasethe deploy stops; the previous release keeps running
verifyafter the new release startedthe running containerthe previous release comes back
postdeployafter verifythe running containera warning
onfailurewhen the deploy fails at any stepthis machinea warning
prerollbackbefore lf rollback switchesthe running (old) release's containerthe rollback stops
postrollbackafter lf rollback switchedthe restored release's containera warning
predestroybefore lf sites rm removes anythingthe running containernothing is removed

The pre hooks, release and verify stop what they guard. The post hooks and onfailure only report. onfailure never runs again because of its own failure.

The lifecycle

lf deploy
  prebuild ─► build ─► postbuild ─► predeploy           this machine
     │
     ▼
  upload ─► build on the server ─► release ─► start ─► verify ─► postdeploy   server
                                     │                   │
                                     └─ fails: stop      └─ fails: previous release back
  any failure ─► onfailure                              this machine

lf rollback
  prerollback (old release) ─► switch ─► postrollback (restored release)

lf sites rm
  predestroy ─► stop and delete

Where server hooks run

release, verify, postdeploy, prerollback, postrollback and predestroy run on the server, in the site's container. The container needs the scripts and the package manager: an app image built from the project has them. When the image lacks the package manager, landfall uses npm there.

The service is the one that serves the site's domains, else the one landfall builds. A compose site with several services can name it:

{ "landfall": { "service": "web", "hooks": { "release": "db:migrate" } } }

A static site runs in nginx, without package.json. It can use prebuild, postbuild, predeploy and onfailure only.

release runs in a one-off container (docker compose run --rm) of the new release. Traefik never routes traffic to it, and it runs once however many replicas the service has. The services it depends on, like a database, start first.

Each release keeps its server hooks in the site's site.json. So lf rollback and lf sites rm run the hooks of the releases on the server, from any folder.

Environment variables

Every hook gets these:

variablevalue
LANDFALL_HOOKthe hook's name
LANDFALL_DEPLOYMENT_IDthe run's id, as lf history shows it
LANDFALL_PROJECTthe site's name
LANDFALL_SERVERthe server's name (hooks on this machine)
LANDFALL_RELEASEthe release id (server hooks)
LANDFALL_VERSIONthe git commit, else the release id
LANDFALL_PREVIOUS_VERSIONthe version that ran before (server hooks of a redeploy and a rollback)
LANDFALL_URLthe app inside its container, like http://127.0.0.1:3000 (verify, postdeploy, postrollback)
LANDFALL_FAILED_STEPwhat failed: the hook's name, or build, deploy (onfailure)
LANDFALL_FAILED_HOOKthe hook that failed, if one did (onfailure)
LANDFALL_ERRORthe error message (onfailure)

Server hooks also get the service's own environment, like the app: release reaches the database the app uses.

Skipping hooks

# deploy without the verify hook
lf deploy --skip-hook verify
 
# deploy without any hook
lf deploy --skip-hooks
 
# roll back without prerollback
lf rollback --skip-hook prerollback

--skip-hook takes any hook and can be given more than once. A skipped hook is said in the output and in the journal (lf history).

Output and failures

Hook output goes to the deploy log, on stderr. A failing hook is named: hook verify failed: npm run smoke.

The deploy has no second copy of the site running: the new release replaces the old one in place. So verify runs right after the start, and a failure brings the previous release back. A new site has no previous release: when its verify fails, it stays as it is, and lf undo removes it.

release runs before the new release starts, and the old one still serves traffic meanwhile. A failing verify does not undo what release did. Write migrations the previous release can run with: add columns before the code uses them, drop them a release later.

Examples

// scripts/smoke.mjs: verify that the app answers
const res = await fetch(`${process.env.LANDFALL_URL}/health`);
if (!res.ok) throw new Error(`health check: ${res.status}`);
// scripts/notify.mjs: postdeploy and onfailure
const text = process.env.LANDFALL_FAILED_STEP
  ? `${process.env.LANDFALL_PROJECT}: deploy failed at ${process.env.LANDFALL_FAILED_STEP}`
  : `${process.env.LANDFALL_PROJECT}: ${process.env.LANDFALL_VERSION} is live`;
await fetch(process.env.CHAT_WEBHOOK, { method: "POST", body: JSON.stringify({ text }) });