Skip to content

Commit 49cb340

Browse files
authored
feat(service): support custom domains on preview branches (#354)
## Purpose of change Let users manage custom domains on preview and staging branches through `service domain --branch <name>`. ## What's changed and why - Remove the production-only guard shared by add, show, retry, wait, and delete. Domain commands still default to production when `--branch` is omitted. - Keep service selection, domain lookup, and recovery actions scoped to the selected branch app. Recovery commands include the resolved project and service IDs plus the quoted branch name. Invalid-hostname and invalid-timeout recovery also preserve a supplied branch before target selection. - Update help, deployment-required advice, and product docs. Remove the retired `SERVICE.BRANCH_NOT_DEPLOYABLE` error. - Cover feature and staging registration and the other four domain commands with branch app and domain IDs. - Use the API recovery hint for custom-domain quota errors. The workspace hourly limit tells users to wait; deleting a domain does not restore that budget. Inventory quota errors without a hint keep the existing deletion guidance. - Use one private recovery-command formatter across the error paths so project, service, and branch selectors cannot drift. Regression tests cover missing deployments and DNS failures on a branch name that needs shell quoting. Companion changes: [Console and domain support](prisma/pdp-control-plane#5746) and [public docs](prisma/web#8514). The REST API already accepts branch apps; the Console restriction is removed by the platform PR. Publish the docs after the platform change is deployed and this CLI change is released. <!-- cli-domain-terminal-examples:start --> ## Terminal examples Captured from this PR's current head (`54b4ea48389004d000508070c0ca1c05a1f57d43`) with the in-process CLI harness and synthetic API responses. ANSI colors are removed. These are actual CLI-rendered examples, not live DNS or certificate tests. The successful fixture assumes the required DNS records already exist. TXT/Cloudflare-specific CLI guidance is a separate change in [#370](#370). ### Add and wait on staging `--branch` selects the app for each invocation. Keep the same selectors on follow-up commands; omitting `--branch` still defaults to production. ```console $ prisma service domain add staging.example.com --project acme-app --service hello-world --branch staging ✔ Added staging.example.com to hello-world. workspace: Acme Inc project: acme-app branch: staging service: hello-world hostname: staging.example.com status: provisioning_tls dns CNAME: staging.example.com -> switchboard.ewr.prisma.build (ttl 300) → Wait for activation: prisma service domain wait staging.example.com → Show the domain: prisma service domain show staging.example.com $ prisma service domain wait staging.example.com --project acme-app --service hello-world --branch staging staging.example.com: active ✔ staging.example.com is live at https://staging.example.com workspace: Acme Inc project: acme-app branch: staging service: hello-world hostname: staging.example.com status: active ``` Both commands exit with status `0`. The `branch: staging` row confirms the selected branch. ### DNS recovery keeps the selected branch A branch name with spaces is quoted in the recovery command. The command also carries the resolved project and service IDs, so retrying targets the same app. ```console $ prisma service domain add staging.example.com --project acme-app --service hello-world --branch 'feature/domain fix' ✘ [SERVICE.DOMAIN_DNS_NOT_CONFIGURED] DNS is not configured for "staging.example.com" why: Create a CNAME to switchboard.ewr.prisma.build first. → Add CNAME staging.example.com -> switchboard.ewr.prisma.build at your DNS provider, then rerun the domain command. → Add the domain: prisma service domain add staging.example.com --service svc_staging --project proj_1 --branch 'feature/domain fix' ``` Exit status: `2`. ### The hourly budget gives wait guidance The API's recovery hint tells users to wait. Deleting an existing domain does not restore the hourly creation budget. ```console $ prisma service domain add staging.example.com --project acme-app --service hello-world --branch staging ✘ [SERVICE.DOMAIN_QUOTA_EXCEEDED] Custom domain quota exceeded why: Failed to add custom domain: This workspace can create 10 custom domains per hour. Try again in 60 minutes. Wait before adding another custom domain to this workspace. → Wait before adding another custom domain to this workspace. ``` Exit status: `2`. <!-- cli-domain-terminal-examples:end --> ## Testing notes / Before-after Before: any domain command with a preview branch failed before calling the API. After: `prisma service domain add staging.example.com --service web --branch staging` targets that branch's app. Passed: - Full CLI package suite: 1,048 passed, one existing skipped test. - Root `pnpm typecheck` (nine tasks), `pnpm format`, `pnpm lint`, and `pnpm check:error-reference`. - Local end-to-end tests: seven passed. The 48 live API tests were skipped because their service token is not configured. Root `pnpm test` and a serial retry stop at the unchanged CLI-engine test “consent with a token: a wrong answer re-prompts instead of failing” after its five-second timeout. That test also fails in isolation, without loading the changed CLI command code. The full changed CLI package suite passes separately. API calls are exercised through the in-process CLI test harness. Live DNS and TLS issuance were not exercised.
1 parent de663bc commit 49cb340

12 files changed

Lines changed: 621 additions & 116 deletions

File tree

‎docs/product/command-principles.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,23 @@ When a group acts on a subordinate resource, the action may be a compound verb p
146146

147147
That is still one action in the command grammar. It is preferable to inventing a temporary top-level `deployment` group for the MVP.
148148

149+
## Custom domains
150+
151+
`service domain` commands manage domains for the service on the selected branch.
152+
They default to `production`; `--branch <name>` selects a preview or staging
153+
branch. The service must have a promoted, running version before a domain can
154+
be added. Domain lookup, retry, waiting, and deletion stay within that service.
155+
156+
New domain attachments require a persistent workspace TXT ownership record as
157+
well as the routing CNAME. Missing proof returns the exact DNS instructions and
158+
a command to retry on the same branch. Existing connected domains keep working;
159+
recreating a branch in the same workspace reuses its TXT record.
160+
161+
Cloudflare proxy detection returns `SERVICE.DOMAIN_CLOUDFLARE_PROXY` with DNS-only
162+
instructions and a retry command for the same target. The customer can turn the
163+
proxy back on after the domain is Active, with Full (strict) SSL/TLS and the
164+
ACME challenge path accessible for certificate renewal.
165+
149166
## Shared Flag Conventions
150167

151168
Shared CLI flag rules:

‎docs/product/error-conventions.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,12 @@ If a production action is blocked or fails, the output should make clear whether
201201

202202
Every error should help the user recover without opening another document first.
203203

204+
Custom-domain input errors preserve an explicitly supplied `--branch` in recovery
205+
commands, even when validation fails before the service is selected. Recovery must
206+
not silently switch a preview or staging request to production.
207+
208+
Custom-domain quota errors use the API's recovery hint when supplied. An hourly
209+
creation limit requires waiting; deleting a domain does not restore that budget.
210+
Without an API hint, the CLI keeps its advice to free an existing domain slot.
211+
204212
If a condition is expected and actionable, document it as a structured failure. If it is a bug, do not hide it behind a fake user-facing success envelope.

‎docs/reference/error-reference.md‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,6 @@ A `project` or `branch` group command was invoked with unusable arguments — fo
473473

474474
A `--branch` flag was passed with an empty or whitespace-only value to a service command; the check runs before any resolution because a blank value must never fall through to the default-branch behavior of omitting the flag. The fix is to pass a non-empty branch name, or omit `--branch` to target the default branch. Meta: none.
475475

476-
### SERVICE.BRANCH_NOT_DEPLOYABLE
477-
478-
A `service domain` command was pointed at a non-production branch, which the domain-target resolver refuses because custom domains on preview branches are not supported in Public Beta. The fix is to use `--branch production`, or attach the domain after promoting to the production branch. Meta: none.
479-
480476
### SERVICE.DELETE_FAILED
481477

482478
`service delete` called the platform's app-teardown API and it rejected; the underlying error's message becomes `why` and the original error is carried in `cause`. Next actions point at `service show` and `service version list` for the service. Meta: none.
@@ -497,13 +493,30 @@ The general "Management API call failed" wrapper for the `service` command famil
497493

498494
The hostname given to a `service domain` command is not a usable custom domain — raised either by local validation before any API call (protocol, path, port, wildcard, single label, or bad DNS labels) or when `service domain add` gets a plain HTTP 400 rejection from the domain API. The fix is to pass a bare hostname such as `shop.acme.com`. Meta: `status`, `apiCode`, `hint` (API-rejection path only; none for local validation).
499495

496+
### SERVICE.DOMAIN_OWNERSHIP_REQUIRED
497+
498+
The API could not find the TXT record proving that this workspace may attach the
499+
hostname. Add the exact name and value in the API hint, keep the TXT record for
500+
future attachments in this workspace, and rerun the suggested command. Recovery
501+
preserves the selected service, project, and branch. This does not mean that the
502+
service needs another deployment. Meta: `status`, `apiCode`, `hint`.
503+
504+
### SERVICE.DOMAIN_CLOUDFLARE_PROXY
505+
506+
The API found Cloudflare proxy addresses while the hostname's routing check
507+
failed. Set that DNS record to DNS only (gray cloud) and rerun the suggested
508+
command. After the domain is Active, Proxied can be enabled again with Full
509+
(strict) SSL/TLS. Keep `/.well-known/acme-challenge/*` accessible without caching
510+
or security challenges so Prisma can renew the certificate. Meta: `status`,
511+
`apiCode`, `hint`.
512+
500513
### SERVICE.DOMAIN_NOT_FOUND
501514

502515
A `service domain` command targeted a hostname that is not attached to the resolved service — raised when the service's domain listing has no matching hostname, or when a show/delete/retry/wait call gets HTTP 404. The fix is to check the hostname and service, or add the domain first. Meta: none.
503516

504517
### SERVICE.DOMAIN_QUOTA_EXCEEDED
505518

506-
`service domain add` was refused because the custom-domain quota is reached — HTTP 429, or a 409 whose text mentions a quota, maximum, or limit. The fix is to delete an existing custom domain before adding another. Meta: `status`, `apiCode`, `hint`.
519+
`service domain add` was refused because the custom-domain quota is reached — HTTP 429, or a 409 whose text mentions a quota, maximum, or limit. The CLI uses the API's recovery hint when supplied, including waiting for an hourly creation budget. Without a hint, it advises deleting an existing custom domain before adding another. Meta: `status`, `apiCode`, `hint`.
507520

508521
### SERVICE.DOMAIN_RETRY_NOT_ELIGIBLE
509522

@@ -551,7 +564,7 @@ A `service logs` response body ended without the terminal record that closes a p
551564

552565
### SERVICE.NO_VERSIONS
553566

554-
The resolved service has no usable version for the command — raised by `service open` when the service has no versions, by `service logs` when it has no live version, and by `service domain add` when the API answers 422 because the production service has no promoted version that can receive a custom domain. The fix on the domain path is to promote a version on the production branch first, then add the domain again. Meta: `status`, `apiCode`, `hint` (domain-add path only; otherwise none).
567+
The resolved service has no usable version for the command — raised by `service open` when the service has no versions, by `service logs` when it has no live version, and by `service domain add` when the API answers 422 because the selected service has no promoted version that can receive a custom domain. The fix on the domain path is to promote a version on the selected branch first, then add the domain again. Meta: `status`, `apiCode`, `hint` (domain-add path only; otherwise none).
555568

556569
### SERVICE.SELECTION_INVALID
557570

‎packages/cli/src/cli.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export const cliGroups: Readonly<
294294
brief:
295295
"Manage custom domains for a service. Register hostnames, drive DNS and TLS verification, inspect status",
296296
description:
297-
"Custom domains point hostnames you own at a service's production branch. After add, create the DNS record the platform reports; wait and retry drive DNS verification and TLS provisioning to done.",
297+
"Custom domains point hostnames you own at a service on any branch. Commands default to production; use --branch to select another branch. After add, create the DNS record the platform reports; wait and retry drive DNS verification and TLS provisioning to done.",
298298
workflow: [
299299
{
300300
run: "service domain add shop.acme.com --service my-api",

‎packages/cli/src/commands/service/domain-add.ts‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ import {
1212

1313
export const serviceDomainAddCommand = defineCommand({
1414
help: {
15-
summary: "Register a custom domain on the service's production branch",
15+
summary: "Register a custom domain on the selected service branch",
1616
description:
17-
"Points a hostname you own at the service instead of its generated URL. After registering, create the DNS record the platform asks for, then 'service domain wait' blocks until verification and TLS provisioning finish.",
18-
examples: ["service domain add shop.acme.com --service my-service"],
17+
"Points a hostname you own at the service instead of its generated URL. Add the routing CNAME and workspace TXT record shown by Console or the verification error, then rerun this command. Keep the TXT record for future attachments in this workspace. Use 'service domain wait' to wait for TLS provisioning.",
18+
examples: [
19+
"service domain add shop.acme.com --service my-service",
20+
"service domain add staging.acme.com --service my-service --branch staging",
21+
],
1922
},
2023
args: domainTargetArgs(),
2124
needs: { credentials: true },
2225
handler: async (args, ctx) => {
23-
const hostname = normalizeDomainHostname(args.positionals.hostname);
26+
const hostname = normalizeDomainHostname(
27+
args.positionals.hostname,
28+
args.flags.branch,
29+
);
2430
const target = await resolveServiceDomainTarget(ctx, {
2531
serviceName: args.flags.service,
2632
projectRef: args.flags.project,
@@ -35,7 +41,7 @@ export const serviceDomainAddCommand = defineCommand({
3541
signal: ctx.signal,
3642
})
3743
.catch((error) => {
38-
throw domainCommandError("add", error, hostname);
44+
throw domainCommandError("add", error, hostname, target.resultTarget);
3945
});
4046

4147
const result: ServiceDomainAddResult = {

‎packages/cli/src/commands/service/domain-delete.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export const serviceDomainDeleteCommand = defineCommand({
2323
args: domainTargetArgs(),
2424
needs: { credentials: true },
2525
handler: async (args, ctx) => {
26-
const hostname = normalizeDomainHostname(args.positionals.hostname);
26+
const hostname = normalizeDomainHostname(
27+
args.positionals.hostname,
28+
args.flags.branch,
29+
);
2730
const target = await resolveServiceDomainTarget(ctx, {
2831
serviceName: args.flags.service,
2932
projectRef: args.flags.project,
@@ -32,7 +35,7 @@ export const serviceDomainDeleteCommand = defineCommand({
3235
});
3336
const domain = await resolveDomainByHostname(
3437
target.provider,
35-
target.service.id,
38+
target.resultTarget,
3639
hostname,
3740
"delete",
3841
ctx.signal,
@@ -53,7 +56,12 @@ export const serviceDomainDeleteCommand = defineCommand({
5356
await target.provider
5457
.removeDomain(domain.id, { signal: ctx.signal })
5558
.catch((error) => {
56-
throw domainCommandError("delete", error, hostname);
59+
throw domainCommandError(
60+
"delete",
61+
error,
62+
hostname,
63+
target.resultTarget,
64+
);
5765
});
5866

5967
const result: ServiceDomainDeleteResult = {

‎packages/cli/src/commands/service/domain-retry.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export const serviceDomainRetryCommand = defineCommand({
2121
args: domainTargetArgs(),
2222
needs: { credentials: true },
2323
handler: async (args, ctx) => {
24-
const hostname = normalizeDomainHostname(args.positionals.hostname);
24+
const hostname = normalizeDomainHostname(
25+
args.positionals.hostname,
26+
args.flags.branch,
27+
);
2528
const target = await resolveServiceDomainTarget(ctx, {
2629
serviceName: args.flags.service,
2730
projectRef: args.flags.project,
@@ -30,15 +33,15 @@ export const serviceDomainRetryCommand = defineCommand({
3033
});
3134
const domain = await resolveDomainByHostname(
3235
target.provider,
33-
target.service.id,
36+
target.resultTarget,
3437
hostname,
3538
"retry",
3639
ctx.signal,
3740
);
3841
const retried = await target.provider
3942
.retryDomain(domain.id, { signal: ctx.signal })
4043
.catch((error) => {
41-
throw domainCommandError("retry", error, hostname);
44+
throw domainCommandError("retry", error, hostname, target.resultTarget);
4245
});
4346

4447
const result: ServiceDomainRetryResult = {

‎packages/cli/src/commands/service/domain-show.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ export const serviceDomainShowCommand = defineCommand({
2121
args: domainTargetArgs(),
2222
needs: { credentials: true },
2323
handler: async (args, ctx) => {
24-
const hostname = normalizeDomainHostname(args.positionals.hostname);
24+
const hostname = normalizeDomainHostname(
25+
args.positionals.hostname,
26+
args.flags.branch,
27+
);
2528
const target = await resolveServiceDomainTarget(ctx, {
2629
serviceName: args.flags.service,
2730
projectRef: args.flags.project,
@@ -30,15 +33,15 @@ export const serviceDomainShowCommand = defineCommand({
3033
});
3134
const domain = await resolveDomainByHostname(
3235
target.provider,
33-
target.service.id,
36+
target.resultTarget,
3437
hostname,
3538
"show",
3639
ctx.signal,
3740
);
3841
const detail = await target.provider
3942
.showDomain(domain.id, { signal: ctx.signal })
4043
.catch((error) => {
41-
throw domainCommandError("show", error, hostname);
44+
throw domainCommandError("show", error, hostname, target.resultTarget);
4245
});
4346

4447
const result: ServiceDomainShowResult = {

‎packages/cli/src/commands/service/domain-wait.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const UNIT_MULTIPLIER_MS: Record<string, number> = {
2626
h: 3_600_000,
2727
};
2828

29-
function parseWaitTimeout(value: string | undefined): number {
29+
function parseWaitTimeout(
30+
value: string | undefined,
31+
branchName?: string,
32+
): number {
3033
if (!value) {
3134
return DEFAULT_TIMEOUT_MS;
3235
}
@@ -36,7 +39,7 @@ function parseWaitTimeout(value: string | undefined): number {
3639
}
3740
const match = DURATION.exec(trimmed);
3841
if (!match) {
39-
throw timeoutInvalidError(value);
42+
throw timeoutInvalidError(value, branchName);
4043
}
4144
const amount = Number.parseInt(match[1] as string, 10);
4245
const multiplier = UNIT_MULTIPLIER_MS[match[2] as string] ?? 1;
@@ -97,8 +100,11 @@ export const serviceDomainWaitCommand = defineCommand({
97100
},
98101
needs: { credentials: true },
99102
handler: async (args, ctx) => {
100-
const hostname = normalizeDomainHostname(args.positionals.hostname);
101-
const timeoutMs = parseWaitTimeout(args.flags.timeout);
103+
const hostname = normalizeDomainHostname(
104+
args.positionals.hostname,
105+
args.flags.branch,
106+
);
107+
const timeoutMs = parseWaitTimeout(args.flags.timeout, args.flags.branch);
102108
const target = await resolveServiceDomainTarget(ctx, {
103109
serviceName: args.flags.service,
104110
projectRef: args.flags.project,
@@ -107,7 +113,7 @@ export const serviceDomainWaitCommand = defineCommand({
107113
});
108114
const domain = await resolveDomainByHostname(
109115
target.provider,
110-
target.service.id,
116+
target.resultTarget,
111117
hostname,
112118
"wait",
113119
ctx.signal,
@@ -147,11 +153,19 @@ export const serviceDomainWaitCommand = defineCommand({
147153
}
148154

149155
if (current.status === "failed") {
150-
throw domainVerificationFailedError(hostname, current);
156+
throw domainVerificationFailedError(
157+
hostname,
158+
current,
159+
target.resultTarget,
160+
);
151161
}
152162

153163
if (timeoutMs === 0 || Date.now() >= deadline) {
154-
throw domainVerificationTimeoutError(hostname, current.status);
164+
throw domainVerificationTimeoutError(
165+
hostname,
166+
current.status,
167+
target.resultTarget,
168+
);
155169
}
156170

157171
// biome-ignore lint/performance/noAwaitInLoops: this polls one domain until it settles, reporting each status change in the order it happened; the sleep between reads keeps the management API request rate down.
@@ -162,7 +176,12 @@ export const serviceDomainWaitCommand = defineCommand({
162176
current = await target.provider
163177
.showDomain(current.id, { signal: ctx.signal })
164178
.catch((error) => {
165-
throw domainCommandError("wait", error, hostname);
179+
throw domainCommandError(
180+
"wait",
181+
error,
182+
hostname,
183+
target.resultTarget,
184+
);
166185
});
167186
}
168187
},

0 commit comments

Comments
 (0)