# Deploys

**`deploy` redeploys what is already hosted; `push` ships your new
commits.** A deploy builds the project's hosted `main` and ships it to one
environment: web app to hosting, functions to Cloud Functions, pending
migrations to the environment's database. Commits that exist only in your
checkout are not in it: run `spaceboy push` (preflight, merge into `main`,
then the same deploy). Run from a linked checkout, `deploy` warns when the
folder has commits or uncommitted changes the hosted copy lacks (and adds
`unshippedLocalWork` to `--json`), then deploys anyway.

```bash
spaceboy deploy --json                                 # development; waits (default 30 min)
spaceboy deploy --target production --json
spaceboy deploy --no-wait --json
spaceboy deploys --project prj_... --json              # newest first
spaceboy deploy-cancel --project prj_... --target development
```

`--target` defaults to `development`. Inside a folder created by
`spaceboy init`, `--project` is optional. Use `deploy` to redeploy after a
secret or setting change, or to publish to production what `push` already
merged.

A succeeded deploy prints where it answers; under `--json` the same values
are the `endpoints` field:

```
App:        https://<site>.dev-sites.spaceboy.ai
GraphQL:    https://us-central1-<project>.cloudfunctions.net/<prefix>__graphql__request__api
Functions:  https://us-central1-<project>.cloudfunctions.net/<prefix>__<export name>
```

## What gets built

- **development** builds `main`, into the caller's own development
  environment (created on first deploy).
- **production** builds `main`. If the workspace holds finished work that
  is not on `main` yet, `deploy --target production` first opens a pull
  request, has a review agent check it (with one automatic fix round),
  merges it, and then deploys; the review timeline prints to stderr.
  `spaceboy reviews --project prj_...` lists these releases.

How the site is cached: pages revalidate on every visit; the files your
build fingerprinted (Vite's `assets/<name>-<hash>.js`, Astro's `_astro/`,
Next's `_next/static/`; read from the bundler's manifest or from what the
build emitted) are cached for a year, since a change gets a new name. Every
file from `web/app/public/` (logos under `brand/` or `assets/`, `samples/`,
icons) keeps its name, whatever the name looks like, and is cached for 5
minutes, then revalidated cheaply (`ETag`, `304`), so a changed logo shows
within minutes of the deploy. Sites deployed before this rule get the
5-minute cache for every file until their next deploy. Per-page link previews:
[Functions](https://spaceboy.ai/docs/agents/functions-and-http-endpoints.md).

## Statuses

| `status` | Meaning |
| --- | --- |
| `QUEUED` | Accepted; waiting to start |
| `PROVISIONING_INFRA` | Creating the environment's hosting, database, sign-in secrets, ... (first deploy, or a new capability) |
| `RUNNING` | The build job is working; `stage` says where |
| `SUCCEEDED` | Live; `resultUrl` is the app's URL |
| `FAILED` | Stopped; `failureReason` says why. The environment keeps its last good version |
| `CANCELED` | Canceled with `deploy-cancel`; the last good version stays |

`stage` while `RUNNING`: `CLONING`, `BUILDING`, `MIGRATING`,
`DEPLOYING_BACKEND`, `DEPLOYING_WEB`, `FINALIZING`. One deploy runs per
environment at a time; a second request returns the active one.

`deploy` exits 0 on `SUCCEEDED` and 1 on `FAILED` (the reason is the
message; integration failures add the dashboard link to fix them).

## Waiting, and resuming a wait

`push` and `deploy` wait for the deploy's own outcome. A status read that
fails (network error, API HTTP 5xx or 429) is retried with backoff for the
rest of `--timeout` and printed on stderr; it never ends the command, because
the deploy carries on on the server. The exit code is the deploy's result.

If the wait ends without a result, `error.code` says why and `error.action`
is the command that resumes it: `DEPLOY_WAIT_TIMEOUT` (still running when
`--timeout` passed) or `DEPLOY_STATUS_UNKNOWN` (the API could not be read
until the end). To pick up a deploy after your process stopped:

```bash
spaceboy deploys --deploy edp_... --wait --json   # exit 0 only on SUCCEEDED
```

With `--json` it prints the deploy when it succeeds, and otherwise one error
value with `code` `DEPLOY_FAILED` or `DEPLOY_CANCELED` and the deploy as
`details`.

## The frontend-only fast path

When nothing under `firebase/`, `package.json`, `package-lock.json` or
`repobot.deploy.json` changed since the environment's last successful
deploy, **and** the environment's settings and secrets are unchanged, the
deploy skips migrations and the functions and ships only the web app.

## Which functions a deploy rebuilds

Each function has a fingerprint: its own code and the modules it loads, the
dependency lockfile, every non-code file the functions upload carries, and
the platform's settings for the environment. A deploy rebuilds only the
functions whose fingerprint changed since the last successful deploy, and
any function that is not serving the revision that deploy left. When only
secrets or settings changed, they are applied to the running functions as a
new revision without a rebuild. A removed function, a missing or unreadable
record, or any doubt means every function is rebuilt.

`deploys --deploy <id>` (and `--json`) reports the path as `pipeline`:
`full`, `partial-backend` (only changed functions), `config-only` (settings
or secrets only), `no-backend-change`, `frontend-only` or `client-only`.

## Before a deploy can run

- Building and deploying to one development environment are free (within the
  organization's monthly free deploy minutes); no card, no payment. $10 a month per published project. Need more room? Add capacity for $10 a month.
- Publishing to **production** is the step a person pays for. The first
  `deploy --target production` prints the one card link (project,
  organization, price, renewal), waits until the payment provider confirms
  it (heartbeat every 30 s, exit 6 on timeout) and continues the deploy by
  itself; `--no-wait-for-payment` exits 4 with `error.code` =
  `PAYMENT_REQUIRED` instead (CI). See
  [Pricing and billing](https://spaceboy.ai/docs/agents/pricing-and-billing.md).
  A redeploy of an already-published project never asks again.
- A second unpublished project's first development deploy exits 4 with
  `error.code` = `FREE_DEVELOPMENT_LIMIT` (one free development environment
  per organization); `error.action` names how to free it, or publish a
  project.
- What the first deploy creates, and what each published project includes:
  [What a project provisions](https://spaceboy.ai/docs/agents/what-a-project-provisions.md).

- An app that sends mail is checked against the platform mail gateway
  before anything is built. If the gateway does not answer ready, a
  development deploy keeps asking for up to 2 minutes and a production
  deploy for up to 20 seconds, then fails with `mail_gateway_not_ready` and
  nothing changes (what is running keeps working). Deploy again in a
  minute.
- The web build gets `VITE_DEPLOY_TARGET` = `development` or `production`
  ([Sign-in and users](https://spaceboy.ai/docs/agents/sign-in-and-users.md)).
- Every development environment's site is served with
  `X-Robots-Tag: noindex, nofollow`, on its Spaceboy address and on any
  custom domain (a published project's development environment included),
  after your own `firebase.json` headers so it cannot be overridden.
  Production is never given it.

## Known problems

- A failed **first** deploy can leave broken, half-created functions in
  the environment, and nothing cleans them up automatically. If later
  deploys keep failing on them, contact support@spaceboy.ai.
- Build logs are not available from the CLI; `failureReason` carries the
  extracted error. Runtime errors: [Logs](https://spaceboy.ai/docs/agents/logs.md).
