# deploy

> Deploy a repository as a new app in one call: a public one on any git host, or one of your own repos.

`noclick.deploy` copies the repository (or takes your repo as it is), makes the app, sets the secrets it reads,
gives it the key to your account it asks for (or has the deploy ask the account's owner), and deploys production.
Ask with `dry_run=True` first to see what it would do, what it would cost a month and what it asks of you, with
nothing made.

```python
plan = noclick.deploy("https://github.com/acme/console", dry_run=True)
print(plan.runtime, f"${plan.estimate.monthly_usd:.2f} a month", plan.missing_secrets)

app = noclick.deploy("https://github.com/acme/console", secrets={"STRIPE_SECRET_KEY": "sk_live_..."}).wait()
print(app.url)
```

## deploy

```typescript
function deploy(
  source: string | ApiObject,
  options: DeployOptions & { dryRun: true },
): Promise<DeployPlan>
function deploy(
  source: string | ApiObject,
  options?: DeployOptions & { dryRun?: false },
): Promise<RepoDeploy>
```

Deploy `source` as a new app of yours: a public git repository's https URL, on any host (copied into a new repo
of yours, the one branch it deploys), or one of your own repos (a `Repo`, its id or its name, deployed as it is).

It makes the app, sets the secrets its noclick.json says its code reads (NoClick makes the ones marked
`generate`; `secrets` brings the rest), gives the app the key to your account it asks for when `accountKey` names
exactly its scopes (only you signed in, or a key holding `keys:create`; otherwise the deploy asks the account's
owner), and deploys production. Never a private repository, a URL with a login, an `ssh://` or `git@` address, or
another account's repo: push a private one to a repo of yours with git and deploy that. What can't deploy is
refused before anything is made. Needs the `apps` and `repos` scopes.

Resolves to a `RepoDeploy` whose `wait()` gives the live app (`url`, where its key stands, passwords to read once);
with `dryRun: true`, to a `DeployPlan` of what it would do, what it would cost and what it asks of you.

    const plan = await deploy("https://gitlab.com/acme/tools/console", { dryRun: true });
    for (const line of plan.accountKey?.can ?? []) console.log("its key can", line.can);
    const done = await (await deploy("https://gitlab.com/acme/tools/console", {
      secrets: { STRIPE_SECRET_KEY: "sk_live_..." },
    })).wait();
    const app = Project.fromId(done.projectId);
    for (const name of done.toReveal) console.log(name, await app.secrets.reveal(name));

Parameters:

- `source` (`string | ApiObject`, required)
- `options.dryRun` (`boolean`, required): Answer what it would do, with nothing made (only reading the repository is charged, its seconds).
- `options.ref` (`string`): A branch, tag or full commit sha to deploy (the repository's default branch when omitted).
- `options.name` (`string`): The app's name (and its copy's); omitted, the repository's own, or the first of name-2, name-3... free.
- `options.secrets` (`Record<string, string>`): Values for the secrets the repository's noclick.json asks you to set, by name. Write-only.
- `options.accountKey` (`string[]`): Give the app the key to your account it asks for, as part of the deploy: exactly its scopes, as the dry run
names them. Only you signed in, or a key holding `keys:create`; otherwise leave it out and the deploy asks the
account's owner.
- `options.keySpendCapUsdPerMonth` (`number`): The most the key may spend in a calendar month (none of its own unless set).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: A `RepoDeploy` operation whose `wait()` gives a `DeployResult` (the app's `url`, where its key
stands, the passwords to read once); with `dry_run=True`, a `DeployPlan` of what it would do, what it
would cost a month and what it asks of you, with nothing made.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

## DeployPlan

```typescript
class DeployPlan
```

What a deploy would do, with nothing made: `deploy(source, { dryRun: true })`.

| Attribute | Type | Description |
| --- | --- | --- |
| `source` | `JsonObject` | What it read: `kind` (`url` or `repo`), the `url` or `repo_id`, and the `commit` and `branch`. |
| `name` | `string` | The app's name (and its copy's): the one you gave, or the first free one from the repository's. |
| `runtime` | `string | null` | Where the app would run (`edge` or `container`); null when it won't run as it stands. |
| `framework` | `string | null` |  |
| `reason` | `string | null` |  |
| `noclickJson` | `boolean` | Whether the repository has a noclick.json. |
| `accountKey` | `DeployKeyAsk | null` |  |
| `secrets` | `DeploySecret[]` |  |
| `missingSecrets` | `string[]` | The secrets you set that the call would have to bring: a deploy without them is refused. |
| `refused` | `JsonObject | null` | Why it won't deploy as it stands (`message`, `next_step`); null when it will. |
| `estimate` | `Estimate` | What it would cost a month, priced for your account (usage-based: what's charged is what's used). |
| `funds` | `JsonObject` | Whether your account can pay to run it now (`ok`); if not, the `refusal` a deploy would meet. |
| `makes` | `string[]` | What it makes: `repo` (a copy) and `project` (the app). |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## DeployResult

```typescript
interface DeployResult
```

A deploy that ended live: what `RepoDeploy.wait()` resolves to.

| Attribute | Type | Description |
| --- | --- | --- |
| `projectId` | `string` |  |
| `name` | `string` |  |
| `url` | `string | null` | Where production serves it. |
| `repoId` | `string | null` |  |
| `deploymentId` | `string | null` |  |
| `commit` | `string | null` |  |
| `accountKey` | `JsonObject | null` | Where its key to the account stands (`status`: `granted`, `waiting` for the owner...); null if it asks none. |
| `toReveal` | `string[]` | The passwords NoClick made that nobody has read yet: read each once with `app.secrets.reveal(name)`. |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## DeploySecret

```typescript
interface DeploySecret
```

A secret the repository's noclick.json says its code reads.

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `generate` | `string | null` | What NoClick makes for it (`secret` or `password`); null for one you set. |
| `description` | `string | null` | What it is, in the repository's words. |
| `required` | `boolean` | For one you set: whether a deploy without it is refused. One NoClick makes is always there. |
| `given` | `boolean` | For one you set: whether the call brought its value. |
| `shownOnce` | `boolean` | For one NoClick makes: whether it's a password you read once (`app.secrets.reveal`). |

## DeployKeyAsk

```typescript
interface DeployKeyAsk
```

The key to your account the repository asks for, as you're shown it before you agree.

| Attribute | Type | Description |
| --- | --- | --- |
| `scopes` | `string[]` |  |
| `can` | `JsonObject[]` | What the key can do, a line per scope (`scope`, `can`, `reads_secrets`). |
| `cannot` | `string[]` | What no app's key can do, whatever it holds. |
| `note` | `string` | That whoever can change the app's code acts with the key. |
| `givenBy` | `"you" | "deploy"` | `you`: you give it in the call (send `accountKey` with these scopes); `deploy`: the deploy asks for it. |
| `atDeploy` | `string | null` | When the deploy asks: `asks_owner`, `given` or `denied`, as the account's `app_key` approval setting decides. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## RepoDeploy

```typescript
class RepoDeploy extends Operation
```

A repository being deployed as a new app: the operation `deploy` returns. `wait()` resolves to a `DeployResult`
once it's live; `progress` shows each stage (copying, making the app, setting its secrets and key, building and
deploying). A deploy that fails after the copy keeps the app and its repo, and its error says how to go on.

| Attribute | Type | Description |
| --- | --- | --- |
| `projectId` | `string | null` | The new app's id, once it's made. |
| `repoId` | `string | null` | The repo it deploys: the copy, or yours. |
| `deploymentId` | `string | null` | The production deployment, once it's started. |
