flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

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

293 lines12,472 bytesCodeBlame
1import { ExternalLink, Globe, Rocket, RotateCw, Trash2 } from "lucide-react";
2import type { ReactNode } from "react";
3import { Form, Link, data, useNavigation } from "react-router";
4
5import type { Deployment, FeatureState } from "@g1t/contracts";
6
7import { host, StatusDot } from "../../components/deploy";
8
9import type { Route } from "./+types/deployments";
10import { page } from "../../lib/meta";
11import { Button, ButtonLink, EmptyState, ErrorText, TimeAgo } from "../../components/ui";
12import { billing, deployments } from "../../lib/services.server";
13import { assertSameOrigin, getViewer, requireUser, roleIn, unwrap } from "../../lib/session.server";
14
15export function meta({ params, ...args }: Route.MetaArgs) {
16 return page(args, { title: `Deployments · ${params.owner}/${params.repo} · g1t` });
17}
18
19export async function loader({ params, context }: Route.LoaderArgs) {
20 const viewer = getViewer(context);
21 const role = roleIn(viewer, params.owner);
22 // Members only; to anyone else the page does not exist.
23 if (!role) throw data(null, { status: 404 });
24 const ref = { workspace: params.owner, slug: params.repo };
25 const [settings, list, features] = await Promise.all([
26 deployments.settings(ref, viewer),
27 deployments.list(ref, viewer),
28 billing.features(params.owner, viewer),
29 ]);
30 const plan = unwrap(features).find((state) => state.plan.feature === "deployments") ?? null;
31 return { role, settings: unwrap(settings), ...unwrap(list), plan };
32}
33
34export async function action({ request, params, context }: Route.ActionArgs) {
35 assertSameOrigin(request);
36 const user = requireUser(context, request);
37 const form = await request.formData();
38 const ref = { workspace: params.owner, slug: params.repo };
39 const intent = form.get("intent");
40 const branch = form.get("branch") ? String(form.get("branch")) : null;
41 if (intent === "redeploy") {
42 const started = await deployments.redeploy(user, ref, branch);
43 return started.ok ? { notice: "Build started." } : { error: started.error.message };
44 }
45 if (intent === "take-down") {
46 const done = await deployments.takeDown(user, ref, branch);
47 return done.ok ? { notice: "Taken down." } : { error: done.error.message };
48 }
49 if (intent === "enable" || intent === "disable") {
50 const saved = await deployments.updateSettings(user, ref, { enabled: intent === "enable" });
51 return saved.ok
52 ? { notice: intent === "enable" ? "Deployments are on. Production is building." : "Deployments are off, and every app is down." }
53 : { error: saved.error.message };
54 }
55 return { error: "Unknown request." };
56}
57
58export default function RepoDeployments({ loaderData, actionData, params }: Route.ComponentProps) {
59 const { settings, deployments: builds, live, plan } = loaderData;
60 const busy = useNavigation().state === "submitting";
61 const base = `/${params.owner}/${params.repo}`;
62 const production = live.find((app) => app.kind === "production");
63 const previews = live.filter((app) => app.kind === "preview");
64
65 return (
66 <div className="mx-auto max-w-5xl">
67 <header className="flex flex-wrap items-start justify-between gap-4">
68 <div>
69 <h1 className="flex items-center gap-2 text-xl font-semibold tracking-tight">
70 <Rocket size={18} className="text-accent" />
71 Deployments
72 </h1>
73 <p className="mt-1 max-w-2xl text-sm text-muted">
74 Every pull request gets a live preview on g1t.page, and the default branch goes to production on each push.
75 Apps run on Cloudflare only while someone visits them.{" "}
76 <a href="https://docs.g1t.sh/guides/deployments/" className="text-fg hover:underline">
77 How it works
78 </a>
79 </p>
80 </div>
81 {settings.enabled && (
82 <a
83 href={settings.productionUrl}
84 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"
85 >
86 <Globe size={13} />
87 {host(settings.productionUrl)}
88 </a>
89 )}
90 </header>
91
92 <div className="mt-4 min-h-6">
93 {actionData && "notice" in actionData && <p className="text-sm text-accent">{actionData.notice}</p>}
94 <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText>
95 </div>
96
97 {!plan?.on ? (
98 <PlanNeeded plan={plan} owner={params.owner} />
99 ) : !settings.enabled ? (
100 <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6">
101 <h2 className="font-medium">Deploy {params.repo}</h2>
102 <p className="mt-1 max-w-2xl text-sm text-muted">
103 Production goes up from <span className="font-mono text-fg">{host(settings.productionUrl)}</span>{" "}
104 as soon as you turn this on, and every open pull request gets its own preview. Workers projects (a{" "}
105 <code className="font-mono text-fg">wrangler.jsonc</code>) and static sites deploy without configuration.
106 </p>
107 <Form method="post" className="mt-4">
108 <Button variant="accent" type="submit" name="intent" value="enable" disabled={busy}>
109 <Rocket size={14} />
110 Turn on deployments
111 </Button>
112 </Form>
113 </section>
114 ) : (
115 <>
116 <section className="mt-2 grid gap-4 md:grid-cols-2">
117 <LiveCard
118 title="Production"
119 hint="The default branch, on every push."
120 app={production}
121 off={!settings.production}
122 actions={
123 <AppActions branch={null} up={!!production} busy={busy} />
124 }
125 />
126 <div className="rounded-xl border border-line bg-surface p-5">
127 <h2 className="text-sm font-medium">Previews</h2>
128 <p className="mt-0.5 text-xs text-faint">
129 {settings.previews
130 ? `One per branch with an open pull request; down when it closes or after ${settings.idleDays} days without a visit.`
131 : "Off for this repository."}
132 </p>
133 {previews.length === 0 ? (
134 <p className="mt-4 text-sm text-muted">No previews are up.</p>
135 ) : (
136 <ul className="mt-3 divide-y divide-line">
137 {previews.map((app) => (
138 <li key={app.url} className="flex items-center gap-3 py-2 text-sm">
139 <span className="shrink-0">
140 <span className="font-medium">{app.branch}</span>
141 {app.number != null && (
142 <Link to={`${base}/pull/${app.number}`} className="ml-1.5 text-xs text-muted hover:underline">
143 #{app.number}
144 </Link>
145 )}
146 </span>
147 <a href={app.url} className="min-w-0 truncate font-mono text-xs text-muted hover:text-fg">
148 {host(app.url)}
149 </a>
150 <span className="ml-auto shrink-0">
151 <AppActions branch={app.branch} up busy={busy} compact />
152 </span>
153 </li>
154 ))}
155 </ul>
156 )}
157 </div>
158 </section>
159
160 <h2 className="mt-10 text-sm font-medium text-muted">Recent builds</h2>
161 <div className="mt-3">
162 {builds.length === 0 ? (
163 <EmptyState title="No builds yet">Push to the default branch or open a pull request.</EmptyState>
164 ) : (
165 <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line">
166 {builds.map((build) => (
167 <BuildRow key={build.id} build={build} base={base} />
168 ))}
169 </ul>
170 )}
171 </div>
172
173 <p className="mt-10 text-sm text-muted">
174 Build command, output directory, idle days, and turning deployments off are under{" "}
175 <Link to={`${base}/settings/deployments`} className="text-fg hover:underline">
176 Settings → Deployments
177 </Link>
178 . Secrets and config for builds and running apps are under{" "}
179 <Link to={`${base}/settings/secrets`} className="text-fg hover:underline">
180 Settings → Secrets and variables
181 </Link>
182 .
183 </p>
184 </>
185 )}
186 </div>
187 );
188}
189
190function PlanNeeded({ plan, owner }: { plan: FeatureState | null; owner: string }) {
191 return (
192 <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6">
193 <h2 className="font-medium">Deployments are part of a paid plan</h2>
194 <p className="mt-1 max-w-2xl text-sm text-muted">
195 Turn on Deployments for the {owner} workspace and every project in it can have a preview for each pull
196 request and production on g1t.page.
197 {plan && ` $${(plan.plan.monthlyCents / 100).toFixed(0)} a month, including:`}
198 </p>
199 {plan && (
200 <ul className="mt-3 grid gap-1.5 text-sm text-muted sm:grid-cols-2">
201 {plan.plan.includes.map((line) => (
202 <li key={line} className="flex gap-2">
203 <span className="text-accent">✓</span>
204 {line}
205 </li>
206 ))}
207 </ul>
208 )}
209 {plan && <p className="mt-3 text-xs text-faint">{plan.plan.overage}</p>}
210 <div className="mt-4">
211 <ButtonLink variant="accent" to={`/${owner}/-/billing`}>
212 See the plan under Billing
213 </ButtonLink>
214 </div>
215 </section>
216 );
217}
218
219function LiveCard({
220 title,
221 hint,
222 app,
223 off,
224 actions,
225}: {
226 title: string;
227 hint: string;
228 app: { url: string; commit: string; deployedAt: string } | undefined;
229 off: boolean;
230 actions: ReactNode;
231}) {
232 return (
233 <div className="rounded-xl border border-line bg-surface p-5">
234 <h2 className="text-sm font-medium">{title}</h2>
235 <p className="mt-0.5 text-xs text-faint">{off ? "Off for this repository." : hint}</p>
236 {app ? (
237 <>
238 <a href={app.url} className="mt-3 flex items-center gap-1.5 font-mono text-sm text-accent hover:underline">
239 {host(app.url)}
240 <ExternalLink size={12} />
241 </a>
242 <p className="mt-1 text-xs text-faint">
243 <span className="font-mono">{app.commit.slice(0, 8)}</span> · deployed <TimeAgo at={app.deployedAt} />
244 </p>
245 </>
246 ) : (
247 <p className="mt-4 text-sm text-muted">Not up.</p>
248 )}
249 {!off && <div className="mt-4">{actions}</div>}
250 </div>
251 );
252}
253
254function AppActions({ branch, up, busy, compact }: { branch: string | null; up: boolean; busy: boolean; compact?: boolean }) {
255 return (
256 <Form method="post" className="flex items-center gap-2">
257 {branch != null && <input type="hidden" name="branch" value={branch} />}
258 <Button variant="quiet" type="submit" name="intent" value="redeploy" disabled={busy} title="Build again from the current head">
259 <RotateCw size={13} />
260 {!compact && "Redeploy"}
261 </Button>
262 {up && (
263 <Button variant="quiet" type="submit" name="intent" value="take-down" disabled={busy} title="Take it down now">
264 <Trash2 size={13} />
265 {!compact && "Take down"}
266 </Button>
267 )}
268 </Form>
269 );
270}
271
272function BuildRow({ build, base }: { build: Deployment; base: string }) {
273 return (
274 <li>
275 <Link to={`${base}/deployments/${build.id}`} className="flex items-center gap-4 px-4 py-3 text-sm hover:bg-surface">
276 <span className="w-20 shrink-0">
277 <StatusDot status={build.status} />
278 </span>
279 <span className="min-w-0 grow">
280 <span className="block truncate font-medium">
281 {build.kind === "production" ? "Production" : `Preview of ${build.branch}`}
282 <span className="ml-2 font-mono text-xs font-normal text-faint">{build.commit.slice(0, 8)}</span>
283 </span>
284 {build.error && <span className="mt-0.5 block truncate text-xs text-muted">{build.error}</span>}
285 </span>
286 <span className="shrink-0 text-xs text-faint">
287 {build.buildSeconds != null && `${build.buildSeconds} s · `}
288 <TimeAgo at={build.createdAt} />
289 </span>
290 </Link>
291 </li>
292 );
293}