g1t/apps/web/app/routes/repo/deployments.tsx

325 lines14,696 bytesCodeBlame

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.page1import { ExternalLink, Globe, Rocket, RotateCw, Trash2 } from "lucide-react";
2import type { ReactNode } from "react";
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look3import { Form, Link, useNavigation } from "react-router";
Deployments: a preview for every pull request, production on g1t.page4
Projects: what a workspace builds and runs, first on every page5import type { Deployment, FeatureState } from "@g1t/contracts";
Deployments: a preview for every pull request, production on g1t.page6
Projects: what a workspace builds and runs, first on every page7import { host, StatusDot } from "../../components/deploy";
8
Deployments: a preview for every pull request, production on g1t.page9import type { Route } from "./+types/deployments";
Social cards for every page: og.g1t.sh10import { page } from "../../lib/meta";
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look11import { Button, ButtonLink, ComputeNote, EmptyState, ErrorText, TimeAgo } from "../../components/ui";
12import { computeNoteFor } from "../../lib/compute.server";
A project is an app or a library: libraries show their package and how to ship a release, not production13import { billing, deployments, projects } from "../../lib/services.server";
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look14import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server";
15import { refusal, requireRepo } from "../../lib/access.server";
16import { whyNot } from "../../lib/access";
Deployments: a preview for every pull request, production on g1t.page17
Social cards for every page: og.g1t.sh18export function meta({ params, ...args }: Route.MetaArgs) {
19 return page(args, { title: `Deployments · ${params.owner}/${params.repo} · g1t` });
Deployments: a preview for every pull request, production on g1t.page20}
21
22export async function loader({ params, context }: Route.LoaderArgs) {
23 const viewer = getViewer(context);
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look24 // Anyone who can read the repository: a public one's to anyone, a private one's to people with a role.
25 const { access } = await requireRepo(context, params, "read");
Projects: what a workspace builds and runs, first on every page26 const ref = { workspace: params.owner, slug: params.repo };
A project is an app or a library: libraries show their package and how to ship a release, not production27 const [settings, list, features, computeNote, project] = await Promise.all([
Projects: what a workspace builds and runs, first on every page28 deployments.settings(ref, viewer),
29 deployments.list(ref, viewer),
Deployments: a preview for every pull request, production on g1t.page30 billing.features(params.owner, viewer),
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look31 // Builds are compute: said before a deploy is refused for it.
32 computeNoteFor(params.owner, "deploy"),
A project is an app or a library: libraries show their package and how to ship a release, not production33 projects.get(params.owner, params.repo, viewer).catch(() => null),
Deployments: a preview for every pull request, production on g1t.page34 ]);
A project is an app or a library: libraries show their package and how to ship a release, not production35 // Set not to deploy, in General settings: turning them on waits for that to change.
36 const notDeploying = project?.ok ? project.value.deploys === "no" : false;
Fixes from the page sweep37 // Deployments come with the g1t plan. Someone outside the workspace does
38 // not see its plans; the page works without, and the deployments service
39 // refuses a deploy the plan does not cover.
40 const plan = features.ok
41 ? (features.value.find((state) => state.plan.feature === "plan" || state.plan.feature === "deployments") ?? null)
42 : null;
A project is an app or a library: libraries show their package and how to ship a release, not production43 return { can: access.can, settings: unwrap(settings), ...unwrap(list), plan, computeNote, notDeploying };
Deployments: a preview for every pull request, production on g1t.page44}
45
46export async function action({ request, params, context }: Route.ActionArgs) {
47 assertSameOrigin(request);
48 const user = requireUser(context, request);
49 const form = await request.formData();
Projects: what a workspace builds and runs, first on every page50 const ref = { workspace: params.owner, slug: params.repo };
Deployments: a preview for every pull request, production on g1t.page51 const intent = form.get("intent");
Projects: what a workspace builds and runs, first on every page52 const branch = form.get("branch") ? String(form.get("branch")) : null;
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look53 // Deploying is compute (Write); turning deployments on or off is a deployment setting (Admin).
54 const refused = await refusal(context, params, intent === "enable" || intent === "disable" ? "manage_integrations" : "run");
55 if (refused) return { error: refused };
Deployments: a preview for every pull request, production on g1t.page56 if (intent === "redeploy") {
Projects: what a workspace builds and runs, first on every page57 const started = await deployments.redeploy(user, ref, branch);
Deployments: a preview for every pull request, production on g1t.page58 return started.ok ? { notice: "Build started." } : { error: started.error.message };
59 }
60 if (intent === "take-down") {
Projects: what a workspace builds and runs, first on every page61 const done = await deployments.takeDown(user, ref, branch);
Deployments: a preview for every pull request, production on g1t.page62 return done.ok ? { notice: "Taken down." } : { error: done.error.message };
63 }
64 if (intent === "enable" || intent === "disable") {
Projects: what a workspace builds and runs, first on every page65 const saved = await deployments.updateSettings(user, ref, { enabled: intent === "enable" });
Deployments: a preview for every pull request, production on g1t.page66 return saved.ok
67 ? { notice: intent === "enable" ? "Deployments are on. Production is building." : "Deployments are off, and every app is down." }
68 : { error: saved.error.message };
69 }
Secrets and variables: one list, rows per environment, for workflows and deployments70 return { error: "Unknown request." };
Deployments: a preview for every pull request, production on g1t.page71}
72
73export default function RepoDeployments({ loaderData, actionData, params }: Route.ComponentProps) {
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look74 const { settings, deployments: builds, live, plan, can } = loaderData;
Deployments: a preview for every pull request, production on g1t.page75 const busy = useNavigation().state === "submitting";
76 const base = `/${params.owner}/${params.repo}`;
77 const production = live.find((app) => app.kind === "production");
78 const previews = live.filter((app) => app.kind === "preview");
79
80 return (
81 <div className="mx-auto max-w-5xl">
82 <header className="flex flex-wrap items-start justify-between gap-4">
83 <div>
84 <h1 className="flex items-center gap-2 text-xl font-semibold tracking-tight">
85 <Rocket size={18} className="text-accent" />
86 Deployments
87 </h1>
88 <p className="mt-1 max-w-2xl text-sm text-muted">
89 Every pull request gets a live preview on g1t.page, and the default branch goes to production on each push.
90 Apps run on Cloudflare only while someone visits them.{" "}
91 <a href="https://docs.g1t.sh/guides/deployments/" className="text-fg hover:underline">
92 How it works
93 </a>
94 </p>
95 </div>
96 {settings.enabled && (
97 <a
Agents and memory, checks and conflicts, profiles, slug renames, custom domains98 href={settings.primaryDomain ? `https://${settings.primaryDomain}` : settings.productionUrl}
Deployments: a preview for every pull request, production on g1t.page99 className="inline-flex items-center gap-1.5 rounded-md border border-line px-3 py-1.5 font-mono text-xs text-muted hover:border-line-strong hover:text-fg"
100 >
101 <Globe size={13} />
Agents and memory, checks and conflicts, profiles, slug renames, custom domains102 {settings.primaryDomain ?? host(settings.productionUrl)}
Deployments: a preview for every pull request, production on g1t.page103 </a>
104 )}
105 </header>
106
107 <div className="mt-4 min-h-6">
108 {actionData && "notice" in actionData && <p className="text-sm text-accent">{actionData.notice}</p>}
109 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look110 {!actionData && <ComputeNote note={loaderData.computeNote} />}
Deployments: a preview for every pull request, production on g1t.page111 </div>
112
Fixes from the page sweep113 {plan != null && !plan.on && !plan.included && live.length === 0 ? (
Deployments: a preview for every pull request, production on g1t.page114 <PlanNeeded plan={plan} owner={params.owner} />
115 ) : !settings.enabled ? (
116 <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6">
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look117 <h2 className="font-medium">Deployments are off</h2>
Deployments: a preview for every pull request, production on g1t.page118 <p className="mt-1 max-w-2xl text-sm text-muted">
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look119 Each project turns deployments on for itself; until then nothing builds or runs. Once on, production goes
120 up at <span className="font-mono text-fg">{host(settings.productionUrl)}</span> and every open pull request
121 gets its own preview. Workers projects (a <code className="font-mono text-fg">wrangler.jsonc</code>) and
122 static sites deploy without configuration.
Deployments: a preview for every pull request, production on g1t.page123 </p>
A project is an app or a library: libraries show their package and how to ship a release, not production124 {loaderData.notDeploying ? (
125 <p className="mt-4 max-w-2xl text-sm text-muted">
126 This project is set as one that doesn't deploy, a library or a tool. To deploy it, choose Deploys or Detect
127 automatically in{" "}
128 <Link to={`/${params.owner}/${params.repo}/settings#deploys`} className="text-fg underline underline-offset-4">
129 its settings
130 </Link>{" "}
131 first.
132 </p>
133 ) : (
134 <Form method="post" className="mt-4">
135 <Button variant="accent" type="submit" name="intent" value="enable" disabled={busy || !can.manage_integrations} title={whyNot(can, "manage_integrations")}>
136 <Rocket size={14} />
137 Turn on deployments
138 </Button>
139 </Form>
140 )}
Deployments: a preview for every pull request, production on g1t.page141 </section>
142 ) : (
143 <>
144 <section className="mt-2 grid gap-4 md:grid-cols-2">
145 <LiveCard
146 title="Production"
147 hint="The default branch, on every push."
148 app={production}
149 off={!settings.production}
150 actions={
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look151 can.run ? <AppActions branch={null} up={!!production} busy={busy} /> : null
Deployments: a preview for every pull request, production on g1t.page152 }
153 />
154 <div className="rounded-xl border border-line bg-surface p-5">
155 <h2 className="text-sm font-medium">Previews</h2>
156 <p className="mt-0.5 text-xs text-faint">
157 {settings.previews
Projects: what a workspace builds and runs, first on every page158 ? `One per branch with an open pull request; down when it closes or after ${settings.idleDays} days without a visit.`
Deployments: a preview for every pull request, production on g1t.page159 : "Off for this repository."}
160 </p>
161 {previews.length === 0 ? (
162 <p className="mt-4 text-sm text-muted">No previews are up.</p>
163 ) : (
164 <ul className="mt-3 divide-y divide-line">
165 {previews.map((app) => (
166 <li key={app.url} className="flex items-center gap-3 py-2 text-sm">
Projects: what a workspace builds and runs, first on every page167 <span className="shrink-0">
168 <span className="font-medium">{app.branch}</span>
169 {app.number != null && (
170 <Link to={`${base}/pull/${app.number}`} className="ml-1.5 text-xs text-muted hover:underline">
171 #{app.number}
172 </Link>
173 )}
174 </span>
Deployments: a preview for every pull request, production on g1t.page175 <a href={app.url} className="min-w-0 truncate font-mono text-xs text-muted hover:text-fg">
Projects: what a workspace builds and runs, first on every page176 {host(app.url)}
Deployments: a preview for every pull request, production on g1t.page177 </a>
178 <span className="ml-auto shrink-0">
Invite-only launch: sign in with GitHub, repository access and lifecycle, many emails, a new look179 {can.run && <AppActions branch={app.branch} up busy={busy} compact />}
Deployments: a preview for every pull request, production on g1t.page180 </span>
181 </li>
182 ))}
183 </ul>
184 )}
185 </div>
186 </section>
187
188 <h2 className="mt-10 text-sm font-medium text-muted">Recent builds</h2>
189 <div className="mt-3">
190 {builds.length === 0 ? (
191 <EmptyState title="No builds yet">Push to the default branch or open a pull request.</EmptyState>
192 ) : (
193 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line">
194 {builds.map((build) => (
195 <BuildRow key={build.id} build={build} base={base} />
196 ))}
197 </ul>
198 )}
199 </div>
200
Secrets and variables: one list, rows per environment, for workflows and deployments201 <p className="mt-10 text-sm text-muted">
202 Build command, output directory, idle days, and turning deployments off are under{" "}
203 <Link to={`${base}/settings/deployments`} className="text-fg hover:underline">
204 Settings → Deployments
205 </Link>
206 . Secrets and config for builds and running apps are under{" "}
207 <Link to={`${base}/settings/secrets`} className="text-fg hover:underline">
208 Settings → Secrets and variables
209 </Link>
Agents and memory, checks and conflicts, profiles, slug renames, custom domains210 , and production's own domains under{" "}
211 <Link to={`${base}/settings/domains`} className="text-fg hover:underline">
212 Settings → Domains
213 </Link>
Secrets and variables: one list, rows per environment, for workflows and deployments214 .
215 </p>
Deployments: a preview for every pull request, production on g1t.page216 </>
217 )}
218 </div>
219 );
220}
221
222function PlanNeeded({ plan, owner }: { plan: FeatureState | null; owner: string }) {
223 return (
224 <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6">
225 <h2 className="font-medium">Deployments are part of a paid plan</h2>
226 <p className="mt-1 max-w-2xl text-sm text-muted">
Projects: what a workspace builds and runs, first on every page227 Turn on Deployments for the {owner} workspace and every project in it can have a preview for each pull
Deployments: a preview for every pull request, production on g1t.page228 request and production on g1t.page.
229 {plan && ` $${(plan.plan.monthlyCents / 100).toFixed(0)} a month, including:`}
230 </p>
231 {plan && (
232 <ul className="mt-3 grid gap-1.5 text-sm text-muted sm:grid-cols-2">
233 {plan.plan.includes.map((line) => (
234 <li key={line} className="flex gap-2">
235 <span className="text-accent">✓</span>
236 {line}
237 </li>
238 ))}
239 </ul>
240 )}
241 {plan && <p className="mt-3 text-xs text-faint">{plan.plan.overage}</p>}
242 <div className="mt-4">
243 <ButtonLink variant="accent" to={`/${owner}/-/billing`}>
244 See the plan under Billing
245 </ButtonLink>
246 </div>
247 </section>
248 );
249}
250
251function LiveCard({
252 title,
253 hint,
254 app,
255 off,
256 actions,
257}: {
258 title: string;
259 hint: string;
260 app: { url: string; commit: string; deployedAt: string } | undefined;
261 off: boolean;
262 actions: ReactNode;
263}) {
264 return (
265 <div className="rounded-xl border border-line bg-surface p-5">
266 <h2 className="text-sm font-medium">{title}</h2>
267 <p className="mt-0.5 text-xs text-faint">{off ? "Off for this repository." : hint}</p>
268 {app ? (
269 <>
270 <a href={app.url} className="mt-3 flex items-center gap-1.5 font-mono text-sm text-accent hover:underline">
Projects: what a workspace builds and runs, first on every page271 {host(app.url)}
Deployments: a preview for every pull request, production on g1t.page272 <ExternalLink size={12} />
273 </a>
274 <p className="mt-1 text-xs text-faint">
275 <span className="font-mono">{app.commit.slice(0, 8)}</span> · deployed <TimeAgo at={app.deployedAt} />
276 </p>
277 </>
278 ) : (
279 <p className="mt-4 text-sm text-muted">Not up.</p>
280 )}
281 {!off && <div className="mt-4">{actions}</div>}
282 </div>
283 );
284}
285
Projects: what a workspace builds and runs, first on every page286function AppActions({ branch, up, busy, compact }: { branch: string | null; up: boolean; busy: boolean; compact?: boolean }) {
Deployments: a preview for every pull request, production on g1t.page287 return (
288 <Form method="post" className="flex items-center gap-2">
Projects: what a workspace builds and runs, first on every page289 {branch != null && <input type="hidden" name="branch" value={branch} />}
Deployments: a preview for every pull request, production on g1t.page290 <Button variant="quiet" type="submit" name="intent" value="redeploy" disabled={busy} title="Build again from the current head">
291 <RotateCw size={13} />
292 {!compact && "Redeploy"}
293 </Button>
294 {up && (
295 <Button variant="quiet" type="submit" name="intent" value="take-down" disabled={busy} title="Take it down now">
296 <Trash2 size={13} />
297 {!compact && "Take down"}
298 </Button>
299 )}
300 </Form>
301 );
302}
303
304function BuildRow({ build, base }: { build: Deployment; base: string }) {
305 return (
306 <li>
307 <Link to={`${base}/deployments/${build.id}`} className="flex items-center gap-4 px-4 py-3 text-sm hover:bg-surface">
308 <span className="w-20 shrink-0">
309 <StatusDot status={build.status} />
310 </span>
311 <span className="min-w-0 grow">
312 <span className="block truncate font-medium">
Projects: what a workspace builds and runs, first on every page313 {build.kind === "production" ? "Production" : `Preview of ${build.branch}`}
Deployments: a preview for every pull request, production on g1t.page314 <span className="ml-2 font-mono text-xs font-normal text-faint">{build.commit.slice(0, 8)}</span>
315 </span>
316 {build.error && <span className="mt-0.5 block truncate text-xs text-muted">{build.error}</span>}
317 </span>
318 <span className="shrink-0 text-xs text-faint">
319 {build.buildSeconds != null && `${build.buildSeconds} s · `}
320 <TimeAgo at={build.createdAt} />
321 </span>
322 </Link>
323 </li>
324 );
325}