g1t/services/deployments/src/index.ts
Pick any line to see why it is the way it is: the commit, the pull request and issue it came from, and what the agent was thinking.
| Deployments: a preview for every pull request, production on g1t.page | 1 | /** |
| Projects: what a workspace builds and runs, first on every page | 2 | * The deployments service: a project's production, deployed from its |
| 3 | * default branch on every push, and a live preview of every branch with an | |
| 4 | * open pull request, on g1t.page. | |
| Deployments: a preview for every pull request, production on g1t.page | 5 | * |
| 6 | * It reacts to events (a pull request opened, ready, pushed to, closed or | |
| Projects: what a workspace builds and runs, first on every page | 7 | * merged; a push to the default branch) for every project built from the |
| 8 | * repository, asks billing whether the workspace pays for Deployments, and | |
| 9 | * asks the runner to build the commit in a sandbox. The sandbox reports | |
| 10 | * back through the API with a token for that build alone; this service | |
| 11 | * opens the upload of its files and puts the finished app in the Workers | |
| 12 | * for Platforms namespace, where the `*.g1t.page` dispatcher finds it by | |
| 13 | * hostname. | |
| Deployments: a preview for every pull request, production on g1t.page | 14 | * |
| 15 | * Nothing here is free. A build is charged by the second; requests, CPU | |
| 16 | * time and apps past the plan's allowance are charged once the month is | |
| 17 | * over. A Worker runs only while it answers a request, so an app no one | |
| 18 | * visits costs nothing, and a preview is taken down when its pull request | |
| Projects: what a workspace builds and runs, first on every page | 19 | * closes or after its project's idle days. |
| Deployments: a preview for every pull request, production on g1t.page | 20 | * |
| 21 | * Reached through service bindings (`POST /rpc/<method>`) and, for a | |
| 22 | * build's reports, through the API (`POST /jobs/<id>/<step>`). | |
| 23 | */ | |
| 24 | ||
| 25 | import { | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 26 | CUSTOM_DOMAIN_TARGET, |
| Deployments: a preview for every pull request, production on g1t.page | 27 | DEPLOYMENTS_ALLOWANCE, |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 28 | SLUG_HOLD_DAYS, |
| Deployments: a preview for every pull request, production on g1t.page | 29 | billingClient, |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 30 | currentWorkspaceSlug, |
| Deployments: a preview for every pull request, production on g1t.page | 31 | fail, |
| 32 | identityClient, | |
| 33 | newId, | |
| 34 | ok, | |
| Projects: what a workspace builds and runs, first on every page | 35 | projectsClient, |
| Deployments: a preview for every pull request, production on g1t.page | 36 | reposClient, |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 37 | staleSlugs, |
| Deployments: a preview for every pull request, production on g1t.page | 38 | workClient, |
| 39 | type DeployKind, | |
| 40 | type DeploySettings, | |
| 41 | type DeployStatus, | |
| 42 | type DeployUsage, | |
| 43 | type Deployment, | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 44 | type Domain, |
| Deployments: a preview for every pull request, production on g1t.page | 45 | type G1tEvent, |
| 46 | type LiveApp, | |
| Projects: what a workspace builds and runs, first on every page | 47 | type Project, |
| 48 | type ProjectDeploys, | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 49 | type ProjectDomains, |
| Projects: what a workspace builds and runs, first on every page | 50 | type ProjectRef, |
| Deployments: a preview for every pull request, production on g1t.page | 51 | type RepoPath, |
| 52 | type Result, | |
| 53 | type ServiceBinding, | |
| 54 | type User, | |
| 55 | type Viewer, | |
| 56 | } from "@g1t/contracts"; | |
| 57 | ||
| 58 | import { Cloudflare, type BuiltWorker, type Manifest } from "./cloudflare"; | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 59 | import { CustomHostnames } from "./custom-hostnames"; |
| 60 | import { Domains, NOT_ENABLED_NOTICE, extraDomains, toDomain } from "./domains"; | |
| 61 | import { appHost, appUrl, label, uniqueLabel } from "./names"; | |
| Deployments: a preview for every pull request, production on g1t.page | 62 | |
| 63 | type Env = { | |
| 64 | DB: D1Database; | |
| 65 | REPOS: ServiceBinding; | |
| 66 | WORK: ServiceBinding; | |
| 67 | IDENTITY: ServiceBinding; | |
| 68 | BILLING: ServiceBinding; | |
| 69 | RUNNER: ServiceBinding; | |
| Projects: what a workspace builds and runs, first on every page | 70 | PROJECTS: ServiceBinding; |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 71 | /** Secrets and variables: the actions service holds the one store. */ |
| 72 | ACTIONS: ServiceBinding; | |
| Deployments: a preview for every pull request, production on g1t.page | 73 | /** Secret: scoped to Workers scripts and analytics on g1t's account. */ |
| 74 | CLOUDFLARE_API_TOKEN?: string; | |
| 75 | CLOUDFLARE_ACCOUNT_ID: string; | |
| 76 | DISPATCH_NAMESPACE: string; | |
| 77 | SITE: string; | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 78 | /** Custom domains: hostname to app, read by the dispatcher. */ |
| 79 | DOMAINS?: KVNamespace; | |
| 80 | /** The g1t.page zone, where custom hostnames are added (Cloudflare for SaaS). */ | |
| 81 | CUSTOM_HOSTNAMES_ZONE_ID?: string; | |
| Deployments: a preview for every pull request, production on g1t.page | 82 | }; |
| 83 | ||
| 84 | /** A build that has not reported in this long has died. */ | |
| 85 | const BUILD_TIMEOUT_MS = 45 * 60 * 1000; | |
| Projects: what a workspace builds and runs, first on every page | 86 | /** A script in the namespace that no app holds, older than this, is removed. */ |
| 87 | const ORPHAN_AFTER_MS = 60 * 60 * 1000; | |
| Deployments: a preview for every pull request, production on g1t.page | 88 | const LIST_LIMIT = 50; |
| 89 | const STATUS_CONTEXT = "g1t / deploy"; | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 90 | /** |
| 91 | * Deliveries of `workspace.renamed` that wait for the projects service to | |
| 92 | * have seen it too, before going ahead with the new slug regardless. | |
| 93 | */ | |
| 94 | const RENAME_WAITS = 3; | |
| Deployments: a preview for every pull request, production on g1t.page | 95 | |
| 96 | const now = () => new Date().toISOString(); | |
| 97 | const month = (at = new Date()) => at.toISOString().slice(0, 7); | |
| 98 | ||
| 99 | async function sha256(text: string): Promise<string> { | |
| 100 | const digest = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(text)); | |
| 101 | return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join(""); | |
| 102 | } | |
| 103 | ||
| 104 | function randomToken(): string { | |
| 105 | return [...crypto.getRandomValues(new Uint8Array(32))].map((b) => b.toString(16).padStart(2, "0")).join(""); | |
| 106 | } | |
| 107 | ||
| 108 | function isMember(viewer: Viewer, slug: string): boolean { | |
| 109 | return !!viewer?.workspaces?.some((membership) => membership.slug === slug.toLowerCase()); | |
| 110 | } | |
| 111 | ||
| Projects: what a workspace builds and runs, first on every page | 112 | /** The repository a project builds from. */ |
| 113 | function repoOf(project: Project): { id: string; path: RepoPath; defaultBranch: string } { | |
| 114 | if (project.source.kind !== "hosted") throw new Error("Only projects hosted on g1t deploy so far."); | |
| 115 | return { id: project.source.repoId, path: project.source.repo, defaultBranch: project.source.defaultBranch }; | |
| 116 | } | |
| 117 | ||
| Deployments: a preview for every pull request, production on g1t.page | 118 | type SettingsRow = { |
| Projects: what a workspace builds and runs, first on every page | 119 | project_id: string; |
| 120 | workspace: string; | |
| 121 | slug: string; | |
| Deployments: a preview for every pull request, production on g1t.page | 122 | repo_id: string; |
| 123 | enabled: number; | |
| 124 | previews: number; | |
| 125 | production: number; | |
| 126 | build_command: string | null; | |
| 127 | output_dir: string | null; | |
| 128 | idle_days: number; | |
| 129 | }; | |
| 130 | ||
| 131 | type DeploymentRow = { | |
| 132 | id: string; | |
| Projects: what a workspace builds and runs, first on every page | 133 | project_id: string; |
| 134 | workspace: string; | |
| 135 | slug: string; | |
| Deployments: a preview for every pull request, production on g1t.page | 136 | repo_id: string; |
| Projects: what a workspace builds and runs, first on every page | 137 | repo: string; |
| Deployments: a preview for every pull request, production on g1t.page | 138 | kind: DeployKind; |
| Projects: what a workspace builds and runs, first on every page | 139 | branch: string | null; |
| Deployments: a preview for every pull request, production on g1t.page | 140 | number: number | null; |
| 141 | commit_sha: string; | |
| 142 | script: string; | |
| 143 | status: DeployStatus; | |
| 144 | error: string | null; | |
| 145 | warnings: string; | |
| 146 | log: string | null; | |
| 147 | token_hash: string | null; | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 148 | trusted: number; |
| Deployments: a preview for every pull request, production on g1t.page | 149 | build_seconds: number | null; |
| 150 | created_by: string; | |
| 151 | created_at: string; | |
| 152 | finished_at: string | null; | |
| 153 | }; | |
| 154 | ||
| 155 | type AppRow = { | |
| 156 | script: string; | |
| Projects: what a workspace builds and runs, first on every page | 157 | project_id: string; |
| 158 | workspace: string; | |
| 159 | slug: string; | |
| Deployments: a preview for every pull request, production on g1t.page | 160 | kind: DeployKind; |
| Projects: what a workspace builds and runs, first on every page | 161 | branch: string | null; |
| Deployments: a preview for every pull request, production on g1t.page | 162 | number: number | null; |
| 163 | commit_sha: string; | |
| 164 | deployed_at: string; | |
| 165 | created_at: string; | |
| 166 | last_request_at: string | null; | |
| Usage limits: unpaid usage can only go so far | 167 | /** Set while its workspace is over its limit; see `holdToLimits`. */ |
| 168 | paused_at: string | null; | |
| Deployments: a preview for every pull request, production on g1t.page | 169 | }; |
| 170 | ||
| 171 | function toDeployment(row: DeploymentRow): Deployment { | |
| 172 | return { | |
| 173 | id: row.id, | |
| 174 | kind: row.kind, | |
| Projects: what a workspace builds and runs, first on every page | 175 | branch: row.branch, |
| Deployments: a preview for every pull request, production on g1t.page | 176 | number: row.number, |
| 177 | commit: row.commit_sha, | |
| 178 | status: row.status, | |
| 179 | url: appUrl(row.script), | |
| 180 | error: row.error, | |
| 181 | warnings: JSON.parse(row.warnings || "[]") as string[], | |
| 182 | buildSeconds: row.build_seconds, | |
| 183 | createdBy: row.created_by, | |
| 184 | createdAt: row.created_at, | |
| 185 | finishedAt: row.finished_at, | |
| 186 | }; | |
| 187 | } | |
| 188 | ||
| Projects: what a workspace builds and runs, first on every page | 189 | function toLive(app: AppRow): LiveApp { |
| 190 | return { | |
| 191 | kind: app.kind, | |
| 192 | branch: app.branch, | |
| 193 | number: app.number, | |
| 194 | url: appUrl(app.script), | |
| 195 | commit: app.commit_sha, | |
| 196 | deployedAt: app.deployed_at, | |
| 197 | }; | |
| 198 | } | |
| 199 | ||
| Deployments: a preview for every pull request, production on g1t.page | 200 | class Deployments { |
| 201 | constructor(private readonly env: Env) {} | |
| 202 | ||
| 203 | private get cloudflare(): Cloudflare | null { | |
| 204 | const token = this.env.CLOUDFLARE_API_TOKEN; | |
| 205 | return token ? new Cloudflare(token, this.env.CLOUDFLARE_ACCOUNT_ID, this.env.DISPATCH_NAMESPACE) : null; | |
| 206 | } | |
| 207 | ||
| 208 | private get db() { | |
| 209 | return this.env.DB; | |
| 210 | } | |
| 211 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 212 | private get domains(): Domains { |
| 213 | const token = this.env.CLOUDFLARE_API_TOKEN; | |
| 214 | const zone = this.env.CUSTOM_HOSTNAMES_ZONE_ID; | |
| 215 | return new Domains(this.env.DB, this.env.DOMAINS, token && zone ? new CustomHostnames(token, zone) : null); | |
| 216 | } | |
| 217 | ||
| Projects: what a workspace builds and runs, first on every page | 218 | private get projects() { |
| 219 | return projectsClient(this.env.PROJECTS); | |
| 220 | } | |
| 221 | ||
| Deployments: a preview for every pull request, production on g1t.page | 222 | /** The workspace itself, as the service acts for it. */ |
| 223 | private async workspaceActor(slug: string): Promise<User | null> { | |
| 224 | const workspace = await identityClient(this.env.IDENTITY).getWorkspace(slug); | |
| 225 | if (!workspace) return null; | |
| 226 | return { | |
| 227 | id: workspace.id, | |
| 228 | username: workspace.slug, | |
| 229 | kind: "workspace", | |
| 230 | verified: true, | |
| 231 | workspaces: [{ slug: workspace.slug, role: "member" }], | |
| 232 | }; | |
| 233 | } | |
| 234 | ||
| Secrets and variables: one list, rows per environment, for workflows and deployments | 235 | /** |
| Projects: what a workspace builds and runs, first on every page | 236 | * What the project's secrets and variables available to deployments give |
| 237 | * production or a preview: its build's environment, and the same again as | |
| 238 | * the running app's bindings. Untrusted builds get no secrets. | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 239 | */ |
| 240 | private async resolve( | |
| Projects: what a workspace builds and runs, first on every page | 241 | project: { id: string; slug: string; repoId: string; repo: RepoPath }, |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 242 | environment: DeployKind, |
| 243 | trusted: boolean, | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 244 | branch: string | null, |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 245 | ): Promise<{ secrets: Record<string, string>; variables: Record<string, string> }> { |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 246 | const [rows, references] = await Promise.all([ |
| 247 | this.rows(project, environment, trusted), | |
| 248 | this.references(project.id, environment === "preview" ? branch : null), | |
| 249 | ]); | |
| 250 | // The project's own rows win over a dependency's address of the same name. | |
| 251 | return { secrets: rows.secrets, variables: { ...references, ...rows.variables } }; | |
| 252 | } | |
| 253 | ||
| 254 | /** | |
| 255 | * Each dependency's address, under the name the dependency gives it: | |
| 256 | * for a preview, the same branch's preview of it if one is up, else its | |
| 257 | * production; for production, its production. | |
| 258 | */ | |
| 259 | private async references(projectId: string, branch: string | null): Promise<Record<string, string>> { | |
| 260 | const graph = await this.projects.graph(projectId).catch(() => null); | |
| 261 | const out: Record<string, string> = {}; | |
| 262 | for (const dependency of graph?.dependsOn ?? []) { | |
| 263 | if (!dependency.as) continue; | |
| 264 | const app = | |
| 265 | (branch | |
| 266 | ? await this.db | |
| 267 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = 'preview' AND branch = ?") | |
| 268 | .bind(dependency.id, branch) | |
| 269 | .first<{ script: string }>() | |
| 270 | : null) ?? | |
| 271 | (await this.db | |
| 272 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = 'production'") | |
| 273 | .bind(dependency.id) | |
| 274 | .first<{ script: string }>()); | |
| 275 | out[dependency.as] = appUrl(app?.script ?? (await label(dependency.workspace, dependency.slug, null))); | |
| 276 | } | |
| 277 | return out; | |
| 278 | } | |
| 279 | ||
| 280 | private async rows( | |
| 281 | project: { id: string; slug: string; repoId: string; repo: RepoPath }, | |
| 282 | environment: DeployKind, | |
| 283 | trusted: boolean, | |
| 284 | ): Promise<{ secrets: Record<string, string>; variables: Record<string, string> }> { | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 285 | const response = await this.env.ACTIONS.fetch("https://actions/rpc/resolve_settings", { |
| 286 | method: "POST", | |
| 287 | headers: { "content-type": "application/json" }, | |
| Projects: what a workspace builds and runs, first on every page | 288 | body: JSON.stringify({ |
| 289 | repoId: project.repoId, | |
| 290 | repo: project.repo, | |
| 291 | projectId: project.id, | |
| 292 | projectSlug: project.slug, | |
| 293 | consumer: "deployments", | |
| 294 | environment, | |
| 295 | trusted, | |
| 296 | }), | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 297 | }); |
| 298 | if (!response.ok) throw new Error(`Secrets and variables could not be read (${response.status}).`); | |
| 299 | const resolved = (await response.json()) as { secrets: Record<string, string>; variables: Record<string, string> }; | |
| 300 | return { secrets: trusted ? resolved.secrets : {}, variables: resolved.variables }; | |
| 301 | } | |
| 302 | ||
| 303 | /** | |
| Projects: what a workspace builds and runs, first on every page | 304 | * Whether a pull request's author is trusted with the project's secrets: |
| 305 | * g1t's agent, or a member of the workspace. Someone from outside gets a | |
| 306 | * preview built without them, as their workflows run. | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 307 | */ |
| 308 | private async insider(repo: RepoPath, author: User, actor: User): Promise<boolean> { | |
| 309 | if (author.kind === "agent" || author.username === "g1t-agent") return true; | |
| 310 | // On a private repository only members can open one at all. | |
| 311 | const found = await reposClient(this.env.REPOS).get(repo, actor); | |
| 312 | if (found.ok && found.value.isPrivate) return true; | |
| 313 | if (author.workspaces?.some((m) => m.slug === repo.namespace.toLowerCase())) return true; | |
| 314 | const members = await identityClient(this.env.IDENTITY).listMembers(repo.namespace, actor); | |
| 315 | return members.ok && members.value.some((m) => m.username.toLowerCase() === author.username.toLowerCase()); | |
| 316 | } | |
| 317 | ||
| Projects: what a workspace builds and runs, first on every page | 318 | private async settingsRow(projectId: string): Promise<SettingsRow | null> { |
| 319 | return this.db.prepare("SELECT * FROM settings WHERE project_id = ?").bind(projectId).first<SettingsRow>(); | |
| Deployments: a preview for every pull request, production on g1t.page | 320 | } |
| 321 | ||
| Projects: what a workspace builds and runs, first on every page | 322 | /** The name an app gets, unique among apps: production, or a branch's preview. */ |
| 323 | private async scriptFor(project: Project, branch: string | null): Promise<string> { | |
| 324 | const base = await label(project.workspace, project.slug, branch); | |
| 325 | const holder = await this.db | |
| 326 | .prepare( | |
| 327 | `SELECT project_id, branch FROM apps WHERE script = ?1 | |
| 328 | UNION ALL SELECT project_id, branch FROM deployments WHERE script = ?1 LIMIT 1`, | |
| 329 | ) | |
| 330 | .bind(base) | |
| 331 | .first<{ project_id: string; branch: string | null }>(); | |
| 332 | if (!holder || (holder.project_id === project.id && (holder.branch ?? null) === branch)) return base; | |
| 333 | return uniqueLabel(base, `${project.id}/${branch ?? ""}`); | |
| 334 | } | |
| 335 | ||
| 336 | private async toSettings(project: Project, row: SettingsRow | null): Promise<DeploySettings> { | |
| Deployments: a preview for every pull request, production on g1t.page | 337 | return { |
| 338 | enabled: !!row?.enabled, | |
| 339 | previews: row ? !!row.previews : true, | |
| 340 | production: row ? !!row.production : true, | |
| 341 | buildCommand: row?.build_command ?? null, | |
| 342 | outputDir: row?.output_dir ?? null, | |
| 343 | idleDays: row?.idle_days ?? 7, | |
| Projects: what a workspace builds and runs, first on every page | 344 | productionUrl: appUrl(await this.scriptFor(project, null)), |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 345 | primaryDomain: await this.domains.primary(project.id).catch(() => null), |
| Deployments: a preview for every pull request, production on g1t.page | 346 | }; |
| 347 | } | |
| 348 | ||
| Projects: what a workspace builds and runs, first on every page | 349 | /** The project, if `viewer` belongs to its workspace. */ |
| 350 | private async memberProject(ref: ProjectRef, viewer: Viewer): Promise<Result<Project>> { | |
| 351 | if (!isMember(viewer, ref.workspace)) return fail("forbidden", "Only members of the workspace can manage its deployments."); | |
| 352 | return this.projects.get(ref.workspace, ref.slug, viewer); | |
| Deployments: a preview for every pull request, production on g1t.page | 353 | } |
| 354 | ||
| 355 | // ---- Methods for the site and the API ------------------------------ | |
| 356 | ||
| Projects: what a workspace builds and runs, first on every page | 357 | async settings(a: { project: ProjectRef; viewer: Viewer }): Promise<Result<DeploySettings>> { |
| 358 | const project = await this.memberProject(a.project, a.viewer); | |
| 359 | if (!project.ok) return project; | |
| 360 | return ok(await this.toSettings(project.value, await this.settingsRow(project.value.id))); | |
| Deployments: a preview for every pull request, production on g1t.page | 361 | } |
| 362 | ||
| 363 | async updateSettings(a: { | |
| 364 | actor: User; | |
| Projects: what a workspace builds and runs, first on every page | 365 | project: ProjectRef; |
| Deployments: a preview for every pull request, production on g1t.page | 366 | changes: Partial<DeploySettings>; |
| 367 | }): Promise<Result<DeploySettings>> { | |
| Projects: what a workspace builds and runs, first on every page | 368 | const found = await this.memberProject(a.project, a.actor); |
| 369 | if (!found.ok) return found; | |
| 370 | const project = found.value; | |
| 371 | const before = await this.toSettings(project, await this.settingsRow(project.id)); | |
| Deployments: a preview for every pull request, production on g1t.page | 372 | const next = { ...before, ...a.changes }; |
| 373 | if (next.enabled && !before.enabled) { | |
| 374 | // Turning it on starts paid work: only with the workspace's plan. | |
| Projects: what a workspace builds and runs, first on every page | 375 | const plan = await billingClient(this.env.BILLING).hasFeature(project.workspace, "deployments"); |
| Deployments: a preview for every pull request, production on g1t.page | 376 | if (!plan.ok) return plan; |
| 377 | } | |
| 378 | const idleDays = Math.min(90, Math.max(1, Math.trunc(Number(next.idleDays) || 7))); | |
| 379 | const clip = (text: string | null | undefined) => (text?.trim() ? text.trim().slice(0, 500) : null); | |
| 380 | await this.db | |
| 381 | .prepare( | |
| Projects: what a workspace builds and runs, first on every page | 382 | `INSERT INTO settings (project_id, workspace, slug, repo_id, enabled, previews, production, build_command, |
| 383 | output_dir, idle_days, updated_by, updated_at) | |
| 384 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12) | |
| 385 | ON CONFLICT (project_id) DO UPDATE SET workspace = ?2, slug = ?3, repo_id = ?4, enabled = ?5, previews = ?6, | |
| 386 | production = ?7, build_command = ?8, output_dir = ?9, idle_days = ?10, updated_by = ?11, updated_at = ?12`, | |
| Deployments: a preview for every pull request, production on g1t.page | 387 | ) |
| 388 | .bind( | |
| Projects: what a workspace builds and runs, first on every page | 389 | project.id, |
| 390 | project.workspace, | |
| 391 | project.slug, | |
| 392 | repoOf(project).id, | |
| Deployments: a preview for every pull request, production on g1t.page | 393 | next.enabled ? 1 : 0, |
| 394 | next.previews ? 1 : 0, | |
| 395 | next.production ? 1 : 0, | |
| 396 | clip(next.buildCommand), | |
| 397 | clip(next.outputDir), | |
| 398 | idleDays, | |
| 399 | a.actor.username, | |
| 400 | now(), | |
| 401 | ) | |
| 402 | .run(); | |
| 403 | // What was turned off comes down now; nothing keeps running unasked. | |
| Projects: what a workspace builds and runs, first on every page | 404 | if (!next.enabled) await this.takeDownWhere(project.id, null); |
| Deployments: a preview for every pull request, production on g1t.page | 405 | else { |
| Projects: what a workspace builds and runs, first on every page | 406 | if (!next.previews) await this.takeDownWhere(project.id, "preview"); |
| 407 | if (!next.production) await this.takeDownWhere(project.id, "production"); | |
| Deployments: a preview for every pull request, production on g1t.page | 408 | } |
| 409 | // Turned on: production goes up from the default branch at once. | |
| 410 | if (next.enabled && next.production && (!before.enabled || !before.production)) { | |
| Projects: what a workspace builds and runs, first on every page | 411 | await this.deployProduction(project, null, a.actor.username); |
| Deployments: a preview for every pull request, production on g1t.page | 412 | } |
| Projects: what a workspace builds and runs, first on every page | 413 | return ok(await this.toSettings(project, await this.settingsRow(project.id))); |
| Deployments: a preview for every pull request, production on g1t.page | 414 | } |
| 415 | ||
| Projects: what a workspace builds and runs, first on every page | 416 | async list(a: { project: ProjectRef; viewer: Viewer }): Promise<Result<{ deployments: Deployment[]; live: LiveApp[] }>> { |
| 417 | const project = await this.memberProject(a.project, a.viewer); | |
| 418 | if (!project.ok) return project; | |
| Deployments: a preview for every pull request, production on g1t.page | 419 | const [deployments, apps] = await Promise.all([ |
| 420 | this.db | |
| Projects: what a workspace builds and runs, first on every page | 421 | .prepare("SELECT * FROM deployments WHERE project_id = ? ORDER BY id DESC LIMIT ?") |
| 422 | .bind(project.value.id, LIST_LIMIT) | |
| Deployments: a preview for every pull request, production on g1t.page | 423 | .all<DeploymentRow>(), |
| 424 | this.db | |
| Projects: what a workspace builds and runs, first on every page | 425 | .prepare("SELECT * FROM apps WHERE project_id = ? ORDER BY kind DESC, deployed_at DESC") |
| 426 | .bind(project.value.id) | |
| Deployments: a preview for every pull request, production on g1t.page | 427 | .all<AppRow>(), |
| 428 | ]); | |
| Projects: what a workspace builds and runs, first on every page | 429 | return ok({ deployments: deployments.results.map(toDeployment), live: apps.results.map(toLive) }); |
| Deployments: a preview for every pull request, production on g1t.page | 430 | } |
| 431 | ||
| Projects: what a workspace builds and runs, first on every page | 432 | async get(a: { project: ProjectRef; id: string; viewer: Viewer }): Promise<Result<Deployment & { log: string | null }>> { |
| 433 | const project = await this.memberProject(a.project, a.viewer); | |
| 434 | if (!project.ok) return project; | |
| Deployments: a preview for every pull request, production on g1t.page | 435 | const row = await this.db |
| Projects: what a workspace builds and runs, first on every page | 436 | .prepare("SELECT * FROM deployments WHERE id = ? AND project_id = ?") |
| 437 | .bind(a.id, project.value.id) | |
| Deployments: a preview for every pull request, production on g1t.page | 438 | .first<DeploymentRow>(); |
| 439 | if (!row) return fail("not_found", "No such deployment."); | |
| 440 | return ok({ ...toDeployment(row), log: row.log }); | |
| 441 | } | |
| 442 | ||
| Projects: what a workspace builds and runs, first on every page | 443 | async redeploy(a: { actor: User; project: ProjectRef; branch: string | null }): Promise<Result<Deployment>> { |
| 444 | const found = await this.memberProject(a.project, a.actor); | |
| 445 | if (!found.ok) return found; | |
| 446 | const project = found.value; | |
| 447 | const settings = await this.settingsRow(project.id); | |
| 448 | if (!settings?.enabled) return fail("conflict", "Deployments are off for this project."); | |
| 449 | if (a.branch == null) { | |
| 450 | return (await this.deployProduction(project, null, a.actor.username)) ?? fail("conflict", "There was nothing to deploy."); | |
| 451 | } | |
| 452 | // A branch's preview comes from its pull request. | |
| 453 | const app = await this.db | |
| 454 | .prepare("SELECT number FROM deployments WHERE project_id = ? AND branch = ? AND number IS NOT NULL ORDER BY id DESC") | |
| 455 | .bind(project.id, a.branch) | |
| 456 | .first<{ number: number }>(); | |
| 457 | if (!app) return fail("not_found", `No pull request has deployed ${a.branch}.`); | |
| 458 | return (await this.deployPreview(project, app.number, a.actor.username, true)) ?? fail("conflict", "Its pull request is not open."); | |
| Deployments: a preview for every pull request, production on g1t.page | 459 | } |
| 460 | ||
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 461 | /** |
| 462 | * A preview stack: the projects that use this one get previews of their | |
| 463 | * own default branch, under the same branch name, so each reaches this | |
| 464 | * branch's preview through its dependency's variable. A change to an API | |
| 465 | * can then be clicked through in the apps that call it. | |
| 466 | */ | |
| 467 | async stack( | |
| 468 | a: { actor: User; project: ProjectRef; branch: string }, | |
| 469 | background: (work: Promise<unknown>) => void, | |
| 470 | ): Promise<Result<string[]>> { | |
| 471 | const found = await this.memberProject(a.project, a.actor); | |
| 472 | if (!found.ok) return found; | |
| 473 | const upstream = await this.db | |
| 474 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = 'preview' AND branch = ?") | |
| 475 | .bind(found.value.id, a.branch) | |
| 476 | .first(); | |
| 477 | if (!upstream) return fail("conflict", `${a.branch} has no preview up to build against.`); | |
| 478 | const graph = await this.projects.graph(found.value.id); | |
| 479 | const ready: { project: Project; settings: SettingsRow }[] = []; | |
| 480 | for (const dependent of graph.usedBy) { | |
| 481 | const project = await this.projects.get(dependent.workspace, dependent.slug, a.actor); | |
| 482 | if (!project.ok) continue; | |
| 483 | const settings = await this.settingsRow(project.value.id); | |
| 484 | if (settings?.enabled && settings.previews) ready.push({ project: project.value, settings }); | |
| 485 | } | |
| 486 | if (ready.length === 0) return fail("conflict", "No project that uses this one has previews turned on."); | |
| 487 | // The builds start after the answer: a person moving on from the page | |
| 488 | // does not stop them. | |
| 489 | background( | |
| 490 | (async () => { | |
| 491 | for (const { project, settings } of ready) { | |
| 492 | const actor = await this.workspaceActor(project.workspace); | |
| 493 | if (!actor) continue; | |
| 494 | const repo = repoOf(project); | |
| 495 | const branches = await reposClient(this.env.REPOS).branches(repo.path, actor); | |
| 496 | const head = branches.ok ? branches.value.find((b) => b.name === repo.defaultBranch)?.hash : undefined; | |
| 497 | if (!head) continue; | |
| 498 | await this.start({ | |
| 499 | project, | |
| 500 | kind: "preview", | |
| 501 | branch: a.branch, | |
| 502 | number: null, | |
| 503 | commit: head, | |
| 504 | source: repo.path, | |
| 505 | reader: actor, | |
| 506 | createdBy: a.actor.username, | |
| 507 | settings, | |
| 508 | // Its own default branch, asked for by a member. | |
| 509 | trusted: true, | |
| 510 | }); | |
| 511 | } | |
| 512 | })().catch((error) => console.error("stack failed", a.project.slug, a.branch, error)), | |
| 513 | ); | |
| 514 | return ok(ready.map(({ project }) => project.name)); | |
| 515 | } | |
| 516 | ||
| Projects: what a workspace builds and runs, first on every page | 517 | async takeDown(a: { actor: User; project: ProjectRef; branch: string | null }): Promise<Result<true>> { |
| 518 | const project = await this.memberProject(a.project, a.actor); | |
| 519 | if (!project.ok) return project; | |
| 520 | await this.takeDownWhere(project.value.id, a.branch == null ? "production" : "preview", a.branch ?? undefined); | |
| Deployments: a preview for every pull request, production on g1t.page | 521 | return ok(true); |
| 522 | } | |
| 523 | ||
| Projects: what a workspace builds and runs, first on every page | 524 | async overview(a: { workspace: string; viewer: Viewer }): Promise<Result<ProjectDeploys[]>> { |
| 525 | const workspace = a.workspace.toLowerCase(); | |
| 526 | if (!isMember(a.viewer, workspace)) return fail("forbidden", "Only members can see a workspace's deployments."); | |
| 527 | const [settings, apps, latest] = await Promise.all([ | |
| 528 | this.db.prepare("SELECT slug, enabled FROM settings WHERE workspace = ?").bind(workspace).all<{ slug: string; enabled: number }>(), | |
| 529 | this.db.prepare("SELECT * FROM apps WHERE workspace = ?").bind(workspace).all<AppRow>(), | |
| 530 | this.db | |
| 531 | .prepare( | |
| 532 | `SELECT * FROM deployments WHERE id IN (SELECT MAX(id) FROM deployments WHERE workspace = ? GROUP BY project_id)`, | |
| 533 | ) | |
| 534 | .bind(workspace) | |
| 535 | .all<DeploymentRow>(), | |
| 536 | ]); | |
| 537 | return ok( | |
| 538 | settings.results.map((row) => { | |
| 539 | const own = apps.results.filter((app) => app.slug === row.slug); | |
| 540 | const production = own.find((app) => app.kind === "production"); | |
| 541 | const newest = latest.results.find((d) => d.slug === row.slug); | |
| 542 | return { | |
| 543 | slug: row.slug, | |
| 544 | enabled: !!row.enabled, | |
| 545 | production: production ? toLive(production) : null, | |
| 546 | previews: own.filter((app) => app.kind === "preview").length, | |
| 547 | latest: newest ? toDeployment(newest) : null, | |
| 548 | }; | |
| 549 | }), | |
| 550 | ); | |
| 551 | } | |
| 552 | ||
| Deployments: a preview for every pull request, production on g1t.page | 553 | async usage(a: { workspace: string; viewer: Viewer }): Promise<Result<DeployUsage>> { |
| 554 | const slug = a.workspace.toLowerCase(); | |
| 555 | if (!isMember(a.viewer, slug)) return fail("forbidden", "Only members can see a workspace's usage."); | |
| 556 | const [meter, apps] = await Promise.all([ | |
| 557 | this.db | |
| 558 | .prepare("SELECT * FROM meters WHERE namespace = ? AND month = ?") | |
| 559 | .bind(slug, month()) | |
| 560 | .first<{ | |
| 561 | requests: number; | |
| 562 | cpu_ms: number; | |
| 563 | peak_apps: number; | |
| 564 | build_seconds: number; | |
| 565 | build_micros: number; | |
| 566 | counted_at: string | null; | |
| 567 | }>(), | |
| Projects: what a workspace builds and runs, first on every page | 568 | this.db.prepare("SELECT COUNT(*) AS n FROM apps WHERE workspace = ?").bind(slug).first<{ n: number }>(), |
| Deployments: a preview for every pull request, production on g1t.page | 569 | ]); |
| 570 | return ok({ | |
| 571 | month: month(), | |
| 572 | requests: meter?.requests ?? 0, | |
| 573 | cpuMs: meter?.cpu_ms ?? 0, | |
| 574 | apps: apps?.n ?? 0, | |
| 575 | peakApps: Math.max(meter?.peak_apps ?? 0, apps?.n ?? 0), | |
| 576 | buildSeconds: meter?.build_seconds ?? 0, | |
| 577 | buildMicros: meter?.build_micros ?? 0, | |
| 578 | countedAt: meter?.counted_at ?? null, | |
| 579 | }); | |
| 580 | } | |
| 581 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 582 | // ---- Custom domains ------------------------------------------------ |
| 583 | ||
| 584 | async listDomains(a: { project: ProjectRef; viewer: Viewer }): Promise<Result<ProjectDomains>> { | |
| 585 | const project = await this.memberProject(a.project, a.viewer); | |
| 586 | if (!project.ok) return project; | |
| 587 | const domains = this.domains; | |
| 588 | await domains.catchUp(project.value.id).catch((error) => console.error("could not check domains", error)); | |
| 589 | const [rows, available, used] = await Promise.all([ | |
| 590 | domains.forProject(project.value.id), | |
| 591 | domains.available(), | |
| 592 | domains.countFor(project.value.workspace), | |
| 593 | ]); | |
| 594 | return ok({ | |
| 595 | domains: rows.map(toDomain), | |
| 596 | target: CUSTOM_DOMAIN_TARGET, | |
| 597 | available, | |
| 598 | notice: available ? null : NOT_ENABLED_NOTICE, | |
| 599 | included: DEPLOYMENTS_ALLOWANCE.customDomains, | |
| 600 | used, | |
| 601 | }); | |
| 602 | } | |
| 603 | ||
| 604 | async addDomain(a: { actor: User; project: ProjectRef; hostname: string; twin?: boolean }): Promise<Result<Domain[]>> { | |
| 605 | const found = await this.memberProject(a.project, a.actor); | |
| 606 | if (!found.ok) return found; | |
| 607 | const project = found.value; | |
| 608 | const plan = await billingClient(this.env.BILLING).hasFeature(project.workspace, "deployments"); | |
| 609 | if (!plan.ok) return plan; | |
| 610 | const added = await this.domains.add({ | |
| 611 | project: { id: project.id, workspace: project.workspace, slug: project.slug }, | |
| 612 | script: await this.productionScript(project), | |
| 613 | hostname: String(a.hostname ?? ""), | |
| 614 | twin: !!a.twin, | |
| 615 | by: a.actor.username, | |
| 616 | }); | |
| 617 | if (!added.ok) return fail(added.code, added.message); | |
| 618 | await this.notePeak(project.workspace); | |
| 619 | return ok(added.rows.map(toDomain)); | |
| 620 | } | |
| 621 | ||
| 622 | async removeDomain(a: { actor: User; project: ProjectRef; id: string }): Promise<Result<true>> { | |
| 623 | const found = await this.memberProject(a.project, a.actor); | |
| 624 | if (!found.ok) return found; | |
| 625 | const row = await this.domains.byId(found.value.id, String(a.id ?? "")); | |
| 626 | if (!row) return fail("not_found", "No such domain."); | |
| 627 | await this.domains.remove(row); | |
| 628 | return ok(true); | |
| 629 | } | |
| 630 | ||
| 631 | async refreshDomain(a: { actor: User; project: ProjectRef; id: string }): Promise<Result<Domain>> { | |
| 632 | const found = await this.memberProject(a.project, a.actor); | |
| 633 | if (!found.ok) return found; | |
| 634 | const domains = this.domains; | |
| 635 | const row = await domains.byId(found.value.id, String(a.id ?? "")); | |
| 636 | if (!row) return fail("not_found", "No such domain."); | |
| 637 | const after = await domains.refresh(row, row.script ?? (await this.productionScript(found.value)), true); | |
| 638 | await this.notePeak(found.value.workspace); | |
| 639 | return ok(toDomain(after)); | |
| 640 | } | |
| 641 | ||
| 642 | /** The script production is up under, or the name it will have. */ | |
| 643 | private async productionScript(project: Project): Promise<string> { | |
| 644 | const app = await this.db | |
| 645 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = 'production'") | |
| 646 | .bind(project.id) | |
| 647 | .first<{ script: string }>(); | |
| 648 | return app?.script ?? (await this.scriptFor(project, null)); | |
| 649 | } | |
| 650 | ||
| Deployments: a preview for every pull request, production on g1t.page | 651 | // ---- Starting builds ----------------------------------------------- |
| 652 | ||
| 653 | /** | |
| 654 | * Opens a deployment and starts its build. Skipped, with the reason | |
| 655 | * recorded, when the workspace's plan is off. | |
| 656 | */ | |
| 657 | private async start(input: { | |
| Projects: what a workspace builds and runs, first on every page | 658 | project: Project; |
| Deployments: a preview for every pull request, production on g1t.page | 659 | kind: DeployKind; |
| Projects: what a workspace builds and runs, first on every page | 660 | branch: string | null; |
| Deployments: a preview for every pull request, production on g1t.page | 661 | number: number | null; |
| 662 | commit: string; | |
| 663 | source: RepoPath; | |
| 664 | reader: User; | |
| 665 | createdBy: string; | |
| 666 | settings: SettingsRow; | |
| Projects: what a workspace builds and runs, first on every page | 667 | /** A push, or work by a member or an agent; see `insider`. */ |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 668 | trusted: boolean; |
| Deployments: a preview for every pull request, production on g1t.page | 669 | }): Promise<Result<Deployment>> { |
| Projects: what a workspace builds and runs, first on every page | 670 | const { project } = input; |
| 671 | const repo = repoOf(project); | |
| 672 | const script = await this.scriptFor(project, input.branch); | |
| Deployments: a preview for every pull request, production on g1t.page | 673 | const id = newId("dpl"); |
| 674 | const token = randomToken(); | |
| Projects: what a workspace builds and runs, first on every page | 675 | const plan = await billingClient(this.env.BILLING).hasFeature(project.workspace, "deployments"); |
| Usage limits: unpaid usage can only go so far | 676 | const limit = await billingClient(this.env.BILLING).checkLimit(project.workspace); |
| Deployments: a preview for every pull request, production on g1t.page | 677 | const cloudflare = this.cloudflare; |
| 678 | const refused = !plan.ok | |
| 679 | ? plan.error.message | |
| Usage limits: unpaid usage can only go so far | 680 | : limit.ok && limit.value.state === "stopped" |
| 681 | ? (limit.value.message ?? "The workspace reached its usage limit.") | |
| Deployments: a preview for every pull request, production on g1t.page | 682 | : !cloudflare |
| 683 | ? "Deployments are not set up on this g1t: it has no Cloudflare token." | |
| 684 | : null; | |
| 685 | await this.db | |
| 686 | .prepare( | |
| Projects: what a workspace builds and runs, first on every page | 687 | `INSERT INTO deployments (id, project_id, workspace, slug, repo_id, repo, kind, branch, number, commit_sha, |
| 688 | script, status, error, token_hash, trusted, created_by, created_at, finished_at) | |
| 689 | VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, | |
| Deployments: a preview for every pull request, production on g1t.page | 690 | ) |
| 691 | .bind( | |
| 692 | id, | |
| Projects: what a workspace builds and runs, first on every page | 693 | project.id, |
| 694 | project.workspace, | |
| 695 | project.slug, | |
| 696 | repo.id, | |
| 697 | `${repo.path.namespace}/${repo.path.name}`, | |
| Deployments: a preview for every pull request, production on g1t.page | 698 | input.kind, |
| Projects: what a workspace builds and runs, first on every page | 699 | input.branch, |
| Deployments: a preview for every pull request, production on g1t.page | 700 | input.number, |
| 701 | input.commit, | |
| 702 | script, | |
| 703 | refused ? "skipped" : "queued", | |
| 704 | refused, | |
| 705 | refused ? null : await sha256(token), | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 706 | input.trusted ? 1 : 0, |
| Deployments: a preview for every pull request, production on g1t.page | 707 | input.createdBy, |
| 708 | now(), | |
| 709 | refused ? now() : null, | |
| 710 | ) | |
| 711 | .run(); | |
| 712 | if (refused) return ok(toDeployment((await this.deploymentRow(id))!)); | |
| 713 | // Older builds of the same app are replaced by this one. | |
| 714 | await this.db | |
| 715 | .prepare( | |
| 716 | `UPDATE deployments SET status = 'skipped', error = 'A newer commit replaced this build.', finished_at = ? | |
| 717 | WHERE script = ? AND id != ? AND status IN ('queued', 'building')`, | |
| 718 | ) | |
| 719 | .bind(now(), script, id) | |
| 720 | .run(); | |
| Projects: what a workspace builds and runs, first on every page | 721 | await this.status(repo.id, input.commit, project, "pending", "Building", `${this.env.SITE}/${project.workspace}/${project.slug}/deployments/${id}`); |
| 722 | // What the project's secrets and variables give builds of this kind. | |
| 723 | const build = await this.resolve( | |
| 724 | { id: project.id, slug: project.slug, repoId: repo.id, repo: repo.path }, | |
| 725 | input.kind, | |
| 726 | input.trusted, | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 727 | input.branch, |
| Projects: what a workspace builds and runs, first on every page | 728 | ); |
| Deployments: a preview for every pull request, production on g1t.page | 729 | const response = await this.env.RUNNER.fetch("https://runner/rpc/start_deploy", { |
| 730 | method: "POST", | |
| 731 | headers: { "content-type": "application/json" }, | |
| 732 | body: JSON.stringify({ | |
| 733 | deployId: id, | |
| 734 | token, | |
| 735 | actor: input.reader, | |
| 736 | source: input.source, | |
| 737 | commit: input.commit, | |
| Projects: what a workspace builds and runs, first on every page | 738 | rootDir: project.source.rootDir, |
| Deployments: a preview for every pull request, production on g1t.page | 739 | buildCommand: input.settings.build_command, |
| 740 | outputDir: input.settings.output_dir, | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 741 | buildEnv: build.variables, |
| 742 | buildSecrets: build.secrets, | |
| Deployments: a preview for every pull request, production on g1t.page | 743 | }), |
| 744 | }); | |
| 745 | const started = response.ok ? ((await response.json()) as Result<true>) : fail("conflict", `The runner answered ${response.status}.`); | |
| 746 | if (!started.ok) await this.finishFailed(id, started.error.message, null, null); | |
| 747 | return ok(toDeployment((await this.deploymentRow(id))!)); | |
| 748 | } | |
| 749 | ||
| Projects: what a workspace builds and runs, first on every page | 750 | private async deployProduction(project: Project, commit: string | null, createdBy: string): Promise<Result<Deployment> | null> { |
| 751 | const settings = await this.settingsRow(project.id); | |
| Deployments: a preview for every pull request, production on g1t.page | 752 | if (!settings?.enabled || !settings.production) return null; |
| Projects: what a workspace builds and runs, first on every page | 753 | const actor = await this.workspaceActor(project.workspace); |
| Deployments: a preview for every pull request, production on g1t.page | 754 | if (!actor) return null; |
| Projects: what a workspace builds and runs, first on every page | 755 | const repo = repoOf(project); |
| Deployments: a preview for every pull request, production on g1t.page | 756 | let head = commit; |
| 757 | if (!head) { | |
| Projects: what a workspace builds and runs, first on every page | 758 | const branches = await reposClient(this.env.REPOS).branches(repo.path, actor); |
| 759 | head = branches.ok ? (branches.value.find((b) => b.name === repo.defaultBranch)?.hash ?? null) : null; | |
| Deployments: a preview for every pull request, production on g1t.page | 760 | } |
| 761 | if (!head) return null; | |
| 762 | return this.start({ | |
| Projects: what a workspace builds and runs, first on every page | 763 | project, |
| Deployments: a preview for every pull request, production on g1t.page | 764 | kind: "production", |
| Projects: what a workspace builds and runs, first on every page | 765 | branch: null, |
| Deployments: a preview for every pull request, production on g1t.page | 766 | number: null, |
| 767 | commit: head, | |
| Projects: what a workspace builds and runs, first on every page | 768 | source: repo.path, |
| Deployments: a preview for every pull request, production on g1t.page | 769 | reader: actor, |
| 770 | createdBy, | |
| 771 | settings, | |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 772 | // The default branch only moves by people and agents with access. |
| 773 | trusted: true, | |
| Deployments: a preview for every pull request, production on g1t.page | 774 | }); |
| 775 | } | |
| 776 | ||
| Projects: what a workspace builds and runs, first on every page | 777 | private async deployPreview(project: Project, number: number, createdBy: string, force = false): Promise<Result<Deployment> | null> { |
| 778 | const settings = await this.settingsRow(project.id); | |
| Deployments: a preview for every pull request, production on g1t.page | 779 | if (!settings?.enabled || !settings.previews) return null; |
| Projects: what a workspace builds and runs, first on every page | 780 | const actor = await this.workspaceActor(project.workspace); |
| Deployments: a preview for every pull request, production on g1t.page | 781 | if (!actor) return null; |
| Projects: what a workspace builds and runs, first on every page | 782 | const repo = repoOf(project); |
| 783 | const detail = await workClient(this.env.WORK).getPull(repo.path, number, actor); | |
| Deployments: a preview for every pull request, production on g1t.page | 784 | if (!detail.ok) return null; |
| 785 | const { pull } = detail.value; | |
| 786 | if ((pull.status !== "open" && pull.status !== "draft") || !pull.headCommit) return null; | |
| Projects: what a workspace builds and runs, first on every page | 787 | // A pull request from a fork (as g1t's agents work) has no branch here. |
| 788 | const branch = pull.branch ?? `pr-${number}`; | |
| Deployments: a preview for every pull request, production on g1t.page | 789 | if (!force) { |
| 790 | // Already built, or being built, at this commit. | |
| 791 | const same = await this.db | |
| 792 | .prepare( | |
| Projects: what a workspace builds and runs, first on every page | 793 | `SELECT id FROM deployments WHERE project_id = ? AND kind = 'preview' AND branch = ? AND commit_sha = ? |
| Deployments: a preview for every pull request, production on g1t.page | 794 | AND status IN ('queued', 'building', 'ready')`, |
| 795 | ) | |
| Projects: what a workspace builds and runs, first on every page | 796 | .bind(project.id, branch, pull.headCommit) |
| Deployments: a preview for every pull request, production on g1t.page | 797 | .first(); |
| 798 | if (same) return null; | |
| 799 | } | |
| 800 | return this.start({ | |
| Projects: what a workspace builds and runs, first on every page | 801 | project, |
| Deployments: a preview for every pull request, production on g1t.page | 802 | kind: "preview", |
| Projects: what a workspace builds and runs, first on every page | 803 | branch, |
| Deployments: a preview for every pull request, production on g1t.page | 804 | number, |
| 805 | commit: pull.headCommit, | |
| Projects: what a workspace builds and runs, first on every page | 806 | source: pull.fork ?? repo.path, |
| Deployments: a preview for every pull request, production on g1t.page | 807 | // The pull request's fork may be private: read it as its author. |
| 808 | reader: pull.author, | |
| 809 | createdBy, | |
| 810 | settings, | |
| Projects: what a workspace builds and runs, first on every page | 811 | trusted: await this.insider(repo.path, pull.author, actor), |
| Deployments: a preview for every pull request, production on g1t.page | 812 | }); |
| 813 | } | |
| 814 | ||
| 815 | // ---- A build's reports --------------------------------------------- | |
| 816 | ||
| 817 | private async deploymentRow(id: string): Promise<DeploymentRow | null> { | |
| 818 | return this.db.prepare("SELECT * FROM deployments WHERE id = ?").bind(id).first<DeploymentRow>(); | |
| 819 | } | |
| 820 | ||
| 821 | /** The build, if `token` is its own and it is still under way. */ | |
| 822 | private async building(id: string, token: unknown): Promise<DeploymentRow | null> { | |
| 823 | const row = await this.deploymentRow(id); | |
| 824 | if (!row?.token_hash || typeof token !== "string") return null; | |
| 825 | if (row.token_hash !== (await sha256(token))) return null; | |
| 826 | return row.status === "queued" || row.status === "building" ? row : null; | |
| 827 | } | |
| 828 | ||
| 829 | async job(id: string, step: string, body: Record<string, unknown>): Promise<Response> { | |
| Deployments work end to end: fixes from the first live run | 830 | // Each report, for the logs: a build's own failure says why. |
| 831 | console.log("build", id, step, typeof body.message === "string" ? body.message.slice(0, 500) : ""); | |
| Deployments: a preview for every pull request, production on g1t.page | 832 | const row = await this.building(id, body.token); |
| 833 | if (!row) return Response.json(fail("not_found", "No such build, or it has finished."), { status: 404 }); | |
| 834 | const cloudflare = this.cloudflare; | |
| 835 | if (!cloudflare) return Response.json(fail("conflict", "Deployments are not set up."), { status: 409 }); | |
| 836 | switch (step) { | |
| 837 | case "started": | |
| 838 | await this.db | |
| 839 | .prepare("UPDATE deployments SET status = 'building', started_at = ? WHERE id = ?") | |
| 840 | .bind(now(), id) | |
| 841 | .run(); | |
| 842 | return Response.json(ok(true)); | |
| 843 | case "session": { | |
| 844 | const manifest = body.manifest as Manifest | undefined; | |
| 845 | if (!manifest || typeof manifest !== "object") return Response.json(fail("invalid", "No manifest."), { status: 400 }); | |
| 846 | const session = await cloudflare.openUpload(row.script, manifest); | |
| 847 | return Response.json(ok({ ...session, uploadUrl: cloudflare.uploadUrl })); | |
| 848 | } | |
| 849 | case "finish": { | |
| 850 | const worker = (body.worker ?? {}) as BuiltWorker; | |
| 851 | const seconds = Number(body.buildSeconds) || 0; | |
| Projects: what a workspace builds and runs, first on every page | 852 | const [namespace, name] = row.repo.split("/") as [string, string]; |
| Deployments: a preview for every pull request, production on g1t.page | 853 | try { |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 854 | // Running apps' secrets and variables are bound here, by g1t: |
| 855 | // they never pass through the build's sandbox. | |
| Projects: what a workspace builds and runs, first on every page | 856 | const runtime = await this.resolve( |
| 857 | { id: row.project_id, slug: row.slug, repoId: row.repo_id, repo: { namespace, name } }, | |
| 858 | row.kind, | |
| 859 | !!row.trusted, | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 860 | row.branch, |
| Projects: what a workspace builds and runs, first on every page | 861 | ); |
| Deployments: a preview for every pull request, production on g1t.page | 862 | await cloudflare.putScript( |
| 863 | row.script, | |
| 864 | worker, | |
| 865 | typeof body.completionJwt === "string" ? body.completionJwt : null, | |
| Projects: what a workspace builds and runs, first on every page | 866 | [`workspace:${row.workspace}`, `project:${row.workspace}/${row.slug}`, row.kind], |
| Secrets and variables: one list, rows per environment, for workflows and deployments | 867 | runtime, |
| Deployments: a preview for every pull request, production on g1t.page | 868 | ); |
| 869 | } catch (error) { | |
| 870 | await this.finishFailed(id, `Cloudflare did not take the app: ${String(error).replace(/^Error: /, "")}`, String(body.log ?? ""), seconds); | |
| 871 | return Response.json(ok(false)); | |
| 872 | } | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 873 | // The same app at an older name (its workspace was renamed) now |
| 874 | // redirects here; it stays up as it was if the redirect cannot be put. | |
| 875 | const redirected = await this.supersede(cloudflare, row); | |
| Deployments: a preview for every pull request, production on g1t.page | 876 | const at = now(); |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 877 | const expires = new Date(Date.parse(at) + SLUG_HOLD_DAYS * 24 * 60 * 60 * 1000).toISOString(); |
| Deployments: a preview for every pull request, production on g1t.page | 878 | await this.db.batch([ |
| 879 | this.db | |
| 880 | .prepare( | |
| 881 | `UPDATE deployments SET status = 'ready', warnings = ?, log = ?, build_seconds = ?, finished_at = ? | |
| 882 | WHERE id = ?`, | |
| 883 | ) | |
| 884 | .bind(JSON.stringify(Array.isArray(body.warnings) ? body.warnings : []), String(body.log ?? ""), seconds, at, id), | |
| 885 | this.db | |
| 886 | .prepare( | |
| Projects: what a workspace builds and runs, first on every page | 887 | `INSERT INTO apps (script, project_id, workspace, slug, kind, branch, number, commit_sha, deployed_at, created_at) |
| 888 | VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?9) | |
| Usage limits: unpaid usage can only go so far | 889 | ON CONFLICT (script) DO UPDATE SET commit_sha = ?8, number = ?7, deployed_at = ?9, paused_at = NULL`, |
| Deployments: a preview for every pull request, production on g1t.page | 890 | ) |
| Projects: what a workspace builds and runs, first on every page | 891 | .bind(row.script, row.project_id, row.workspace, row.slug, row.kind, row.branch, row.number, row.commit_sha, at), |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 892 | // A name that redirected elsewhere (a rename undone) is an app again. |
| 893 | this.db.prepare("DELETE FROM redirects WHERE script = ?").bind(row.script), | |
| 894 | ...redirected.flatMap((old) => [ | |
| 895 | this.db.prepare("DELETE FROM apps WHERE script = ?").bind(old), | |
| 896 | this.db | |
| 897 | .prepare( | |
| 898 | `INSERT INTO redirects (script, target, workspace, created_at, expires_at) VALUES (?1, ?2, ?3, ?4, ?5) | |
| 899 | ON CONFLICT (script) DO UPDATE SET target = ?2, workspace = ?3, created_at = ?4, expires_at = ?5`, | |
| 900 | ) | |
| 901 | .bind(old, appHost(row.script), row.workspace, at, expires), | |
| 902 | ]), | |
| Deployments: a preview for every pull request, production on g1t.page | 903 | ]); |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 904 | // The project's own domains serve production wherever it is up. |
| 905 | if (row.kind === "production") { | |
| 906 | await this.domains.follow(row.project_id, row.script).catch((error) => console.error("could not point domains", error)); | |
| 907 | } | |
| Deployments: a preview for every pull request, production on g1t.page | 908 | await this.chargeBuild(row, seconds); |
| Projects: what a workspace builds and runs, first on every page | 909 | await this.notePeak(row.workspace); |
| 910 | await this.statusFor(row, "success", row.kind === "preview" ? "Preview is live" : "Production is live", appUrl(row.script)); | |
| Deployments: a preview for every pull request, production on g1t.page | 911 | return Response.json(ok(true)); |
| 912 | } | |
| 913 | case "fail": | |
| 914 | await this.finishFailed(id, String(body.message ?? "The build failed."), body.log == null ? null : String(body.log), Number(body.buildSeconds) || null); | |
| 915 | return Response.json(ok(true)); | |
| 916 | default: | |
| 917 | return Response.json(fail("not_found", "No such step."), { status: 404 }); | |
| 918 | } | |
| 919 | } | |
| 920 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 921 | /** |
| 922 | * Older names of the app `row` just put up: one project's production, or | |
| 923 | * its preview of one branch, has one name, so any other app row for the | |
| 924 | * same is the app under the name it had before its workspace was renamed. | |
| 925 | * Each is replaced in the namespace by a redirect to the new name; the | |
| 926 | * names that were are returned, for their app rows to go. | |
| 927 | */ | |
| 928 | private async supersede(cloudflare: Cloudflare, row: DeploymentRow): Promise<string[]> { | |
| 929 | const older = await this.db | |
| 930 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = ? AND branch IS ? AND script != ?") | |
| 931 | .bind(row.project_id, row.kind, row.branch, row.script) | |
| 932 | .all<{ script: string }>(); | |
| 933 | const done: string[] = []; | |
| 934 | for (const { script } of older.results) { | |
| 935 | try { | |
| 936 | await cloudflare.redirectScript(script, appHost(row.script)); | |
| 937 | done.push(script); | |
| 938 | } catch (error) { | |
| 939 | // Left as it is, the next deploy of this app tries again. | |
| 940 | console.error("could not redirect", script, "to", row.script, error); | |
| 941 | } | |
| 942 | } | |
| 943 | return done; | |
| 944 | } | |
| 945 | ||
| Deployments: a preview for every pull request, production on g1t.page | 946 | private async finishFailed(id: string, message: string, log: string | null, seconds: number | null): Promise<void> { |
| 947 | const row = await this.deploymentRow(id); | |
| 948 | if (!row || (row.status !== "queued" && row.status !== "building")) return; | |
| 949 | await this.db | |
| 950 | .prepare( | |
| 951 | `UPDATE deployments SET status = 'failed', error = ?, log = COALESCE(?, log), build_seconds = ?, finished_at = ? | |
| 952 | WHERE id = ?`, | |
| 953 | ) | |
| 954 | .bind(message.slice(0, 2000), log, seconds, now(), id) | |
| 955 | .run(); | |
| 956 | // A failed build still used its sandbox. | |
| 957 | if (seconds) await this.chargeBuild(row, seconds); | |
| Projects: what a workspace builds and runs, first on every page | 958 | await this.statusFor(row, "failure", "Deployment failed", `${this.env.SITE}/${row.workspace}/${row.slug}/deployments/${id}`); |
| Deployments: a preview for every pull request, production on g1t.page | 959 | } |
| 960 | ||
| 961 | /** Each build is charged by the second at the container price plus the margin. */ | |
| 962 | private async chargeBuild(row: DeploymentRow, seconds: number): Promise<void> { | |
| Prices keep themselves current with what g1t pays | 963 | const costs = await this.costs(); |
| 964 | const cost = Math.ceil(Math.ceil(seconds) * costs.buildSecond); | |
| Deployments: a preview for every pull request, production on g1t.page | 965 | if (cost <= 0) return; |
| Projects: what a workspace builds and runs, first on every page | 966 | const what = |
| 967 | row.kind === "preview" | |
| 968 | ? `the ${row.branch} preview of ${row.workspace}/${row.slug}` | |
| 969 | : `${row.workspace}/${row.slug} to production`; | |
| Deployments: a preview for every pull request, production on g1t.page | 970 | await billingClient(this.env.BILLING).chargeFeature({ |
| Projects: what a workspace builds and runs, first on every page | 971 | workspace: row.workspace, |
| Deployments: a preview for every pull request, production on g1t.page | 972 | feature: "deployments", |
| 973 | costMicros: cost, | |
| 974 | description: `Building ${what} (${Math.ceil(seconds)} s)`, | |
| Projects: what a workspace builds and runs, first on every page | 975 | repo: row.repo, |
| Deployments: a preview for every pull request, production on g1t.page | 976 | reference: `deploy/${row.id}`, |
| 977 | }); | |
| 978 | await this.db | |
| 979 | .prepare( | |
| 980 | `INSERT INTO meters (namespace, month, build_seconds, build_micros) VALUES (?1, ?2, ?3, ?4) | |
| 981 | ON CONFLICT (namespace, month) DO UPDATE SET build_seconds = build_seconds + ?3, build_micros = build_micros + ?4`, | |
| 982 | ) | |
| Projects: what a workspace builds and runs, first on every page | 983 | .bind(row.workspace, month(), Math.ceil(seconds), cost) |
| Deployments: a preview for every pull request, production on g1t.page | 984 | .run(); |
| 985 | } | |
| 986 | ||
| Prices keep themselves current with what g1t pays | 987 | /** |
| 988 | * What each unit costs g1t now, from billing's price book, which follows | |
| 989 | * what Cloudflare bills. The plan's figures if billing cannot say. | |
| 990 | */ | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 991 | private async costs(): Promise<{ |
| 992 | buildSecond: number; | |
| 993 | millionRequests: number; | |
| 994 | millionCpuMs: number; | |
| 995 | appMonth: number; | |
| 996 | domainMonth: number; | |
| 997 | }> { | |
| Prices keep themselves current with what g1t pays | 998 | const a = DEPLOYMENTS_ALLOWANCE; |
| 999 | const book = await billingClient(this.env.BILLING) | |
| 1000 | .prices() | |
| 1001 | .catch(() => null); | |
| 1002 | const cost = (meter: string, fallback: number) => book?.prices.find((p) => p.meter === meter)?.costMicros ?? fallback; | |
| 1003 | return { | |
| 1004 | buildSecond: cost("build_second", a.microsPerBuildSecond), | |
| 1005 | millionRequests: cost("app_requests", a.microsPerMillionRequests), | |
| 1006 | millionCpuMs: cost("app_cpu", a.microsPerMillionCpuMs), | |
| 1007 | appMonth: cost("app_month", a.microsPerAppMonth), | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1008 | domainMonth: cost("custom_domain_month", a.microsPerDomainMonth), |
| Prices keep themselves current with what g1t pays | 1009 | }; |
| 1010 | } | |
| 1011 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1012 | /** Remembers the most apps, and custom domains, the workspace had at once this month. */ |
| Projects: what a workspace builds and runs, first on every page | 1013 | private async notePeak(workspace: string): Promise<void> { |
| Deployments: a preview for every pull request, production on g1t.page | 1014 | await this.db |
| 1015 | .prepare( | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1016 | `INSERT INTO meters (namespace, month, peak_apps, peak_domains) |
| 1017 | VALUES (?1, ?2, (SELECT COUNT(*) FROM apps WHERE workspace = ?1), | |
| 1018 | (SELECT COUNT(*) FROM domains WHERE workspace = ?1 AND cf_hostname_id IS NOT NULL)) | |
| Deployments: a preview for every pull request, production on g1t.page | 1019 | ON CONFLICT (namespace, month) DO UPDATE SET |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1020 | peak_apps = MAX(peak_apps, (SELECT COUNT(*) FROM apps WHERE workspace = ?1)), |
| 1021 | peak_domains = MAX(peak_domains, (SELECT COUNT(*) FROM domains WHERE workspace = ?1 AND cf_hostname_id IS NOT NULL))`, | |
| Deployments: a preview for every pull request, production on g1t.page | 1022 | ) |
| Projects: what a workspace builds and runs, first on every page | 1023 | .bind(workspace, month()) |
| Deployments: a preview for every pull request, production on g1t.page | 1024 | .run(); |
| 1025 | } | |
| 1026 | ||
| Projects: what a workspace builds and runs, first on every page | 1027 | /** |
| 1028 | * The check on the commit: `g1t / deploy`, or, for one of several | |
| 1029 | * projects on a repository, `g1t / deploy (<project>)`. | |
| 1030 | */ | |
| 1031 | private async status( | |
| 1032 | repoId: string, | |
| 1033 | sha: string, | |
| 1034 | project: { slug: string; primary: boolean }, | |
| 1035 | state: string, | |
| 1036 | description: string, | |
| 1037 | targetUrl: string, | |
| 1038 | ): Promise<void> { | |
| 1039 | const context = project.primary ? STATUS_CONTEXT : `${STATUS_CONTEXT} (${project.slug})`; | |
| Deployments: a preview for every pull request, production on g1t.page | 1040 | await this.env.WORK.fetch("https://work/rpc/set_commit_status", { |
| 1041 | method: "POST", | |
| 1042 | headers: { "content-type": "application/json" }, | |
| Projects: what a workspace builds and runs, first on every page | 1043 | body: JSON.stringify({ repoId, sha, context, state, description, targetUrl }), |
| Deployments: a preview for every pull request, production on g1t.page | 1044 | }).catch(() => undefined); |
| 1045 | } | |
| 1046 | ||
| Projects: what a workspace builds and runs, first on every page | 1047 | private async statusFor(row: DeploymentRow, state: string, description: string, targetUrl: string): Promise<void> { |
| 1048 | const projects = await this.projects.byRepo(row.repo_id); | |
| 1049 | const primary = projects.find((p) => p.id === row.project_id)?.primary ?? true; | |
| 1050 | await this.status(row.repo_id, row.commit_sha, { slug: row.slug, primary }, state, description, targetUrl); | |
| 1051 | } | |
| 1052 | ||
| Deployments: a preview for every pull request, production on g1t.page | 1053 | // ---- Taking apps down ---------------------------------------------- |
| 1054 | ||
| 1055 | private async removeApp(script: string): Promise<void> { | |
| 1056 | await this.cloudflare?.deleteScript(script); | |
| 1057 | await this.db.prepare("DELETE FROM apps WHERE script = ?").bind(script).run(); | |
| 1058 | } | |
| 1059 | ||
| Projects: what a workspace builds and runs, first on every page | 1060 | private async takeDownWhere(projectId: string, kind: DeployKind | null, branch?: string): Promise<void> { |
| Deployments: a preview for every pull request, production on g1t.page | 1061 | const apps = await this.db |
| 1062 | .prepare( | |
| Projects: what a workspace builds and runs, first on every page | 1063 | `SELECT script FROM apps WHERE project_id = ?1 AND (?2 IS NULL OR kind = ?2) AND (?3 IS NULL OR branch = ?3)`, |
| Deployments: a preview for every pull request, production on g1t.page | 1064 | ) |
| Projects: what a workspace builds and runs, first on every page | 1065 | .bind(projectId, kind, branch ?? null) |
| Deployments: a preview for every pull request, production on g1t.page | 1066 | .all<{ script: string }>(); |
| 1067 | for (const app of apps.results) await this.removeApp(app.script); | |
| 1068 | } | |
| 1069 | ||
| 1070 | // ---- Events -------------------------------------------------------- | |
| 1071 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1072 | /** `attempts`: which delivery of the event this is, from 1. */ |
| 1073 | async onEvent(event: G1tEvent, attempts = 1): Promise<void> { | |
| Deployments: a preview for every pull request, production on g1t.page | 1074 | switch (event.type) { |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1075 | case "workspace.renamed": |
| 1076 | await this.renamed(event.data, attempts); | |
| 1077 | break; | |
| Deployments: a preview for every pull request, production on g1t.page | 1078 | case "pull.opened": |
| 1079 | case "pull.ready": | |
| Projects: what a workspace builds and runs, first on every page | 1080 | case "pull.updated": |
| 1081 | for (const project of await this.projects.byRepo(event.data.repoId)) { | |
| 1082 | await this.deployPreview(project, event.data.number, "g1t"); | |
| 1083 | } | |
| Deployments: a preview for every pull request, production on g1t.page | 1084 | break; |
| 1085 | case "pull.closed": | |
| 1086 | case "pull.merged": | |
| Projects: what a workspace builds and runs, first on every page | 1087 | for (const project of await this.projects.byRepo(event.data.repoId)) { |
| 1088 | const apps = await this.db | |
| 1089 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = 'preview' AND number = ?") | |
| 1090 | .bind(project.id, event.data.number) | |
| 1091 | .all<{ script: string }>(); | |
| 1092 | for (const app of apps.results) await this.removeApp(app.script); | |
| 1093 | } | |
| Deployments: a preview for every pull request, production on g1t.page | 1094 | break; |
| Projects: what a workspace builds and runs, first on every page | 1095 | case "git.push": |
| Deployments: a preview for every pull request, production on g1t.page | 1096 | if (!event.data.defaultBranch) break; |
| Projects: what a workspace builds and runs, first on every page | 1097 | for (const project of await this.projects.byRepo(event.data.repoId)) { |
| 1098 | await this.deployProduction(project, event.data.after, event.actor ?? "g1t"); | |
| 1099 | } | |
| Deployments: a preview for every pull request, production on g1t.page | 1100 | break; |
| 1101 | } | |
| 1102 | } | |
| 1103 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1104 | /** |
| 1105 | * A workspace's slug changed, and with it every app's name: production | |
| 1106 | * at `<project>-<workspace>`, previews at `<project>-git-<branch>-<workspace>`. | |
| 1107 | * | |
| 1108 | * Its rows move to the slug it has now (asked of identity, so a delivery | |
| 1109 | * twice over, or an older rename after a newer one, ends the same), and | |
| 1110 | * each app that is up is built again from the same commit under its new | |
| 1111 | * name. The old name keeps serving the app until the new one is live; | |
| 1112 | * then the build's finish puts a redirect to the new name in its place | |
| 1113 | * (see `supersede`), held for as long as the workspace holds its old slug. | |
| 1114 | * | |
| 1115 | * Paused apps are left: they are rebuilt, under the new name, when the | |
| 1116 | * workspace is under its limit again, and the old name redirects then. | |
| 1117 | * Builds under way for the old name are dropped and started again under | |
| 1118 | * the new one. Only rows still under an old slug are acted on, so a | |
| 1119 | * second delivery builds nothing. | |
| 1120 | */ | |
| 1121 | private async renamed(renamed: { workspaceId: string; from: string; to: string }, attempts: number): Promise<void> { | |
| 1122 | const current = await currentWorkspaceSlug(this.env.IDENTITY, renamed); | |
| 1123 | const stale = staleSlugs(renamed, current); | |
| 1124 | if (stale.length === 0) return; | |
| 1125 | const marks = stale.map(() => "?").join(", "); | |
| 1126 | ||
| 1127 | // What to build again, read before the rows move. | |
| 1128 | const [apps, building] = await Promise.all([ | |
| 1129 | this.db | |
| 1130 | .prepare(`SELECT * FROM apps WHERE workspace IN (${marks}) AND paused_at IS NULL`) | |
| 1131 | .bind(...stale) | |
| 1132 | .all<AppRow>(), | |
| 1133 | this.db | |
| 1134 | .prepare(`SELECT * FROM deployments WHERE workspace IN (${marks}) AND status IN ('queued', 'building') ORDER BY id`) | |
| 1135 | .bind(...stale) | |
| 1136 | .all<DeploymentRow>(), | |
| 1137 | ]); | |
| 1138 | type Target = { projectId: string; kind: DeployKind; branch: string | null; number: number | null; commit: string }; | |
| 1139 | const targets = new Map<string, Target>(); | |
| 1140 | const key = (t: { project_id: string; kind: DeployKind; branch: string | null }) => `${t.project_id}/${t.kind}/${t.branch ?? ""}`; | |
| 1141 | for (const app of apps.results) { | |
| 1142 | targets.set(key(app), { projectId: app.project_id, kind: app.kind, branch: app.branch, number: app.number, commit: app.commit_sha }); | |
| 1143 | } | |
| 1144 | // A build under way is newer than what is up. | |
| 1145 | for (const row of building.results) { | |
| 1146 | targets.set(key(row), { projectId: row.project_id, kind: row.kind, branch: row.branch, number: row.number, commit: row.commit_sha }); | |
| 1147 | } | |
| 1148 | ||
| 1149 | // The projects, as the projects service has them now. | |
| 1150 | const projectIds = [...new Set([...targets.values()].map((t) => t.projectId))]; | |
| 1151 | const projects = new Map<string, Project>(); | |
| 1152 | if (projectIds.length > 0) { | |
| 1153 | const repoIds = await this.db | |
| 1154 | .prepare(`SELECT DISTINCT repo_id FROM settings WHERE project_id IN (${projectIds.map(() => "?").join(", ")})`) | |
| 1155 | .bind(...projectIds) | |
| 1156 | .all<{ repo_id: string }>(); | |
| 1157 | for (const { repo_id } of repoIds.results) { | |
| 1158 | for (const project of await this.projects.byRepo(repo_id)) { | |
| 1159 | if (projectIds.includes(project.id)) projects.set(project.id, project); | |
| 1160 | } | |
| 1161 | } | |
| 1162 | // The projects service hears of the rename on its own queue: wait for | |
| 1163 | // it a few deliveries, so the builds read the repository by its new name. | |
| 1164 | const behind = [...projects.values()].some((p) => p.workspace !== current); | |
| 1165 | if (behind && attempts < RENAME_WAITS) throw new Error(`projects has not seen ${renamed.from} renamed to ${current} yet`); | |
| 1166 | } | |
| 1167 | ||
| 1168 | // Every row moves at once. | |
| 1169 | const at = now(); | |
| 1170 | const statements: D1PreparedStatement[] = []; | |
| 1171 | for (const slug of stale) { | |
| 1172 | statements.push( | |
| 1173 | this.db | |
| 1174 | .prepare( | |
| 1175 | `UPDATE deployments SET status = 'skipped', error = 'The workspace was renamed: built again under its new name.', | |
| 1176 | finished_at = ? WHERE workspace = ? AND status IN ('queued', 'building')`, | |
| 1177 | ) | |
| 1178 | .bind(at, slug), | |
| 1179 | this.db.prepare("UPDATE settings SET workspace = ?1 WHERE workspace = ?2").bind(current, slug), | |
| 1180 | this.db.prepare("UPDATE apps SET workspace = ?1 WHERE workspace = ?2").bind(current, slug), | |
| 1181 | this.db | |
| 1182 | .prepare( | |
| 1183 | `UPDATE deployments SET workspace = ?1, | |
| 1184 | repo = CASE WHEN substr(repo, 1, length(?2) + 1) = ?2 || '/' THEN ?1 || substr(repo, length(?2) + 1) ELSE repo END | |
| 1185 | WHERE workspace = ?2`, | |
| 1186 | ) | |
| 1187 | .bind(current, slug), | |
| 1188 | this.db.prepare("UPDATE redirects SET workspace = ?1 WHERE workspace = ?2").bind(current, slug), | |
| 1189 | this.domains.rename(slug, current), | |
| 1190 | // Counters add up; the peak is the higher; a month charged stays charged. | |
| 1191 | this.db | |
| 1192 | .prepare( | |
| 1193 | `INSERT INTO meters (namespace, month, requests, cpu_ms, peak_apps, peak_domains, build_seconds, build_micros, counted_at, charged_at) | |
| 1194 | SELECT ?1, month, requests, cpu_ms, peak_apps, peak_domains, build_seconds, build_micros, counted_at, charged_at | |
| 1195 | FROM meters WHERE namespace = ?2 | |
| 1196 | ON CONFLICT (namespace, month) DO UPDATE SET | |
| 1197 | requests = requests + excluded.requests, | |
| 1198 | cpu_ms = cpu_ms + excluded.cpu_ms, | |
| 1199 | peak_apps = MAX(peak_apps, excluded.peak_apps), | |
| 1200 | peak_domains = MAX(peak_domains, excluded.peak_domains), | |
| 1201 | build_seconds = build_seconds + excluded.build_seconds, | |
| 1202 | build_micros = build_micros + excluded.build_micros, | |
| 1203 | counted_at = COALESCE(MAX(counted_at, excluded.counted_at), counted_at, excluded.counted_at), | |
| 1204 | charged_at = COALESCE(charged_at, excluded.charged_at)`, | |
| 1205 | ) | |
| 1206 | .bind(current, slug), | |
| 1207 | this.db.prepare("DELETE FROM meters WHERE namespace = ?").bind(slug), | |
| 1208 | ); | |
| 1209 | } | |
| 1210 | await this.db.batch(statements); | |
| 1211 | ||
| 1212 | // Each app again, under its new name. Its dependencies' addresses are | |
| 1213 | // read again too, so apps that call one another follow the rename. | |
| 1214 | // Failures are logged, not retried: the rows have moved, and the next | |
| 1215 | // deploy of the app puts it under its new name all the same. | |
| 1216 | const actor = await this.workspaceActor(current); | |
| 1217 | for (const target of targets.values()) { | |
| 1218 | const found = projects.get(target.projectId); | |
| 1219 | if (!found) continue; | |
| 1220 | const project = this.underSlug(found, current, stale); | |
| 1221 | try { | |
| 1222 | const started = | |
| 1223 | target.kind === "production" | |
| 1224 | ? await this.deployProduction(project, target.commit, "g1t") | |
| 1225 | : target.number != null | |
| 1226 | ? await this.deployPreview(project, target.number, "g1t", true) | |
| 1227 | : await this.rebuildStack(project, target.branch, target.commit, actor); | |
| 1228 | if (started && !started.ok) console.log("could not rebuild", project.slug, target.branch, started.error.message); | |
| 1229 | } catch (error) { | |
| 1230 | console.error("could not rebuild after rename", project.slug, target.branch, error); | |
| 1231 | } | |
| 1232 | } | |
| 1233 | } | |
| 1234 | ||
| 1235 | /** `project` under the workspace's slug now, whether or not projects has caught up. */ | |
| 1236 | private underSlug(project: Project, current: string, stale: string[]): Project { | |
| 1237 | const source = | |
| 1238 | project.source.kind === "hosted" && stale.includes(project.source.repo.namespace) | |
| 1239 | ? { ...project.source, repo: { ...project.source.repo, namespace: current } } | |
| 1240 | : project.source; | |
| 1241 | return { ...project, workspace: current, source }; | |
| 1242 | } | |
| 1243 | ||
| 1244 | /** A stack's preview (no pull request of its own) built again at `commit`. */ | |
| 1245 | private async rebuildStack(project: Project, branch: string | null, commit: string, actor: User | null): Promise<Result<Deployment> | null> { | |
| 1246 | if (!actor || branch == null) return null; | |
| 1247 | const settings = await this.settingsRow(project.id); | |
| 1248 | if (!settings?.enabled || !settings.previews) return null; | |
| 1249 | return this.start({ | |
| 1250 | project, | |
| 1251 | kind: "preview", | |
| 1252 | branch, | |
| 1253 | number: null, | |
| 1254 | commit, | |
| 1255 | source: repoOf(project).path, | |
| 1256 | reader: actor, | |
| 1257 | createdBy: "g1t", | |
| 1258 | settings, | |
| 1259 | // As `stack` built it: the project's own default branch. | |
| 1260 | trusted: true, | |
| 1261 | }); | |
| 1262 | } | |
| 1263 | ||
| Deployments: a preview for every pull request, production on g1t.page | 1264 | // ---- The sweep ----------------------------------------------------- |
| 1265 | ||
| 1266 | /** | |
| 1267 | * Every few minutes: builds that died are failed; usage is counted; idle | |
| Projects: what a workspace builds and runs, first on every page | 1268 | * previews, the apps of workspaces whose plan ended, and scripts no app |
| 1269 | * holds come down; and a month that is over is charged past its | |
| 1270 | * allowance. | |
| Deployments: a preview for every pull request, production on g1t.page | 1271 | */ |
| 1272 | async sweep(): Promise<void> { | |
| 1273 | const cutoff = new Date(Date.now() - BUILD_TIMEOUT_MS).toISOString(); | |
| 1274 | const stuck = await this.db | |
| 1275 | .prepare("SELECT id FROM deployments WHERE status IN ('queued', 'building') AND created_at < ?") | |
| 1276 | .bind(cutoff) | |
| 1277 | .all<{ id: string }>(); | |
| 1278 | for (const { id } of stuck.results) await this.finishFailed(id, "The build did not finish in 45 minutes.", null, null); | |
| 1279 | ||
| 1280 | const apps = (await this.db.prepare("SELECT * FROM apps").all<AppRow>()).results; | |
| Projects: what a workspace builds and runs, first on every page | 1281 | const workspaces = [...new Set(apps.map((app) => app.workspace))]; |
| Deployments: a preview for every pull request, production on g1t.page | 1282 | |
| 1283 | // Apps of workspaces whose plan has ended come down. | |
| 1284 | const billing = billingClient(this.env.BILLING); | |
| Usage limits: unpaid usage can only go so far | 1285 | await this.holdToLimits(apps).catch((error) => console.error("could not apply limits", error)); |
| Deployments: a preview for every pull request, production on g1t.page | 1286 | for (const workspace of workspaces) { |
| 1287 | const plan = await billing.hasFeature(workspace, "deployments"); | |
| 1288 | if (!plan.ok && plan.error.code === "payment_required") { | |
| Projects: what a workspace builds and runs, first on every page | 1289 | for (const app of apps.filter((a) => a.workspace === workspace)) await this.removeApp(app.script); |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1290 | // Custom domains cost g1t by the month: they go with the plan. |
| 1291 | await this.domains.removeWhere("workspace", workspace).catch((error) => console.error("could not remove domains", error)); | |
| Deployments: a preview for every pull request, production on g1t.page | 1292 | } |
| 1293 | } | |
| 1294 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1295 | await this.domains |
| 1296 | .sweep(async (projectId) => { | |
| 1297 | const app = await this.db | |
| 1298 | .prepare("SELECT script FROM apps WHERE project_id = ? AND kind = 'production'") | |
| 1299 | .bind(projectId) | |
| 1300 | .first<{ script: string }>(); | |
| 1301 | return app?.script ?? null; | |
| 1302 | }) | |
| 1303 | .catch((error) => console.error("could not check domains", error)); | |
| 1304 | ||
| Projects: what a workspace builds and runs, first on every page | 1305 | await this.removeOrphans(apps).catch((error) => console.error("could not remove orphans", error)); |
| Deployments: a preview for every pull request, production on g1t.page | 1306 | await this.count(apps).catch((error) => console.error("could not count usage", error)); |
| 1307 | await this.takeDownIdle(); | |
| 1308 | await this.chargeMonths(); | |
| 1309 | } | |
| 1310 | ||
| Usage limits: unpaid usage can only go so far | 1311 | /** |
| 1312 | * Pauses the apps of workspaces that reached their limit for usage not | |
| 1313 | * yet paid for, and rebuilds them from the same commit once they are | |
| 1314 | * under it again. Paused apps answer with a notice and run nothing. | |
| 1315 | */ | |
| 1316 | private async holdToLimits(apps: AppRow[]): Promise<void> { | |
| 1317 | const cloudflare = this.cloudflare; | |
| 1318 | if (!cloudflare) return; | |
| 1319 | const billing = billingClient(this.env.BILLING); | |
| 1320 | for (const workspace of [...new Set(apps.map((app) => app.workspace))]) { | |
| 1321 | const limit = await billing.checkLimit(workspace); | |
| 1322 | if (!limit.ok) continue; | |
| 1323 | const theirs = apps.filter((app) => app.workspace === workspace); | |
| 1324 | if (limit.value.state === "stopped") { | |
| 1325 | for (const app of theirs.filter((a) => !a.paused_at)) { | |
| 1326 | await cloudflare.pauseScript(app.script); | |
| 1327 | await this.db.prepare("UPDATE apps SET paused_at = ? WHERE script = ?").bind(now(), app.script).run(); | |
| 1328 | } | |
| 1329 | continue; | |
| 1330 | } | |
| 1331 | const paused = theirs.filter((a) => a.paused_at); | |
| 1332 | if (paused.length === 0) continue; | |
| 1333 | const actor = await this.workspaceActor(workspace); | |
| 1334 | if (!actor) continue; | |
| 1335 | for (const app of paused) { | |
| 1336 | const project = await this.projects.get(workspace, app.slug, actor); | |
| 1337 | if (!project.ok) continue; | |
| 1338 | // A failed or refused rebuild leaves it paused, to try again next time. | |
| 1339 | const rebuilt = | |
| 1340 | app.kind === "production" | |
| 1341 | ? await this.deployProduction(project.value, app.commit_sha, "g1t") | |
| 1342 | : app.number != null | |
| 1343 | ? await this.deployPreview(project.value, app.number, "g1t", true) | |
| 1344 | : null; | |
| 1345 | if (rebuilt && !rebuilt.ok) console.log("could not resume", app.script, rebuilt.error.message); | |
| 1346 | } | |
| 1347 | } | |
| 1348 | } | |
| 1349 | ||
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1350 | /** |
| 1351 | * Scripts in the namespace that no app holds, such as ones renamed. An | |
| 1352 | * old address that redirects to its app's new one is held until its | |
| 1353 | * redirect expires, then removed with the rest. | |
| 1354 | */ | |
| Projects: what a workspace builds and runs, first on every page | 1355 | private async removeOrphans(apps: AppRow[]): Promise<void> { |
| 1356 | const cloudflare = this.cloudflare; | |
| 1357 | if (!cloudflare) return; | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1358 | await this.db.prepare("DELETE FROM redirects WHERE expires_at < ?").bind(now()).run(); |
| 1359 | const redirects = await this.db.prepare("SELECT script FROM redirects").all<{ script: string }>(); | |
| 1360 | const held = new Set([...apps.map((app) => app.script), ...redirects.results.map((r) => r.script)]); | |
| Projects: what a workspace builds and runs, first on every page | 1361 | const building = await this.db |
| 1362 | .prepare("SELECT script FROM deployments WHERE status IN ('queued', 'building')") | |
| 1363 | .all<{ script: string }>(); | |
| 1364 | for (const row of building.results) held.add(row.script); | |
| 1365 | const cutoff = Date.now() - ORPHAN_AFTER_MS; | |
| 1366 | for (const script of await cloudflare.listScripts()) { | |
| 1367 | if (!held.has(script.id) && Date.parse(script.modified_on) < cutoff) await cloudflare.deleteScript(script.id); | |
| 1368 | } | |
| 1369 | } | |
| 1370 | ||
| Deployments: a preview for every pull request, production on g1t.page | 1371 | /** Counts this month's requests and CPU time per workspace, from analytics. */ |
| 1372 | private async count(apps: AppRow[]): Promise<void> { | |
| 1373 | const cloudflare = this.cloudflare; | |
| 1374 | if (!cloudflare || apps.length === 0) return; | |
| 1375 | const start = `${month()}-01T00:00:00Z`; | |
| 1376 | const totals = await cloudflare.usage(apps.map((app) => app.script), start, now()); | |
| 1377 | // Analytics only counts apps that are up; the meter keeps what earlier | |
| 1378 | // apps used by never going down. | |
| 1379 | const perWorkspace = new Map<string, { requests: number; cpuMs: number }>(); | |
| 1380 | for (const app of apps) { | |
| 1381 | const used = totals.get(app.script); | |
| 1382 | if (!used) continue; | |
| Projects: what a workspace builds and runs, first on every page | 1383 | const sum = perWorkspace.get(app.workspace) ?? { requests: 0, cpuMs: 0 }; |
| Deployments: a preview for every pull request, production on g1t.page | 1384 | sum.requests += used.requests; |
| 1385 | sum.cpuMs += used.cpuMs; | |
| Projects: what a workspace builds and runs, first on every page | 1386 | perWorkspace.set(app.workspace, sum); |
| Deployments: a preview for every pull request, production on g1t.page | 1387 | } |
| 1388 | const at = now(); | |
| Projects: what a workspace builds and runs, first on every page | 1389 | for (const [workspace, used] of perWorkspace) { |
| Deployments: a preview for every pull request, production on g1t.page | 1390 | await this.db |
| 1391 | .prepare( | |
| 1392 | `INSERT INTO meters (namespace, month, requests, cpu_ms, counted_at) VALUES (?1, ?2, ?3, ?4, ?5) | |
| 1393 | ON CONFLICT (namespace, month) DO UPDATE SET | |
| 1394 | requests = MAX(requests, ?3), cpu_ms = MAX(cpu_ms, ?4), counted_at = ?5`, | |
| 1395 | ) | |
| Projects: what a workspace builds and runs, first on every page | 1396 | .bind(workspace, month(), used.requests, used.cpuMs, at) |
| Deployments: a preview for every pull request, production on g1t.page | 1397 | .run(); |
| 1398 | } | |
| 1399 | // When each preview last answered anyone, for the idle sweep. | |
| 1400 | const recent = await cloudflare.usage( | |
| 1401 | apps.filter((app) => app.kind === "preview").map((app) => app.script), | |
| 1402 | new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(), | |
| 1403 | at, | |
| 1404 | ); | |
| 1405 | for (const [script, used] of recent) { | |
| 1406 | if (used.requests > 0) { | |
| 1407 | await this.db.prepare("UPDATE apps SET last_request_at = ? WHERE script = ?").bind(at, script).run(); | |
| 1408 | } | |
| 1409 | } | |
| Projects: what a workspace builds and runs, first on every page | 1410 | for (const workspace of new Set(apps.map((app) => app.workspace))) await this.notePeak(workspace); |
| Prices keep themselves current with what g1t pays | 1411 | // What this month's traffic past the plan will cost, so the workspace's |
| 1412 | // limit counts it now rather than when the month closes. | |
| 1413 | const costs = await this.costs(); | |
| 1414 | const a = DEPLOYMENTS_ALLOWANCE; | |
| 1415 | for (const workspace of perWorkspace.keys()) { | |
| 1416 | const meter = await this.db | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1417 | .prepare("SELECT requests, cpu_ms, peak_apps, peak_domains FROM meters WHERE namespace = ? AND month = ?") |
| Prices keep themselves current with what g1t pays | 1418 | .bind(workspace, month()) |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1419 | .first<{ requests: number; cpu_ms: number; peak_apps: number; peak_domains: number }>(); |
| Prices keep themselves current with what g1t pays | 1420 | if (!meter) continue; |
| 1421 | const cost = Math.ceil( | |
| 1422 | (Math.max(0, meter.requests - a.requests) / 1_000_000) * costs.millionRequests + | |
| 1423 | (Math.max(0, meter.cpu_ms - a.cpuMs) / 1_000_000) * costs.millionCpuMs + | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1424 | Math.max(0, meter.peak_apps - a.apps) * costs.appMonth + |
| 1425 | extraDomains(meter.peak_domains ?? 0) * costs.domainMonth, | |
| Prices keep themselves current with what g1t pays | 1426 | ); |
| 1427 | await billingClient(this.env.BILLING) | |
| 1428 | .notePending(workspace, "deployments", cost) | |
| 1429 | .catch((error) => console.error("could not note pending usage", error)); | |
| 1430 | } | |
| Deployments: a preview for every pull request, production on g1t.page | 1431 | } |
| 1432 | ||
| Projects: what a workspace builds and runs, first on every page | 1433 | /** Previews no one has visited in their project's idle days. */ |
| Deployments: a preview for every pull request, production on g1t.page | 1434 | private async takeDownIdle(): Promise<void> { |
| 1435 | const idle = await this.db | |
| 1436 | .prepare( | |
| Projects: what a workspace builds and runs, first on every page | 1437 | `SELECT apps.script FROM apps JOIN settings ON settings.project_id = apps.project_id |
| Deployments: a preview for every pull request, production on g1t.page | 1438 | WHERE apps.kind = 'preview' |
| 1439 | AND COALESCE(apps.last_request_at, apps.deployed_at) < strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || settings.idle_days || ' days')`, | |
| 1440 | ) | |
| 1441 | .all<{ script: string }>(); | |
| 1442 | for (const { script } of idle.results) await this.removeApp(script); | |
| 1443 | } | |
| 1444 | ||
| 1445 | /** Charges each month that is over for what it used past the allowance, once. */ | |
| 1446 | private async chargeMonths(): Promise<void> { | |
| 1447 | const due = await this.db | |
| 1448 | .prepare("SELECT * FROM meters WHERE month < ? AND charged_at IS NULL") | |
| 1449 | .bind(month()) | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1450 | .all<{ namespace: string; month: string; requests: number; cpu_ms: number; peak_apps: number; peak_domains: number }>(); |
| Deployments: a preview for every pull request, production on g1t.page | 1451 | const a = DEPLOYMENTS_ALLOWANCE; |
| Prices keep themselves current with what g1t pays | 1452 | const costs = await this.costs(); |
| Deployments: a preview for every pull request, production on g1t.page | 1453 | for (const meter of due.results) { |
| 1454 | const extraRequests = Math.max(0, meter.requests - a.requests); | |
| 1455 | const extraCpu = Math.max(0, meter.cpu_ms - a.cpuMs); | |
| 1456 | const extraApps = Math.max(0, meter.peak_apps - a.apps); | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1457 | const moreDomains = extraDomains(meter.peak_domains ?? 0); |
| Deployments: a preview for every pull request, production on g1t.page | 1458 | const cost = Math.ceil( |
| Prices keep themselves current with what g1t pays | 1459 | (extraRequests / 1_000_000) * costs.millionRequests + |
| 1460 | (extraCpu / 1_000_000) * costs.millionCpuMs + | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1461 | extraApps * costs.appMonth + |
| 1462 | moreDomains * costs.domainMonth, | |
| Deployments: a preview for every pull request, production on g1t.page | 1463 | ); |
| 1464 | if (cost > 0) { | |
| 1465 | const parts = [ | |
| 1466 | extraApps && `${extraApps} extra apps`, | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1467 | moreDomains && `${moreDomains} extra custom domains`, |
| Deployments: a preview for every pull request, production on g1t.page | 1468 | extraRequests && `${extraRequests.toLocaleString("en-US")} extra requests`, |
| 1469 | extraCpu && `${extraCpu.toLocaleString("en-US")} extra CPU ms`, | |
| 1470 | ].filter(Boolean); | |
| 1471 | const charged = await billingClient(this.env.BILLING).chargeFeature({ | |
| 1472 | workspace: meter.namespace, | |
| 1473 | feature: "deployments", | |
| 1474 | costMicros: cost, | |
| 1475 | description: `Deployments in ${meter.month} past the plan: ${parts.join(", ")}`, | |
| 1476 | reference: `deployments/${meter.namespace}/${meter.month}`, | |
| 1477 | }); | |
| 1478 | if (!charged.ok) continue; | |
| 1479 | } | |
| 1480 | await this.db | |
| 1481 | .prepare("UPDATE meters SET charged_at = ? WHERE namespace = ? AND month = ?") | |
| 1482 | .bind(now(), meter.namespace, meter.month) | |
| 1483 | .run(); | |
| 1484 | } | |
| 1485 | } | |
| 1486 | } | |
| 1487 | ||
| 1488 | /** `POST /rpc/<method>`: the arguments are the body. */ | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 1489 | async function rpc(service: Deployments, method: string, args: any, ctx: ExecutionContext): Promise<unknown> { |
| Deployments: a preview for every pull request, production on g1t.page | 1490 | switch (method) { |
| 1491 | case "settings": | |
| 1492 | return service.settings(args); | |
| 1493 | case "update_settings": | |
| 1494 | return service.updateSettings(args); | |
| 1495 | case "list": | |
| 1496 | return service.list(args); | |
| 1497 | case "get": | |
| 1498 | return service.get(args); | |
| 1499 | case "redeploy": | |
| 1500 | return service.redeploy(args); | |
| 1501 | case "take_down": | |
| 1502 | return service.takeDown(args); | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 1503 | case "stack": |
| 1504 | return service.stack(args, (work) => ctx.waitUntil(work)); | |
| Projects: what a workspace builds and runs, first on every page | 1505 | case "overview": |
| 1506 | return service.overview(args); | |
| Deployments: a preview for every pull request, production on g1t.page | 1507 | case "usage": |
| 1508 | return service.usage(args); | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1509 | case "domains": |
| 1510 | return service.listDomains(args); | |
| 1511 | case "add_domain": | |
| 1512 | return service.addDomain(args); | |
| 1513 | case "remove_domain": | |
| 1514 | return service.removeDomain(args); | |
| 1515 | case "refresh_domain": | |
| 1516 | return service.refreshDomain(args); | |
| Deployments: a preview for every pull request, production on g1t.page | 1517 | default: |
| 1518 | return undefined; | |
| 1519 | } | |
| 1520 | } | |
| 1521 | ||
| 1522 | export default { | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 1523 | async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> { |
| Deployments: a preview for every pull request, production on g1t.page | 1524 | const { pathname } = new URL(request.url); |
| 1525 | if (request.method !== "POST") return new Response("Not found\n", { status: 404 }); | |
| 1526 | const service = new Deployments(env); | |
| 1527 | const body = (await request.json().catch(() => ({}))) as Record<string, unknown>; | |
| 1528 | const rpcMatch = pathname.match(/^\/rpc\/([a-z_]+)$/); | |
| 1529 | if (rpcMatch) { | |
| Project dependencies: addresses, preview stacks, Affects, and agents who know | 1530 | const result = await rpc(service, rpcMatch[1], body, ctx); |
| Deployments: a preview for every pull request, production on g1t.page | 1531 | return result === undefined ? new Response("Unknown method\n", { status: 404 }) : Response.json(result); |
| 1532 | } | |
| 1533 | // A build's reports, forwarded by the API. | |
| 1534 | const jobMatch = pathname.match(/^\/jobs\/([a-z0-9_]+)\/(started|session|finish|fail)$/); | |
| 1535 | if (jobMatch) return service.job(jobMatch[1], jobMatch[2], body); | |
| 1536 | return new Response("Not found\n", { status: 404 }); | |
| 1537 | }, | |
| 1538 | ||
| 1539 | async queue(batch: MessageBatch<G1tEvent>, env: Env): Promise<void> { | |
| 1540 | const service = new Deployments(env); | |
| 1541 | for (const message of batch.messages) { | |
| 1542 | try { | |
| Agents and memory, checks and conflicts, profiles, slug renames, custom domains | 1543 | await service.onEvent(message.body, message.attempts); |
| Deployments: a preview for every pull request, production on g1t.page | 1544 | message.ack(); |
| 1545 | } catch (error) { | |
| 1546 | console.error("deployments could not handle", message.body.type, error); | |
| 1547 | message.retry(); | |
| 1548 | } | |
| 1549 | } | |
| 1550 | }, | |
| 1551 | ||
| 1552 | async scheduled(_controller: ScheduledController, env: Env): Promise<void> { | |
| 1553 | await new Deployments(env).sweep(); | |
| 1554 | }, | |
| 1555 | } satisfies ExportedHandler<Env, G1tEvent>; |