# Bootstrapping cofactor/deploy/

1. Read how the system is really deployed: the infrastructure-as-code, the CI workflows and the service entrypoints. Do not guess from folder names.
2. Make one target per independently deployed unit. Two things that always ship together are one target; two things that can ship apart are two.
3. Write `system.yml`:

   ```yaml
   environments:
     dev:
       policy: auto              # auto, or approval (a human approves each plan)
       requireChecks: [CI]       # exact check-run names that must pass on a commit
   targets:
     - id: db                    # lowercase letters, digits, dashes
       owns: ['packages/db/**']  # globs of the paths this target is built from
       confirmTimeoutMinutes: 30 # how long to wait for the status runbook to confirm
       forwardOnly: true         # schema targets only: never rolled back, only forward
       migrations: 'packages/db/migrations/*.sql'   # targets owning SQL migrations only
       runbooks:
         dev: { deploy: deploy-db-dev, status: status-db-dev }   # per stage
   ```

   The `runbooks.<stage>.deploy` and `.status` values are runbook names. Each is defined as a repo file at `cofactor/runbooks/<slug>.yml`; see `docs/features/runbooks.md` for the file format.

4. Write `contracts/<id>.yml` for every target, one file each:

   ```yaml
   provides: ['route:POST /widgets@1', 'column:widgets.name@1']
   requires: ['db:column:widgets.name@1']
   ```

   A symbol is `kind:name@n`. A requirement is `<target>:<symbol>`. Symbols are lowercase kind, any name, and a version `n` of 1 or more.

5. Write two runbooks per target per stage (files in `cofactor/runbooks/`, named in `system.yml`). The deploy runbook starts the deploy and exits; it never waits for the rollout. The status runbook reports what is live: it writes this JSON to the file named by `$COFACTOR_RESULT_FILE`:

   ```json
   {"version":"<sha>","state":"steady"}
   ```

   `state` is `steady`, `rolling` or `failed`. The status runbook must NOT put secrets, tokens or hostnames in that file.

6. Run `cofactor deploy graph validate` and fix every issue it prints.
