| 1 | import { ExternalLink, Globe, Rocket, RotateCw, Trash2 } from "lucide-react"; |
| 2 | import type { ReactNode } from "react"; |
| 3 | import { Form, Link } 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 { ButtonLink, ComputeNote, EmptyState, ErrorText, SubmitButton, TimeAgo } from "../../components/ui"; |
| 12 | import { Hint } from "../../components/ui/hint"; |
| 13 | import { computeNoteFor } from "../../lib/compute.server"; |
| 14 | import { neverDeploys } from "../../lib/project-kind"; |
| 15 | import { billing, deployments, projects } from "../../lib/services.server"; |
| 16 | import { assertSameOrigin, getViewer, requireUser, unwrap } from "../../lib/session.server"; |
| 17 | import { refusal, requireRepo } from "../../lib/access.server"; |
| 18 | import { whyNot } from "../../lib/access"; |
| 19 | import { buildError, groupBuilds, type BuildGroup } from "../../lib/deployments"; |
| 20 | |
| 21 | export function meta({ params, ...args }: Route.MetaArgs) { |
| 22 | return page(args, { title: `Deployments · ${params.owner}/${params.repo} · g1t` }); |
| 23 | } |
| 24 | |
| 25 | export async function loader({ params, context }: Route.LoaderArgs) { |
| 26 | const viewer = getViewer(context); |
| 27 | // Anyone who can read the repository: a public one's to anyone, a private one's to people with a role. |
| 28 | const { access } = await requireRepo(context, params, "read"); |
| 29 | const ref = { workspace: params.owner, slug: params.repo }; |
| 30 | const [settings, list, features, computeNote, project] = await Promise.all([ |
| 31 | deployments.settings(ref, viewer), |
| 32 | deployments.list(ref, viewer), |
| 33 | billing.features(params.owner, viewer), |
| 34 | // Builds are compute: said before a deploy is refused for it. |
| 35 | computeNoteFor(params.owner, "deploy"), |
| 36 | projects.get(params.owner, params.repo, viewer).catch(() => null), |
| 37 | ]); |
| 38 | // Set not to deploy, in General settings: turning them on waits for that to change. |
| 39 | const notDeploying = project?.ok ? neverDeploys(project.value) : false; |
| 40 | // Deployments come with the g1t plan. Someone outside the workspace does |
| 41 | // not see its plans; the page works without, and the deployments service |
| 42 | // refuses a deploy the plan does not cover. |
| 43 | const plan = features.ok |
| 44 | ? (features.value.find((state) => state.plan.feature === "plan" || state.plan.feature === "deployments") ?? null) |
| 45 | : null; |
| 46 | return { can: access.can, settings: unwrap(settings), ...unwrap(list), plan, computeNote, notDeploying }; |
| 47 | } |
| 48 | |
| 49 | export async function action({ request, params, context }: Route.ActionArgs) { |
| 50 | assertSameOrigin(request); |
| 51 | const user = requireUser(context, request); |
| 52 | const form = await request.formData(); |
| 53 | const ref = { workspace: params.owner, slug: params.repo }; |
| 54 | const intent = form.get("intent"); |
| 55 | const branch = form.get("branch") ? String(form.get("branch")) : null; |
| 56 | // Deploying is compute (Write); turning deployments on or off is a deployment setting (Admin). |
| 57 | const refused = await refusal(context, params, intent === "enable" || intent === "disable" ? "manage_integrations" : "run"); |
| 58 | if (refused) return { error: refused }; |
| 59 | if (intent === "redeploy") { |
| 60 | const started = await deployments.redeploy(user, ref, branch); |
| 61 | return started.ok ? { notice: "Build started." } : { error: started.error.message }; |
| 62 | } |
| 63 | if (intent === "take-down") { |
| 64 | const done = await deployments.takeDown(user, ref, branch); |
| 65 | return done.ok ? { notice: "Taken down." } : { error: done.error.message }; |
| 66 | } |
| 67 | if (intent === "enable" || intent === "disable") { |
| 68 | const saved = await deployments.updateSettings(user, ref, { enabled: intent === "enable" }); |
| 69 | return saved.ok |
| 70 | ? { notice: intent === "enable" ? "Deployments are on. Production is building." : "Deployments are off, and every app is down." } |
| 71 | : { error: saved.error.message }; |
| 72 | } |
| 73 | return { error: "Unknown request." }; |
| 74 | } |
| 75 | |
| 76 | export default function RepoDeployments({ loaderData, actionData, params }: Route.ComponentProps) { |
| 77 | const { settings, deployments: builds, live, plan, can } = loaderData; |
| 78 | const base = `/${params.owner}/${params.repo}`; |
| 79 | const production = live.find((app) => app.kind === "production"); |
| 80 | const previews = live.filter((app) => app.kind === "preview"); |
| 81 | |
| 82 | return ( |
| 83 | <div className="mx-auto max-w-5xl"> |
| 84 | <header className="flex flex-wrap items-start justify-between gap-4"> |
| 85 | <div> |
| 86 | <h1 className="flex items-center gap-2 text-xl font-semibold tracking-tight"> |
| 87 | <Rocket size={18} className="text-accent" /> |
| 88 | Deployments |
| 89 | </h1> |
| 90 | <p className="mt-1 max-w-2xl text-sm text-muted"> |
| 91 | Every pull request gets a live preview on g1t.page, and the default branch goes to production on each push. |
| 92 | Apps run on Cloudflare only while someone visits them.{" "} |
| 93 | <a href="https://docs.g1t.sh/guides/deployments/" className="text-fg hover:underline"> |
| 94 | How it works |
| 95 | </a> |
| 96 | </p> |
| 97 | </div> |
| 98 | {settings.enabled && ( |
| 99 | <a |
| 100 | href={settings.primaryDomain ? `https://${settings.primaryDomain}` : settings.productionUrl} |
| 101 | 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" |
| 102 | > |
| 103 | <Globe size={13} /> |
| 104 | {settings.primaryDomain ?? host(settings.productionUrl)} |
| 105 | </a> |
| 106 | )} |
| 107 | </header> |
| 108 | |
| 109 | <div className="mt-4 min-h-6"> |
| 110 | {actionData && "notice" in actionData && <p className="text-sm text-success">{actionData.notice}</p>} |
| 111 | <ErrorText>{actionData && "error" in actionData ? actionData.error : null}</ErrorText> |
| 112 | {!actionData && <ComputeNote note={loaderData.computeNote} />} |
| 113 | </div> |
| 114 | |
| 115 | {plan != null && !plan.on && !plan.included && live.length === 0 ? ( |
| 116 | <PlanNeeded plan={plan} owner={params.owner} /> |
| 117 | ) : !settings.enabled ? ( |
| 118 | <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6"> |
| 119 | <h2 className="font-medium">Deployments are off</h2> |
| 120 | <p className="mt-1 max-w-2xl text-sm text-muted"> |
| 121 | Each project turns deployments on for itself; until then nothing builds or runs. Once on, production goes |
| 122 | up at <span className="font-mono text-fg">{host(settings.productionUrl)}</span> and every open pull request |
| 123 | gets its own preview. Workers projects (a <code className="font-mono text-fg">wrangler.jsonc</code>) and |
| 124 | static sites deploy without configuration. |
| 125 | </p> |
| 126 | {loaderData.notDeploying ? ( |
| 127 | <p className="mt-4 max-w-2xl text-sm text-muted"> |
| 128 | This project is set to be something that doesn't deploy, such as a library or a tool. To deploy it, choose |
| 129 | App or site, deployed on g1t, in{" "} |
| 130 | <Link to={`/${params.owner}/${params.repo}/settings#kind`} className="text-fg underline underline-offset-4"> |
| 131 | its settings |
| 132 | </Link>{" "} |
| 133 | first. |
| 134 | </p> |
| 135 | ) : ( |
| 136 | <Form method="post" className="mt-4"> |
| 137 | <Hint label={whyNot(can, "manage_integrations")} disabled={!can.manage_integrations}> |
| 138 | <SubmitButton variant="accent" name="intent" value="enable" pending="Turning on…" disabled={!can.manage_integrations}> |
| 139 | <Rocket size={14} /> |
| 140 | Turn on deployments |
| 141 | </SubmitButton> |
| 142 | </Hint> |
| 143 | </Form> |
| 144 | )} |
| 145 | </section> |
| 146 | ) : ( |
| 147 | <> |
| 148 | <section className="mt-2 grid gap-4 md:grid-cols-2"> |
| 149 | <LiveCard |
| 150 | title="Production" |
| 151 | hint="The default branch, on every push." |
| 152 | app={production} |
| 153 | off={!settings.production} |
| 154 | actions={ |
| 155 | can.run ? <AppActions branch={null} up={!!production} /> : null |
| 156 | } |
| 157 | /> |
| 158 | <div className="rounded-xl border border-line bg-surface p-5"> |
| 159 | <h2 className="text-sm font-medium">Previews</h2> |
| 160 | <p className="mt-0.5 text-xs text-faint"> |
| 161 | {settings.previews |
| 162 | ? `One per branch with an open pull request; down when it closes or after ${settings.idleDays} days without a visit.` |
| 163 | : "Off for this repository."} |
| 164 | </p> |
| 165 | {previews.length === 0 ? ( |
| 166 | <p className="mt-4 text-sm text-muted">No previews are up.</p> |
| 167 | ) : ( |
| 168 | <ul className="mt-3 divide-y divide-line"> |
| 169 | {previews.map((app) => ( |
| 170 | <li key={app.url} className="flex items-center gap-3 py-2 text-sm"> |
| 171 | <span className="shrink-0"> |
| 172 | <span className="font-medium">{app.branch}</span> |
| 173 | {app.number != null && ( |
| 174 | <Link to={`${base}/pull/${app.number}`} className="ml-1.5 text-xs text-muted hover:underline"> |
| 175 | #{app.number} |
| 176 | </Link> |
| 177 | )} |
| 178 | </span> |
| 179 | <a href={app.url} className="min-w-0 truncate font-mono text-xs text-muted hover:text-fg"> |
| 180 | {host(app.url)} |
| 181 | </a> |
| 182 | <span className="ml-auto shrink-0"> |
| 183 | {can.run && <AppActions branch={app.branch} up compact />} |
| 184 | </span> |
| 185 | </li> |
| 186 | ))} |
| 187 | </ul> |
| 188 | )} |
| 189 | </div> |
| 190 | </section> |
| 191 | |
| 192 | <h2 className="mt-10 text-sm font-medium text-muted">Recent builds</h2> |
| 193 | <div className="mt-3"> |
| 194 | {builds.length === 0 ? ( |
| 195 | <EmptyState title="No builds yet">Push to the default branch or open a pull request.</EmptyState> |
| 196 | ) : ( |
| 197 | <ul className="divide-y divide-line overflow-hidden rounded-xl border border-line"> |
| 198 | {groupBuilds(builds).map((group) => ( |
| 199 | <BuildRow key={group.build.id} group={group} base={base} /> |
| 200 | ))} |
| 201 | </ul> |
| 202 | )} |
| 203 | </div> |
| 204 | |
| 205 | <p className="mt-10 text-sm text-muted"> |
| 206 | Build command, output directory, idle days, and turning deployments off are under{" "} |
| 207 | <Link to={`${base}/settings/deployments`} className="text-fg hover:underline"> |
| 208 | Settings → Deployments |
| 209 | </Link> |
| 210 | . Secrets and config for builds and running apps are under{" "} |
| 211 | <Link to={`${base}/settings/secrets`} className="text-fg hover:underline"> |
| 212 | Settings → Secrets and variables |
| 213 | </Link> |
| 214 | , and production's own domains under{" "} |
| 215 | <Link to={`${base}/settings/domains`} className="text-fg hover:underline"> |
| 216 | Settings → Domains |
| 217 | </Link> |
| 218 | . |
| 219 | </p> |
| 220 | </> |
| 221 | )} |
| 222 | </div> |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | function PlanNeeded({ plan, owner }: { plan: FeatureState | null; owner: string }) { |
| 227 | return ( |
| 228 | <section className="mt-2 rounded-xl border border-accent/30 bg-accent/5 p-6"> |
| 229 | <h2 className="font-medium">Deployments are part of a paid plan</h2> |
| 230 | <p className="mt-1 max-w-2xl text-sm text-muted"> |
| 231 | Turn on Deployments for the {owner} workspace and every project in it can have a preview for each pull |
| 232 | request and production on g1t.page. |
| 233 | {plan && ` $${(plan.plan.monthlyCents / 100).toFixed(0)} a month, including:`} |
| 234 | </p> |
| 235 | {plan && ( |
| 236 | <ul className="mt-3 grid gap-1.5 text-sm text-muted sm:grid-cols-2"> |
| 237 | {plan.plan.includes.map((line) => ( |
| 238 | <li key={line} className="flex gap-2"> |
| 239 | <span className="text-success">✓</span> |
| 240 | {line} |
| 241 | </li> |
| 242 | ))} |
| 243 | </ul> |
| 244 | )} |
| 245 | {plan && <p className="mt-3 text-xs text-faint">{plan.plan.overage}</p>} |
| 246 | <div className="mt-4"> |
| 247 | <ButtonLink variant="accent" to={`/${owner}/-/billing`}> |
| 248 | See the plan under Billing |
| 249 | </ButtonLink> |
| 250 | </div> |
| 251 | </section> |
| 252 | ); |
| 253 | } |
| 254 | |
| 255 | function LiveCard({ |
| 256 | title, |
| 257 | hint, |
| 258 | app, |
| 259 | off, |
| 260 | actions, |
| 261 | }: { |
| 262 | title: string; |
| 263 | hint: string; |
| 264 | app: { url: string; commit: string; deployedAt: string } | undefined; |
| 265 | off: boolean; |
| 266 | actions: ReactNode; |
| 267 | }) { |
| 268 | return ( |
| 269 | <div className="rounded-xl border border-line bg-surface p-5"> |
| 270 | <h2 className="text-sm font-medium">{title}</h2> |
| 271 | <p className="mt-0.5 text-xs text-faint">{off ? "Off for this repository." : hint}</p> |
| 272 | {app ? ( |
| 273 | <> |
| 274 | <a href={app.url} className="mt-3 flex items-center gap-1.5 font-mono text-sm text-accent hover:underline"> |
| 275 | {host(app.url)} |
| 276 | <ExternalLink size={12} /> |
| 277 | </a> |
| 278 | <p className="mt-1 text-xs text-faint"> |
| 279 | <span className="font-mono">{app.commit.slice(0, 8)}</span> · deployed <TimeAgo at={app.deployedAt} /> |
| 280 | </p> |
| 281 | </> |
| 282 | ) : ( |
| 283 | <p className="mt-4 text-sm text-muted">Not up.</p> |
| 284 | )} |
| 285 | {!off && <div className="mt-4">{actions}</div>} |
| 286 | </div> |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | function AppActions({ branch, up, compact }: { branch: string | null; up: boolean; compact?: boolean }) { |
| 291 | // Which app's buttons say they are working: production has no branch. |
| 292 | const app = branch ?? "production"; |
| 293 | return ( |
| 294 | <Form method="post" className="flex items-center gap-2"> |
| 295 | {branch != null && <input type="hidden" name="branch" value={branch} />} |
| 296 | <input type="hidden" name="app" value={app} /> |
| 297 | <Hint label="Build again from the current head"> |
| 298 | <SubmitButton |
| 299 | variant="quiet" |
| 300 | name="intent" |
| 301 | value="redeploy" |
| 302 | match={{ app }} |
| 303 | icon={compact} |
| 304 | pending="Redeploying…" |
| 305 | aria-label={compact ? "Redeploy" : undefined} |
| 306 | > |
| 307 | <RotateCw size={13} /> |
| 308 | {!compact && "Redeploy"} |
| 309 | </SubmitButton> |
| 310 | </Hint> |
| 311 | {up && ( |
| 312 | <Hint label="Take it down now"> |
| 313 | <SubmitButton |
| 314 | variant="quiet" |
| 315 | name="intent" |
| 316 | value="take-down" |
| 317 | match={{ app }} |
| 318 | icon={compact} |
| 319 | pending="Taking down…" |
| 320 | aria-label={compact ? "Take down" : undefined} |
| 321 | > |
| 322 | <Trash2 size={13} /> |
| 323 | {!compact && "Take down"} |
| 324 | </SubmitButton> |
| 325 | </Hint> |
| 326 | )} |
| 327 | </Form> |
| 328 | ); |
| 329 | } |
| 330 | |
| 331 | function BuildRow({ group, base }: { group: BuildGroup<Deployment>; base: string }) { |
| 332 | const { build, count } = group; |
| 333 | const error = buildError(build); |
| 334 | return ( |
| 335 | <li> |
| 336 | <Link to={`${base}/deployments/${build.id}`} className="flex items-center gap-4 px-4 py-3 text-sm hover:bg-surface"> |
| 337 | <span className="w-20 shrink-0"> |
| 338 | <StatusDot status={build.status} /> |
| 339 | {/* A run of the same failure is one row: how many, and when the latest was. */} |
| 340 | {count > 1 && <span className="mt-0.5 block pl-3 text-xs text-muted">{count} times</span>} |
| 341 | </span> |
| 342 | <span className="min-w-0 grow"> |
| 343 | <span className="block truncate font-medium"> |
| 344 | {build.kind === "production" ? "Production" : `Preview of ${build.branch}`} |
| 345 | <span className="ml-2 font-mono text-xs font-normal text-faint">{build.commit.slice(0, 8)}</span> |
| 346 | </span> |
| 347 | {error && <span className="mt-0.5 line-clamp-2 text-xs text-muted sm:line-clamp-1">{error}</span>} |
| 348 | </span> |
| 349 | <span className="shrink-0 text-xs text-faint"> |
| 350 | {count > 1 ? "last " : build.buildSeconds != null && `${build.buildSeconds} s · `} |
| 351 | <TimeAgo at={build.createdAt} /> |
| 352 | </span> |
| 353 | </Link> |
| 354 | </li> |
| 355 | ); |
| 356 | } |