g1t/apps/web/app/routes/repo/deployments.tsx
| 1 | import { ExternalLink, Globe, Rocket, RotateCw, Trash2 } from "lucide-react"; |
| 2 | import type { ReactNode } from "react"; |
| 3 | import { Form, Link, data, useNavigation } from "react-router"; |
| 4 | |
| 5 | import type { Deployment, FeatureState } from "@g1t/contracts"; |
| 6 | |
| 7 | import { host, StatusDot } from "../../components/deploy"; |
| 8 | |
| 9 | import type { Route } from "./+types/deployments"; |
| 10 | import { page } from "../../lib/meta"; |
| 11 | import { Button, ButtonLink, EmptyState, ErrorText, TimeAgo } from "../../components/ui"; |
| 12 | import { billing, deployments } from "../../lib/services.server"; |
| 13 | import { assertSameOrigin, getViewer, requireUser, roleIn, unwrap } from "../../lib/session.server"; |
| 14 | |
| 15 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 16 | return page(args, { title: `Deployments · ${params.owner}/${params.repo} · g1t` }); |
| 17 | } |
| 18 | |
| 19 | export 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 | |
| 34 | export 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 | |
| 58 | export 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.primaryDomain ? `https://${settings.primaryDomain}` : 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 | {settings.primaryDomain ?? 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 | , and production's own domains under{" "} |
| 183 | <Link to={`${base}/settings/domains`} className="text-fg hover:underline"> |
| 184 | Settings → Domains |
| 185 | </Link> |
| 186 | . |
| 187 | </p> |
| 188 | </> |
| 189 | )} |
| 190 | </div> |
| 191 | ); |
| 192 | } |
| 193 | |
| 194 | function PlanNeeded({ plan, owner }: { plan: FeatureState | null; owner: string }) { |
| 195 | return ( |
| 196 | <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6"> |
| 197 | <h2 className="font-medium">Deployments are part of a paid plan</h2> |
| 198 | <p className="mt-1 max-w-2xl text-sm text-muted"> |
| 199 | Turn on Deployments for the {owner} workspace and every project in it can have a preview for each pull |
| 200 | request and production on g1t.page. |
| 201 | {plan && ` $${(plan.plan.monthlyCents / 100).toFixed(0)} a month, including:`} |
| 202 | </p> |
| 203 | {plan && ( |
| 204 | <ul className="mt-3 grid gap-1.5 text-sm text-muted sm:grid-cols-2"> |
| 205 | {plan.plan.includes.map((line) => ( |
| 206 | <li key={line} className="flex gap-2"> |
| 207 | <span className="text-accent">✓</span> |
| 208 | {line} |
| 209 | </li> |
| 210 | ))} |
| 211 | </ul> |
| 212 | )} |
| 213 | {plan && <p className="mt-3 text-xs text-faint">{plan.plan.overage}</p>} |
| 214 | <div className="mt-4"> |
| 215 | <ButtonLink variant="accent" to={`/${owner}/-/billing`}> |
| 216 | See the plan under Billing |
| 217 | </ButtonLink> |
| 218 | </div> |
| 219 | </section> |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | function LiveCard({ |
| 224 | title, |
| 225 | hint, |
| 226 | app, |
| 227 | off, |
| 228 | actions, |
| 229 | }: { |
| 230 | title: string; |
| 231 | hint: string; |
| 232 | app: { url: string; commit: string; deployedAt: string } | undefined; |
| 233 | off: boolean; |
| 234 | actions: ReactNode; |
| 235 | }) { |
| 236 | return ( |
| 237 | <div className="rounded-xl border border-line bg-surface p-5"> |
| 238 | <h2 className="text-sm font-medium">{title}</h2> |
| 239 | <p className="mt-0.5 text-xs text-faint">{off ? "Off for this repository." : hint}</p> |
| 240 | {app ? ( |
| 241 | <> |
| 242 | <a href={app.url} className="mt-3 flex items-center gap-1.5 font-mono text-sm text-accent hover:underline"> |
| 243 | {host(app.url)} |
| 244 | <ExternalLink size={12} /> |
| 245 | </a> |
| 246 | <p className="mt-1 text-xs text-faint"> |
| 247 | <span className="font-mono">{app.commit.slice(0, 8)}</span> · deployed <TimeAgo at={app.deployedAt} /> |
| 248 | </p> |
| 249 | </> |
| 250 | ) : ( |
| 251 | <p className="mt-4 text-sm text-muted">Not up.</p> |
| 252 | )} |
| 253 | {!off && <div className="mt-4">{actions}</div>} |
| 254 | </div> |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | function AppActions({ branch, up, busy, compact }: { branch: string | null; up: boolean; busy: boolean; compact?: boolean }) { |
| 259 | return ( |
| 260 | <Form method="post" className="flex items-center gap-2"> |
| 261 | {branch != null && <input type="hidden" name="branch" value={branch} />} |
| 262 | <Button variant="quiet" type="submit" name="intent" value="redeploy" disabled={busy} title="Build again from the current head"> |
| 263 | <RotateCw size={13} /> |
| 264 | {!compact && "Redeploy"} |
| 265 | </Button> |
| 266 | {up && ( |
| 267 | <Button variant="quiet" type="submit" name="intent" value="take-down" disabled={busy} title="Take it down now"> |
| 268 | <Trash2 size={13} /> |
| 269 | {!compact && "Take down"} |
| 270 | </Button> |
| 271 | )} |
| 272 | </Form> |
| 273 | ); |
| 274 | } |
| 275 | |
| 276 | function BuildRow({ build, base }: { build: Deployment; base: string }) { |
| 277 | return ( |
| 278 | <li> |
| 279 | <Link to={`${base}/deployments/${build.id}`} className="flex items-center gap-4 px-4 py-3 text-sm hover:bg-surface"> |
| 280 | <span className="w-20 shrink-0"> |
| 281 | <StatusDot status={build.status} /> |
| 282 | </span> |
| 283 | <span className="min-w-0 grow"> |
| 284 | <span className="block truncate font-medium"> |
| 285 | {build.kind === "production" ? "Production" : `Preview of ${build.branch}`} |
| 286 | <span className="ml-2 font-mono text-xs font-normal text-faint">{build.commit.slice(0, 8)}</span> |
| 287 | </span> |
| 288 | {build.error && <span className="mt-0.5 block truncate text-xs text-muted">{build.error}</span>} |
| 289 | </span> |
| 290 | <span className="shrink-0 text-xs text-faint"> |
| 291 | {build.buildSeconds != null && `${build.buildSeconds} s · `} |
| 292 | <TimeAgo at={build.createdAt} /> |
| 293 | </span> |
| 294 | </Link> |
| 295 | </li> |
| 296 | ); |
| 297 | } |