import { all, first } from "../db"; import { HttpError } from "../errors"; import { loadRepoById } from "../repos"; import { ZERO_SHA } from "../git/store"; import { createDeployment, loadSite } from "./deploy"; import type { DeployRequest } from "./deploy"; const MAX_PREVIEWS_PER_PUSH = 5; async function tryDeploy(input: DeployRequest): Promise { try { return (await createDeployment(input)).id; } catch (error) { if (error instanceof HttpError) { console.warn("Pages deploy skipped", input.repo.id, error.message); return null; } throw error; } } /** Push hook: deploys the site branch to production and open pull request heads to previews. */ export async function deployPagesForPush( repoId: string, branch: string | null, sha: string, pusherId: string | null, ): Promise { if (!branch || sha === ZERO_SHA) return 0; const [repo, site] = await Promise.all([loadRepoById(repoId), loadSite(repoId)]); if (!repo || !site || site.source !== "branch" || repo.visibility !== "public") return 0; const base = { repo, site, ref: `refs/heads/${branch}`, sha, artifactId: null, creatorId: pusherId }; if (branch === site.branch) { return (await tryDeploy({ ...base, environment: "production", prNumber: null })) ? 1 : 0; } if (site.previews !== 1) return 0; const pulls = await all<{ number: number }>( `SELECT i.number FROM pull_requests p JOIN issues i ON i.id = p.issue_id WHERE p.repo_id = ? AND p.head_repo_id = ? AND p.head_ref = ? AND i.state = 'open' LIMIT ?`, repo.id, repo.id, branch, MAX_PREVIEWS_PER_PUSH, ); let count = 0; for (const pull of pulls) { if (await tryDeploy({ ...base, environment: "preview", prNumber: pull.number })) count += 1; } return count; } type RunRow = { repo_id: string; source_repo_id: string; untrusted: number; event: string; ref: string; sha: string; pr_number: number | null; actor_id: string | null; conclusion: string | null; }; /** CI hook: deploys the configured artifact of a successful run to production or its pull request preview. */ export async function deployPagesForRun(runId: string): Promise { const runRow = await first( "SELECT repo_id, source_repo_id, untrusted, event, ref, sha, pr_number, actor_id, conclusion FROM workflow_runs WHERE id = ?", runId, ); if (!runRow || runRow.conclusion !== "success") return null; // Previews share the owner's Pages origin, so code from forks must never be deployed there. if (runRow.source_repo_id !== runRow.repo_id || runRow.untrusted !== 0) return null; const [repo, site] = await Promise.all([loadRepoById(runRow.repo_id), loadSite(runRow.repo_id)]); if (!repo || !site || site.source !== "workflow" || repo.visibility !== "public") return null; const preview = runRow.event === "pull_request" && runRow.pr_number !== null; if (preview && site.previews !== 1) return null; if (!preview && runRow.ref !== `refs/heads/${site.branch}`) return null; const artifact = await first<{ id: string }>( "SELECT id FROM job_artifacts WHERE run_id = ? AND name = ? ORDER BY created_at DESC LIMIT 1", runId, site.artifact_name, ); if (!artifact) return null; return tryDeploy({ repo, site, environment: preview ? "preview" : "production", prNumber: preview ? runRow.pr_number : null, ref: runRow.ref, sha: runRow.sha, artifactId: artifact.id, creatorId: runRow.actor_id, }); }