# CI/CD

This page says how an app is deployed from its own CI: the workflow
`durable-sdk ci init --deploy` writes, the one secret it needs, what each
job does and what to do when one is red. It teaches `durable-sdk ci init`
and the CI use of `durable-sdk deploy --json`. The last section says how
the platform releases itself, so you know the discipline behind
`<app>.durablesdk.com`.

## One command

```bash
durable-sdk ci init --deploy [--app <slug>] [--branch main]
```

writes `.github/workflows/durable.yml` with four jobs:

| Job          | When                              | After   | What                                                                                                                                                                                                                               |
| ------------ | --------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `quick`      | every pull request                | —       | `durable-sdk test --changed`: `durable-sdk index --check` as its first row when the root carries `graph-index.json` (the committed index held to the tree), then the tests the change reaches, the guards a changed path can break |
| `prove`      | every pull request and every push | —       | `durable-sdk test --pr` on a Postgres service, then the store proof                                                                                                                                                                |
| `preview`    | every pull request                | `quick` | `durable-sdk deploy --branch <the branch> --app <slug> --json`; the hostname on the pull request                                                                                                                                   |
| `production` | a push to the default branch      | `prove` | `durable-sdk deploy --prod --app <slug> --json`; the hostname and the state in the job's summary                                                                                                                                   |

The app is `--app <slug>`, else the link `.durable-sdk/app.json` in the
directory (written by `apps create` and `apps link`), else the command
is refused and says so. `--branch` is the default branch (`main` unless
said). Without `--deploy` the file carries the two test jobs alone
([Testing](https://durablesdk.com/docs/testing.md)).

A tree made by `durable-sdk init` ships this file already, the app's
name as the slug: the pipeline is there from the first commit. `durable-sdk
ci init --deploy --check` says whether the file is still what the command
renders (a step of your CI beside your other generated files); `--force`
writes it again over a file a person edited.

## The one secret

A person mints a key of scope `deploy` on the dashboard
(`/dashboard/keys`, expiry 90 days by default) and stores it as the
repository secret `DURABLE_API_KEY`. The workflow reads it into the two
deploy jobs' environment and nowhere else: never on a command line, never
in a log line (the CLI redacts every credential it knows), never in the
summary. A key never mints a key, so a leaked CI key cannot widen itself;
revoke it with `durable-sdk keys revoke <id>` and mint another.

## The preview job

On every pull request, after `quick` is green, the job deploys the tree
on the pull request's head branch and comments the hostname on the pull
request:

> Preview: `https://my-app-git-feature-x.durablesdk.com` (ready, 3f2a9c1).

One comment per pull request: the job finds its earlier comment by a
marker and updates it, so a pull request with twenty pushes has one line,
the current one. A preview runs on the app's one shared `preview`
database branch, has no scheduled tick (`request-only`) and is swept
after the tier's preview days ([Deploy](https://durablesdk.com/docs/hosting/deploy.md#previews)).

## The production job

On a push to the default branch, after `prove` is green, the job deploys
to production with the commit's short hash as the message. The platform
builds the tree (the migrations inside the build), aliases the build,
checks it and promotes it under the app's rule (`auto` by default;
`manual` leaves it `ready` for `durable-sdk deployments promote <id>`,
[Hosting](https://durablesdk.com/docs/hosting.md#a-production-deployment-staged-checked-promoted)).
The job's summary carries the app, the state and the hostname; one
production deploy runs at a time (a second push waits, never cancelled).

## `--json` and the exit grammar in CI

Both jobs run `durable-sdk deploy … --json > deployment.json`: stdout is
the final deployment row alone (or a refusal as `{ "error": { "code",
"message", "remedy", "docs" } }`), progress goes to stderr, and the exit
code is the one grammar:

| Exit | Meaning                                                      | The job                                        |
| ---- | ------------------------------------------------------------ | ---------------------------------------------- |
| 0    | `ready` or `promoted`                                        | green; the hostname printed and commented      |
| 1    | `failed` or `cancelled`; the sentence and the remedy printed | red with the sentence, the remedy and the code |
| 2    | a refused request (4xx) or a bad flag                        | red with the refusal's sentence and remedy     |
| 3    | the platform unreachable or a 5xx                            | red; re-run the job                            |

The job reads `state`, `url` and `error` off the file with `jq` and
fails with `durable-sdk deploy exited <code>: <message> · <remedy>
(<code>)`, so the reason is the first line of the annotation.

## When a job is red

- **The index is stale** (`quick` red on its first row, `index --check`):
  the committed `graph-index.json` or `workflow-index.json` is not what
  the tree walks to; run `durable-sdk index` and commit the two files. A
  deploy never waits on it: the build regenerates them.
- **The build failed** (`plane.deploy.build_failed`): `durable-sdk
deployments events <id>` prints the build's lines; fix forward and
  push.
- **The check was red** (`plane.deploy.check_failed`): the build stands
  on its own hostname (`<slug>-<short id>.durablesdk.com`) and was never
  promoted; read `/health` and the check's rows there
  (`durable-sdk deployments get <id>`), fix forward.
- **The build ran past the tier's minutes**
  (`plane.deploy.build_timeout`): the build was cancelled; a smaller
  tree, or Pro's `build_minutes`.
- **A refusal** (exit 2): the sentence names it; `durable-sdk explain
<code>` says the remedy. The usual ones in CI: a key revoked or
  expired (`plane.key.revoked`, `plane.key.expired`: mint another), no
  app named (`plane.cli.no_app`: `--app` or `apps link`), the tier's
  live previews full (`plane.deploy.previews_live`: `durable-sdk
environments rm <name>`).
- **Exit 3**: the platform was unreachable from the runner; re-run.

A red production job leaves production as it was: the previous
deployment serves until a build is checked green and promoted.

## The GitHub App

The zero-configuration path is the [GitHub App](https://durablesdk.com/docs/hosting/github.md):
connect the repository from the app's Settings page and every push
deploys with no workflow and no key in the job, a check run on the commit
and a preview per branch and pull request; that page says when to pick it
over the workflow above, and how the two stand together.

## How the platform itself is released

The platform keeps the discipline it asks of you, so its own release
never serves a broken build:

1. **Proved.** A change is a pull request; its CI runs the platform's
   typecheck, lint, tests and build.
2. **Staged.** A merge lands on the production branch and the platform's
   builder makes the production deployment (the migrations inside the
   build, under a credential that never leaves the builder). The
   deployment is ready on its own hostname while the previous one still
   serves `durablesdk.com`.
3. **Checked.** A release gate reads the staged deployment: `/health`,
   the API's document, the operator API under a read bearer, the docs
   index, the landing page; a red row leaves the previous deployment
   serving and the new one on its hostname for a person to read.
4. **Promoted.** A green gate re-points `durablesdk.com` at the new
   deployment, no rebuild; `/health` is read once more.
5. **Rolled back** in one command when a person asks: the hostnames
   re-pointed at the previous deployment at once, `/health` read.

A hosted app is released the same way under its own rule
([Hosting](https://durablesdk.com/docs/hosting.md#a-production-deployment-staged-checked-promoted)):
the hostname your CI comments and the state it prints are those steps
seen from the outside.

---

Prompt for an agent:

```text
Read https://durablesdk.com/docs/hosting/ci-cd.md, then give a Durable app a CI pipeline that deploys a preview per pull request and production behind its tests; run durable-sdk doctor --host before and after; a refused line is a refusal id: durable-sdk explain <id>
```
