
# CI/CD with deploy keys

A deploy key lets CI ship one app without a person's agent. Put it in `AGC_TOKEN` and run `agc ship --from-git <commit>`: the release follows the same rehearsal and approval rules as any ship. Success is exit 0 with `"outcome": "live"`, and exit 10 means a person approves.

```bash
AGC_TOKEN=agcd_… agc ship --from-git <40-character sha> --idempotency-key camp-bookings/ship/<sha>
```

## Ask your agent

```text
Set up GitHub Actions to ship camp-bookings on every push to main, using a deploy key.
Tell me where to make the key and which secret to add.
```

The person makes the key in the console, which shows it once, and adds it as a repository secret named `AGC_TOKEN`. An agent never asks for its value.

## Steps

1. **Make the key** (the app's owner only): **Apps**, the app, **Settings** at the bottom of its sidebar, **Deploy keys**. Under **Make a deploy key**, fill in **Name** (such as `github-actions`, at most 60 characters), **Purpose** (no default; for CI, **Ship-only: ship this app, never decide**) and **Expires** (**Expires in 30 days**, preselected, **Expires in 60 days** or **Expires in 90 days**), then **Make key**. The next page shows the key (`agcd_…`) once.2. **Store it** as a CI secret named `AGC_TOKEN`, which agc uses in place of any saved agent. Check it with `agc whoami`: its `data` has `"kind": "deploy_key"`, `state`, `purpose` and `expires_at`, and `source` reads `AGC_TOKEN`.
3. **Ship the commit:**

```yaml
# .github/workflows/ship.yml
on: { push: { branches: [main] } }
jobs:
  ship:
    runs-on: ubuntu-latest
    env: { AGC_TOKEN: "${{ secrets.AGC_TOKEN }}" }
    steps:
      - uses: actions/checkout@v4
      - run: npm install -g https://agent.cloud/agc.tgz
      - shell: bash
        run: |
          set +e
          key="$GITHUB_REPOSITORY/ship/$GITHUB_SHA"
          agc ship --from-git "$GITHUB_SHA" --idempotency-key "$key" --branch main > ship.ndjson
          code=$?
          if [ "$code" -eq 20 ]; then agc wait --idempotency-key "$key" > ship.ndjson; code=$?; fi
          tail -n 1 ship.ndjson | tee ship-envelope.json
          case "$code" in
            0) jq -e '.ok == true and .outcome == "live"' ship-envelope.json ;;
            10|20) echo "Pending: resume with agc wait and the same key"; exit "$code" ;;
            *) exit "$code" ;;
          esac
      - uses: actions/upload-artifact@v4
        if: always()
        with: { name: ship-outcome, path: "ship*" }
```

`CI` is set, so agc answers in JSON, and the envelope and op are kept as artifacts. Exit 10 (approval pending) and 20 (still running after a bounded wait) fail the job, so pending is never mistaken for live. Resume with the same key.

4. **If the job died before the answer**, find the ship by its key in a checkout (agc reads the app from `agentcloud.toml`) instead of shipping again. `op.not_found` means nothing was accepted with that key, so it's safe to ship.

```bash
agc wait --idempotency-key "camp-bookings/ship/<sha>"
```

## How it works

**Scope.** Control stores only the key's hash, bound to its maker, organization, app and purpose, and rereads it on every request, so a revoke or purpose change applies at once. Another app, even in the same organization, answers `app.unavailable` without revealing whether it exists. The key can't create apps (`agent.not_allowed`).

**No key decides an approval.** Its ships get the same plan, rehearsal and [release policy](/docs/owners/release-governance.md) as an agent's, with `decision.in_session: false`. The key relays the link and a person decides in the console (`agc approve` with a key is `approval.no_session`).

| Purpose (console label) | What the key may do |
|---|---|
| Builder: ship | As a Builder agent on this app: read, masked copies, ship, generate secrets, request connections, previews, feedback |
| Mirror-only: test on copies | Read, use masked copies of production, send feedback; no shipping |
| Ship-only: ship this app, never decide | Status, ops, review, schema, secret names, upload snapshots and assets, ship, read approvals; refused for logs, feedback, connections, copies, secrets, domains, previews |
| Task tokens: mint short-lived sandbox tokens | Mint and revoke [task tokens](/docs/ship/work-tab-and-builders.md#tasks-and-task-tokens) for this app's builder sandboxes, and `whoami` |

Pick Ship-only for CI: a leaked one can only ship the app's code through rehearsal, while a leaked Builder key exposes its whole row. Mirror-only suits test jobs. Anything else is `agent.purpose`.

**`--from-git`.** It needs `AGC_TOKEN` and the full 40-character id (`git rev-parse <ref>^{commit}`). agc packs exactly that commit from git objects, never the working tree, so a bare clone works. The commit's own `agentcloud.toml`, migrations, checks, ignore files and assets decide everything, and `--app` must equal its `[app] name`. git runs with no credentials, config, hooks or filters and fetches nothing. A commit always packs to the same bytes and automatic key. `--branch` shows on the release page, and `--work` and `--pr` are recorded on the op. The answer adds `data.commit` and `data.snapshot_sha256`. `[[verify]]` is skipped, with a warning.

**Idempotency keys.** agc's automatic key repeats for 24 hours, but a key you pass never expires. Resent with the same content, it returns the original op. With different content, it's `ship.idempotency_conflict`. To ship the same commit again after a failed release, use a new key, for example with an attempt number.

## Replacing and revoking a key

A key's expiry is fixed; `agc status` reports `expires_at`. Seven days before it, Agent Cloud records `key.expiring` in the app's activity and warns the key’s person and the app’s owners once: always by email, by Slack or iMessage when that switch is on, and to channels subscribed to the app’s `failures` notices. Until the key is revoked or expires, their console pages show a banner naming it. To replace a key, make a new one, swap it into CI's secrets, then revoke the old one, so no job runs keyless. Concurrent jobs can share one key.

**Replace a Builder key in one step.** A Builder key's row offers **Replace**: pick an expiry (1 to 90 days) and copy the new value, shown once. The old key works until the new one's first request, which moves the old key's open tasks to it at the same counter, carries over its ask settings and revokes the old key (`auth.invalid` from then on). Until then the row shows **Replacement waiting for first use**, and you can cancel it.

**Revoke** on the key's row takes effect at its next request, with no confirmation. A key also stops at its expiry, when its person leaves the organization, or when the app is transferred. Ending a key runs the same cleanup as revoking an agent: unapproved releases that haven't started on production are cancelled, its masked copies are deleted and its task tokens end ([what revoking cleans up](/docs/owners/managing-agents.md#what-revoking-cleans-up)).

## Reference

Only the app's owner makes a key (anyone else gets "Not yours to make"). Anyone in the organization sees them, view only if they don't manage the app. Who revokes from where is on [Managing agents](/docs/owners/managing-agents.md#who-can-do-what).

A live key's row shows its name, purpose, person, `expires` date, and when it was last used or `not used yet`. A Builder or Ship-only key's row also sets how many [Work](/docs/ship/work-tab-and-builders.md) asks it may file: **Asks an hour** (1 to 100, default 20) and **Asking on**/**Asking off**; past the cap it gets `ask.quota`, and switched off, `ask.off`. A Task tokens key adds its live tokens. Revoked and expired keys leave the list but stay in their maker's Your agents. An unclaimed app has no **Deploy keys** section until someone [claims it](/docs/getting-started/working-with-your-coding-agent.md#claiming-an-app-and-connecting-your-agent).

## Errors

Every code, its exit and fix: [Error code catalog](/docs/reference/error-code-catalog.md). `agc whoami` in CI after the key was revoked in the app's settings:

```json
{ "ok": false, "product": "agent.cloud", "command": "whoami",
  "error": { "retryable": false, "code": "auth.invalid",
    "message": "This deploy key was disconnected: revoked in the app’s settings.",
    "fix": "The token in AGC_TOKEN was refused. Replace the deploy key with a new one from the app’s settings in the console, or remove AGC_TOKEN deliberately." },
  "next": [{ "cmd": "agc whoami", "why": "confirm which credential is in use, once AGC_TOKEN is replaced" }] }
```

`auth.expired` (past its expiry) has the same fix. Snapshot, idempotency-key and task-token limits: [Limits and quotas](/docs/reference/limits-and-quotas.md).

## Related

- [agc ship, end to end](/docs/ship/agc-ship-end-to-end.md)
- [Work tab and builders](/docs/ship/work-tab-and-builders.md)
- [Release governance](/docs/owners/release-governance.md)
- [agc environment variables](/docs/reference/agc-environment-variables.md)

<!-- feedback:start -->
---
Wrong, outdated or unclear? Run `agc feedback docs "ship/ci-cd-with-deploy-keys: <what is wrong>" --body <file>` with the sentence quoted in the file. No code or app data is sent.
<!-- feedback:end -->
