pr_01m47d24b0e6n91zwymwxg0vpx/packages/contracts/src/deployments.ts

107 lines4,032 bytesCodeBlame
1import type { User, Viewer } from "./identity";
2import type { RepoPath } from "./repos";
3import type { Result } from "./result";
4
5/**
6 * Deployments: every pull request gets a live preview on g1t.page, and the
7 * default branch goes to production on each push. Apps run as Workers in a
8 * Workers for Platforms namespace, so an app no one visits costs nothing.
9 * A paid feature: the workspace turns it on with a monthly plan (see
10 * `Feature` in `./billing`), and each repository then chooses for itself.
11 */
12
13/** The domain apps are served on. Never g1t.sh, so they share no cookies with it. */
14export const DEPLOYMENTS_DOMAIN = "g1t.page";
15
16/** A repository's deployment settings. */
17export type DeploySettings = {
18 /** Whether this repository deploys at all. Off until someone turns it on. */
19 enabled: boolean;
20 /** A preview for every open pull request. */
21 previews: boolean;
22 /** The default branch deployed to production on every push. */
23 production: boolean;
24 /** Runs instead of the project's own `build` script. */
25 buildCommand: string | null;
26 /** What to serve, for a static site; found by itself when null. */
27 outputDir: string | null;
28 /** A preview no one has visited in this many days is taken down. */
29 idleDays: number;
30 /** Where production is served. */
31 productionUrl: string;
32};
33
34export type DeployKind = "preview" | "production";
35
36export type DeployStatus =
37 /** Waiting for a sandbox. */
38 | "queued"
39 | "building"
40 | "ready"
41 | "failed"
42 /** Not built: the workspace's plan is off, or the build was replaced. */
43 | "skipped";
44
45/** One build of one commit, and where it went. */
46export type Deployment = {
47 id: string;
48 kind: DeployKind;
49 /** For a preview: the pull request. */
50 number: number | null;
51 commit: string;
52 status: DeployStatus;
53 url: string;
54 /** Why it failed or was skipped. */
55 error: string | null;
56 /** What the build could not provide, such as bindings not provisioned yet. */
57 warnings: string[];
58 /** How long the build ran, in seconds; charged at the container price. */
59 buildSeconds: number | null;
60 createdBy: string;
61 /** RFC 3339. */
62 createdAt: string;
63 finishedAt: string | null;
64};
65
66/** An app that is up: production, or one pull request's preview. */
67export type LiveApp = {
68 kind: DeployKind;
69 number: number | null;
70 url: string;
71 commit: string;
72 /** RFC 3339: when it was last deployed. */
73 deployedAt: string;
74};
75
76/** What a workspace's apps used this month against its plan. */
77export type DeployUsage = {
78 /** `YYYY-MM`. */
79 month: string;
80 requests: number;
81 cpuMs: number;
82 /** Apps up now, and the most at once this month. */
83 apps: number;
84 peakApps: number;
85 buildSeconds: number;
86 /** Charged so far this month for builds, in millionths of a dollar. */
87 buildMicros: number;
88 /** RFC 3339: when requests and CPU time were last counted. */
89 countedAt: string | null;
90};
91
92export interface DeploymentsApi {
93 /** Members of the workspace only. */
94 settings(repo: RepoPath, viewer: Viewer): Promise<Result<DeploySettings>>;
95 /** Members of the workspace only. Turning deployments on needs the plan. */
96 updateSettings(actor: User, repo: RepoPath, changes: Partial<DeploySettings>): Promise<Result<DeploySettings>>;
97 /** The newest deployments first, and what is up now. */
98 list(repo: RepoPath, viewer: Viewer): Promise<Result<{ deployments: Deployment[]; live: LiveApp[] }>>;
99 /** One deployment, with its build log. */
100 get(repo: RepoPath, id: string, viewer: Viewer): Promise<Result<Deployment & { log: string | null }>>;
101 /** Builds production, or a pull request's preview, again from its current head. */
102 redeploy(actor: User, repo: RepoPath, number: number | null): Promise<Result<Deployment>>;
103 /** Takes production, or a pull request's preview, down now. */
104 takeDown(actor: User, repo: RepoPath, number: number | null): Promise<Result<true>>;
105 /** What the workspace's apps used this month. Members only. */
106 usage(workspace: string, viewer: Viewer): Promise<Result<DeployUsage>>;
107}