Skip to content

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.sh
538 linesCodeBlameRaw
1import { Bot, Boxes, Cpu, Plus, ServerCog, ShieldAlert, Trash2 } from "lucide-react";
2import { useEffect, useState } from "react";
3import { Form, Link } from "react-router";
4
5import { type RegistrationToken, RUNNER_DOWNLOADS, RUNNER_FILES, RUNNER_IMAGE, type Runner, type RunnerGroup, type RunnerIsolation } from "@g1t/contracts";
6
7import { agentRunHref, workflowRunHref } from "../lib/runners";
8import type { RunnersAction, RunnersData } from "../lib/runners.server";
9import { CopyLine, EmptyState, ErrorText, Field, Input, SubmitButton, TimeAgo } from "./ui";
10import { Badge, type BadgeTone } from "./ui/badge";
11import { Button } from "./ui/button";
12import { Card } from "./ui/card";
13import { Checkbox } from "./ui/checkbox";
14import { RadioGroup, RadioOption } from "./ui/radio-group";
15import { SelectField } from "./ui/select";
16import { Switch } from "./ui/switch";
17import { Tabs, TabsList, TabsTrigger } from "./ui/tabs";
18import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip";
19
20/** Online and waiting for work reads as Idle; online and working, Busy. */
21export const STATUS: Record<Runner["status"], { dot: string; text: string; label: string }> = {
22 online: { dot: "bg-success", text: "text-success", label: "Idle" },
23 busy: { dot: "bg-info", text: "text-info", label: "Busy" },
24 offline: { dot: "bg-faint", text: "text-muted", label: "Offline" },
25};
26
27/** Where a runner's current work is: its workflow run, or the agent run it is. */
28function workHref(work: NonNullable<Runner["work"]>): string | null {
29 if (!work.runId || !work.repo) return null;
30 return work.kind === "workflow" ? workflowRunHref(work.repo, work.runId) : agentRunHref(work.repo, work.runId);
31}
32
33export const OS_NAMES: Record<Runner["os"], string> = { linux: "Linux", macos: "macOS", windows: "Windows" };
34
35/** How a runner keeps work from its machine, as a badge and what it means. */
36export const ISOLATION: Record<RunnerIsolation, { label: string; tone: BadgeTone; about: string }> = {
37 "nested-container": {
38 label: "Sandboxed",
39 tone: "success",
40 about: "Each job runs in a fresh container inside the runner's own sandboxed container. Neither reaches the machine.",
41 },
42 vm: {
43 label: "VM per job",
44 tone: "success",
45 about: "Each job runs in a throwaway virtual machine of its own, such as Windows Sandbox, with no folder of the machine's.",
46 },
47 container: {
48 label: "Shared Docker",
49 tone: "warn",
50 about: "Each job runs in a container, but the runner shares the machine's Docker Engine, so anything that takes it over controls the machine.",
51 },
52 host: {
53 label: "Unsandboxed",
54 tone: "danger",
55 about: "Jobs run directly on the machine (host mode). It is not given public repositories' work.",
56 },
57};
58
59function IsolationBadge({ isolation }: { isolation: Runner["isolation"] }) {
60 if (!isolation) return null;
61 const level = ISOLATION[isolation];
62 if (!level) return null;
63 return (
64 <Tooltip>
65 <TooltipTrigger asChild>
66 <Badge size="md" tone={level.tone} tabIndex={0}>
67 {level.label}
68 </Badge>
69 </TooltipTrigger>
70 <TooltipContent className="max-w-72">{level.about}</TooltipContent>
71 </Tooltip>
72 );
73}
74
75function Section({ icon, title, about, children }: { icon: React.ReactNode; title: string; about: React.ReactNode; children: React.ReactNode }) {
76 return (
77 <section className="grid gap-x-10 gap-y-4 border-t border-line pt-8 first:border-t-0 first:pt-0 lg:grid-cols-[16rem_1fr]">
78 <div>
79 <h2 className="flex items-center gap-2 font-medium">
80 {icon}
81 {title}
82 </h2>
83 <div className="mt-1 text-sm text-muted">{about}</div>
84 </div>
85 <div className="min-w-0">{children}</div>
86 </section>
87 );
88}
89
90export function RunnerRow({ runner, manage }: { runner: Runner; manage: boolean }) {
91 const status = STATUS[runner.status];
92 const href = runner.work ? workHref(runner.work) : null;
93 return (
94 <li className="flex flex-wrap items-start gap-x-4 gap-y-2 px-4 py-3">
95 <span className={`mt-1.5 size-2 shrink-0 rounded-full ${status.dot}`} aria-hidden />
96 <div className="min-w-0 grow basis-60">
97 <div className="flex flex-wrap items-center gap-x-2 gap-y-1">
98 <span className="font-medium">{runner.name}</span>
99 <span className={`text-xs font-medium ${status.text}`}>{status.label}</span>
100 <IsolationBadge isolation={runner.isolation} />
101 {runner.ephemeral && <Badge size="md">ephemeral</Badge>}
102 {runner.group && <span className="text-xs text-faint">in {runner.group}</span>}
103 </div>
104 <div className="mt-1.5 flex flex-wrap gap-1">
105 {runner.labels.map((label) => (
106 <span key={label} className="rounded-md bg-raised px-1.5 py-0.5 font-mono text-[0.6875rem] text-fg-soft">
107 {label}
108 </span>
109 ))}
110 </div>
111 {runner.work && (
112 <p className="mt-1.5 truncate text-xs text-muted">
113 {runner.work.kind === "workflow" ? "Running " : "Agent work: "}
114 {href ? (
115 <Link to={href} className="text-fg hover:underline">
116 {runner.work.name}
117 </Link>
118 ) : (
119 <span className="text-fg">{runner.work.name}</span>
120 )}
121 {runner.work.repo && <> in {runner.work.repo}</>}
122 </p>
123 )}
124 </div>
125 <div className="ml-6 grow text-xs text-faint sm:ml-0 sm:shrink-0 sm:grow-0 sm:text-right">
126 <div>
127 {OS_NAMES[runner.os]} {runner.arch}
128 {runner.version && <> · {runner.version}</>}
129 </div>
130 <div className="mt-0.5">{runner.lastSeenAt ? <>Seen <TimeAgo at={runner.lastSeenAt} /></> : "Never seen"}</div>
131 </div>
132 {manage && (
133 <Form
134 method="post"
135 className="shrink-0"
136 onSubmit={(event) => {
137 if (!confirm(`Remove ${runner.name}? Its credential stops working at once${runner.work ? ", and the job it is running fails" : ""}.`)) event.preventDefault();
138 }}
139 >
140 <input type="hidden" name="intent" value="remove" />
141 <input type="hidden" name="id" value={runner.id} />
142 <input type="hidden" name="name" value={runner.name} />
143 <SubmitButton
144 icon
145 match={{ intent: "remove", id: runner.id }}
146 aria-label={`Remove ${runner.name}`}
147 variant="ghost"
148 size="inline"
149 className="p-1.5 text-faint hover:text-danger"
150 >
151 <Trash2 size={15} />
152 </SubmitButton>
153 </Form>
154 )}
155 </li>
156 );
157}
158
159type Platform = "linux" | "macos" | "windows" | "docker";
160
161/** The copy-paste steps for one platform. */
162export function installSteps(platform: Platform, token: RegistrationToken | null, arch: "x64" | "arm64" = "x64"): { title: string; lines: string[] }[] {
163 const url = token?.url ?? "https://g1t.sh";
164 const value = token?.token ?? "<registration token>";
165 const register = (exe: string) => `${exe} register --url ${url} --token ${value}`;
166 switch (platform) {
167 case "linux": {
168 const file = arch === "arm64" ? RUNNER_FILES["linux-arm64"] : RUNNER_FILES["linux-x64"];
169 return [
170 { title: "Download", lines: [`curl -fsSLo g1t-runner ${RUNNER_DOWNLOADS}/latest/${file} && chmod +x g1t-runner`] },
171 { title: "Register", lines: [register("./g1t-runner")] },
172 { title: "Run it, or install it as a service", lines: ["./g1t-runner run", "sudo ./g1t-runner service install"] },
173 ];
174 }
175 case "macos": {
176 const file = arch === "x64" ? RUNNER_FILES["macos-x64"] : RUNNER_FILES["macos-arm64"];
177 return [
178 { title: "Download", lines: [`curl -fsSLo g1t-runner ${RUNNER_DOWNLOADS}/latest/${file} && chmod +x g1t-runner`] },
179 { title: "Register", lines: [register("./g1t-runner")] },
180 { title: "Run it, or install it as a launch agent", lines: ["./g1t-runner run", "./g1t-runner service install"] },
181 ];
182 }
183 case "windows":
184 return [
185 {
186 title: "Download (PowerShell)",
187 lines: [`Invoke-WebRequest ${RUNNER_DOWNLOADS}/latest/${RUNNER_FILES["windows-x64"]} -OutFile g1t-runner.exe`],
188 },
189 { title: "Register", lines: [register(".\\g1t-runner.exe")] },
190 { title: "Run it, or install it as a service (as Administrator)", lines: [".\\g1t-runner.exe run", ".\\g1t-runner.exe service install"] },
191 ];
192 case "docker":
193 return [
194 {
195 title: "Register and run under Sysbox, each job in a container inside the runner's",
196 lines: [
197 `docker run -d --name g1t-runner --restart unless-stopped --runtime=sysbox-runc -v g1t-runner:/data ${RUNNER_IMAGE} register-and-run --url ${url} --token ${value}`,
198 ],
199 },
200 ];
201 }
202}
203
204export function NewRunner({ token, groups, repoScoped }: { token: RegistrationToken | null; groups: RunnerGroup[]; repoScoped: boolean }) {
205 const [platform, setPlatform] = useState<Platform>("linux");
206 const [arch, setArch] = useState<"x64" | "arm64">("x64");
207 return (
208 <Card className="space-y-4 p-4">
209 {!token ? (
210 <Form method="post" className="flex flex-wrap items-end gap-3">
211 <input type="hidden" name="intent" value="token" />
212 {!repoScoped && groups.length > 1 && (
213 <Field label="Group">
214 <SelectField
215 name="group"
216 defaultValue={groups.find((g) => g.default)?.name ?? groups[0]?.name}
217 className="w-auto min-w-40"
218 options={groups.map((group) => ({ value: group.name, label: group.name }))}
219 />
220 </Field>
221 )}
222 <SubmitButton match={{ intent: "token" }} pending="Making a token…">
223 <Plus size={15} />
224 New runner
225 </SubmitButton>
226 <p className="basis-full text-xs text-faint">Makes a registration token that lasts an hour and registers any number of runners. It is shown once.</p>
227 </Form>
228 ) : (
229 <p className="text-sm">
230 Registration token made. It expires <TimeAgoFuture at={token.expiresAt} />
231 {token.group ? <>; its runners join {token.group}</> : null}. Copy it now: it is not shown again.
232 </p>
233 )}
234 <Tabs value={platform} onValueChange={(value) => setPlatform(value as Platform)}>
235 <div className="flex flex-wrap items-center gap-3">
236 <TabsList aria-label="Where the runner runs">
237 <TabsTrigger value="linux">Linux</TabsTrigger>
238 <TabsTrigger value="macos">macOS</TabsTrigger>
239 <TabsTrigger value="windows">Windows</TabsTrigger>
240 <TabsTrigger value="docker">Docker</TabsTrigger>
241 </TabsList>
242 {(platform === "linux" || platform === "macos") && (
243 <TabsList aria-label="Architecture">
244 <button
245 type="button"
246 onClick={() => setArch("x64")}
247 className={`rounded-md px-2.5 py-1 text-xs font-medium ${arch === "x64" ? "bg-raised text-fg" : "text-muted hover:text-fg"}`}
248 >
249 {platform === "macos" ? "Intel" : "x64"}
250 </button>
251 <button
252 type="button"
253 onClick={() => setArch("arm64")}
254 className={`rounded-md px-2.5 py-1 text-xs font-medium ${arch === "arm64" ? "bg-raised text-fg" : "text-muted hover:text-fg"}`}
255 >
256 {platform === "macos" ? "Apple silicon" : "arm64"}
257 </button>
258 </TabsList>
259 )}
260 </div>
261 </Tabs>
262 <ol className="space-y-3">
263 {installSteps(platform, token, platform === "macos" && arch === "x64" ? "x64" : arch).map((step, at) => (
264 <li key={step.title}>
265 <p className="mb-1.5 text-xs text-muted">
266 {at + 1}. {step.title}
267 </p>
268 <div className="space-y-1.5">
269 {step.lines.map((line) => (
270 <CopyLine key={line} text={line} prompt />
271 ))}
272 </div>
273 </li>
274 ))}
275 </ol>
276 <p className="text-xs text-faint">
277 Each job runs in a container of its own; on Linux, run the Docker command under Sysbox so neither the runner nor its jobs reach the machine.{" "}
278 <code className="font-mono">--no-docker</code>, which runs jobs on the machine itself, is deprecated. Add{" "}
279 <code className="font-mono">--labels gpu,cuda</code> for jobs to ask for, or <code className="font-mono">--ephemeral</code> for a runner that takes
280 one job and removes itself.{" "}
281 <a href="https://docs.g1t.sh/guides/self-hosted-runners/" className="underline underline-offset-2">
282 Self-hosted runners
283 </a>
284 </p>
285 </Card>
286 );
287}
288
289/** "in 59m", for a time still to come. */
290function TimeAgoFuture({ at }: { at: string }) {
291 const minutes = Math.max(0, Math.round((new Date(at).getTime() - Date.now()) / 60_000));
292 return (
293 <time dateTime={at} suppressHydrationWarning>
294 {minutes >= 60 ? "in an hour" : `in ${minutes}m`}
295 </time>
296 );
297}
298
299export function Groups({
300 groups,
301 repositories,
302 manage,
303 action,
304}: {
305 groups: RunnerGroup[];
306 repositories: string[];
307 manage: boolean;
308 action: RunnersAction | undefined;
309}) {
310 const [editing, setEditing] = useState<string | null>(null);
311 // A group saved closes its form: the list above now shows it as saved.
312 useEffect(() => {
313 if (action?.notice) setEditing(null);
314 }, [action]);
315 return (
316 <div className="space-y-3">
317 <Card asChild divided>
318 <ul>
319 {groups.map((group) => (
320 <li key={group.id} className="px-4 py-3">
321 <div className="flex flex-wrap items-center gap-x-3 gap-y-1">
322 <span className="font-medium">{group.name}</span>
323 {group.default && <Badge size="md">default</Badge>}
324 <span className="text-xs text-muted">
325 {group.runners} {group.runners === 1 ? "runner" : "runners"} ·{" "}
326 {group.repositories.length === 0 ? "every repository" : group.repositories.join(", ")}
327 </span>
328 {manage && (
329 <span className="ml-auto flex items-center gap-2">
330 <Button type="button" variant="outline" onClick={() => setEditing(editing === group.id ? null : group.id)}>
331 {editing === group.id ? "Close" : "Edit"}
332 </Button>
333 {!group.default && (
334 <Form method="post" onSubmit={(event) => { if (!confirm(`Delete ${group.name}? Its runners join the default group.`)) event.preventDefault(); }}>
335 <input type="hidden" name="intent" value="delete-group" />
336 <input type="hidden" name="id" value={group.id} />
337 <SubmitButton variant="outline" match={{ intent: "delete-group", id: group.id }} pending="Deleting…">
338 Delete
339 </SubmitButton>
340 </Form>
341 )}
342 </span>
343 )}
344 </div>
345 {editing === group.id && <GroupForm group={group} repositories={repositories} />}
346 </li>
347 ))}
348 </ul>
349 </Card>
350 {manage && (editing === "new" ? <GroupForm group={null} repositories={repositories} /> : (
351 <Button type="button" variant="outline" onClick={() => setEditing("new")}>
352 <Plus size={15} />
353 New group
354 </Button>
355 ))}
356 </div>
357 );
358}
359
360function GroupForm({ group, repositories }: { group: RunnerGroup | null; repositories: string[] }) {
361 const [reach, setReach] = useState(group && group.repositories.length > 0 ? "some" : "all");
362 return (
363 <Card asChild tone="bg" radius="lg" className="mt-3 space-y-3 p-3">
364 <Form method="post">
365 <input type="hidden" name="intent" value="group" />
366 {group && <input type="hidden" name="id" value={group.id} />}
367 <Field label="Name">
368 <Input name="name" defaultValue={group?.name ?? ""} placeholder="GPU machines" maxLength={64} required={!group} />
369 </Field>
370 <fieldset className="space-y-2 text-sm">
371 <legend className="mb-1 text-xs text-muted">Repositories that may use its runners</legend>
372 <RadioGroup name="reach" value={reach} onValueChange={setReach}>
373 <RadioOption value="all" label="Every repository in the workspace" />
374 <RadioOption value="some" label="Only these" />
375 </RadioGroup>
376 {reach === "some" && (
377 <div className="grid gap-1 pl-6 sm:grid-cols-2">
378 {repositories.map((name) => (
379 <label key={name} className="flex min-h-8 cursor-pointer items-center gap-2 font-mono text-xs">
380 <Checkbox name="repository" value={name} defaultChecked={group?.repositories.includes(name)} />
381 {name}
382 </label>
383 ))}
384 </div>
385 )}
386 </fieldset>
387 <SubmitButton match={{ intent: "group", id: group?.id }} pending="Saving…">
388 Save group
389 </SubmitButton>
390 </Form>
391 </Card>
392 );
393}
394
395export function RunnerSettingsForm({ data, manage, scope }: { data: RunnersData; manage: boolean; scope: "workspace" | "project" }) {
396 const settings = data.settings;
397 const [agents, setAgents] = useState(settings?.agentsOnSelfHosted ?? false);
398 const [forks, setForks] = useState(settings?.forkPullRequests ?? false);
399 if (!settings) return null;
400 // The intent comes from the button pressed: a hidden one before it would
401 // win the form's first "intent", and Follow the workspace would only save.
402 return (
403 <Form method="post" className="space-y-4">
404 {scope === "project" && settings.inherited && (
405 <p className="text-sm text-muted">These are the workspace&apos;s settings. Saving here gives this project its own.</p>
406 )}
407 <Card asChild className="flex items-start justify-between gap-4 p-4">
408 <label>
409 <span className="min-w-0">
410 <span className="flex items-center gap-2 text-sm font-medium">
411 <Bot size={15} className="text-muted" />
412 Run g1t's work on self-hosted runners
413 </span>
414 <span className="mt-1 block text-sm text-muted">
415 Agent runs, checks, reviews and the merge queue run on your runners with these labels instead of g1t&apos;s sandboxes. The machine time is
416 free; the model is paid as before, through g1t&apos;s model proxy, or by your own provider if you connected one. Agent work needs Docker, or a
417 Linux runner.
418 </span>
419 <span className="mt-3 block max-w-sm">
420 <Input name="agentLabels" defaultValue={settings.agentLabels.join(", ")} disabled={!manage} aria-label="Labels for agent work" />
421 </span>
422 </span>
423 <Switch name="agents" checked={agents} onCheckedChange={setAgents} disabled={!manage} aria-label="Run g1t's work on self-hosted runners" />
424 </label>
425 </Card>
426 <label className={`flex items-start justify-between gap-4 rounded-xl border p-4 ${forks ? "border-danger/50 bg-danger/5" : "border-line bg-surface"}`}>
427 <span className="min-w-0">
428 <span className="flex items-center gap-2 text-sm font-medium">
429 <ShieldAlert size={15} className={forks ? "text-danger" : "text-muted"} />
430 Let pull requests from forks use self-hosted runners
431 </span>
432 <span className="mt-1 block text-sm text-muted">
433 Off by default, and best left off. Anyone who can open a pull request could then run any code on your machines, read what they can
434 reach, and leave something behind for the next job. Their jobs never get secrets either way.
435 </span>
436 </span>
437 <Switch name="forks" checked={forks} onCheckedChange={setForks} disabled={!manage} />
438 </label>
439 <p className="text-xs text-faint">
440 g1t&apos;s network guardrails are not enforced on your own machines: a job or an agent there reaches whatever the machine can.
441 </p>
442 {manage && (
443 <div className="flex flex-wrap gap-3">
444 <SubmitButton name="intent" value="settings" pending="Saving…">
445 Save
446 </SubmitButton>
447 {scope === "project" && !settings.inherited && (
448 <SubmitButton name="intent" value="inherit" variant="outline" pending="Following…">
449 Follow the workspace
450 </SubmitButton>
451 )}
452 </div>
453 )}
454 </Form>
455 );
456}
457
458/** Settings, Runners: a workspace's, or a project's own. */
459export function RunnersPanel({
460 data,
461 action,
462 scope,
463 manage,
464}: {
465 data: RunnersData;
466 action: RunnersAction | undefined;
467 scope: "workspace" | "project";
468 manage: boolean;
469}) {
470 const online = data.runners.filter((runner) => runner.status !== "offline").length;
471 return (
472 <div className="max-w-5xl space-y-8">
473 <div className="min-h-6">
474 {action?.notice && <p className="text-sm text-success">{action.notice}</p>}
475 <ErrorText>{action?.error ?? data.error ?? null}</ErrorText>
476 </div>
477 <Section
478 icon={<ServerCog size={15} className="text-muted" />}
479 title="Runners"
480 about={
481 <>
482 <p>
483 Your own machines, for jobs with <code className="font-mono text-fg">runs-on: self-hosted</code>. Their time is free on every plan.
484 </p>
485 <p className="mt-2">
486 {data.runners.length === 0 ? "None yet." : `${online} of ${data.runners.length} online.`}
487 </p>
488 </>
489 }
490 >
491 {data.runners.length === 0 ? (
492 <EmptyState title="No self-hosted runners">
493 Add one below. It connects out to g1t; nothing needs to reach the machine.
494 </EmptyState>
495 ) : (
496 <Card asChild divided>
497 <ul>
498 {data.runners.map((runner) => (
499 <RunnerRow key={runner.id} runner={runner} manage={manage && (scope === "workspace" || runner.repo !== null)} />
500 ))}
501 </ul>
502 </Card>
503 )}
504 </Section>
505 {manage && (
506 <Section
507 icon={<Plus size={15} className="text-muted" />}
508 title="New runner"
509 about="Download g1t-runner, register it with a token made here, and start it. Linux, macOS and Windows, x64 and arm64."
510 >
511 <NewRunner token={action?.token ?? null} groups={data.groups} repoScoped={scope === "project"} />
512 </Section>
513 )}
514 {scope === "workspace" && (
515 <Section
516 icon={<Boxes size={15} className="text-muted" />}
517 title="Groups"
518 about="Which repositories may use which runners. A runner joins the default group, every repository, unless its token names another."
519 >
520 <Groups groups={data.groups} repositories={data.repositories} manage={manage} action={action} />
521 </Section>
522 )}
523 <Section
524 icon={<Cpu size={15} className="text-muted" />}
525 title="Where work runs"
526 about={scope === "workspace" ? "For every project in the workspace, unless a project says otherwise." : "For this project."}
527 >
528 {/* Keyed to what is saved, so following the workspace again shows its settings, not the switches as they were. */}
529 <RunnerSettingsForm
530 key={data.settings ? `${data.settings.inherited}:${data.settings.agentsOnSelfHosted}:${data.settings.forkPullRequests}:${data.settings.agentLabels.join(",")}` : "none"}
531 data={data}
532 manage={manage}
533 scope={scope}
534 />
535 </Section>
536 </div>
537 );
538}