Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/integrations/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ void project rollback [version]

Void restores recorded triggers along with the code. Versions without a complete trigger snapshot keep current routes and schedules. Database recovery is separate: use [`void db restore`](../reference/cli/database.md#void-db-restore) to undo D1 migrations.

#### Clear a deployment with a candidate at 0%

A failed deploy can leave the live version at 100% and a candidate at 0%.

1. Pause other deploys and let any in progress finish. In the Cloudflare dashboard, select the account and Worker named in the error, then open **Deployments**.
2. Confirm **Active Deployment** still shows the exact versions and percentages Void reported. Continue only if the 0% version is unused, including by version overrides.
3. Select **Rollback** on the live version. [Cloudflare's split deployment rollback guide](https://developers.cloudflare.com/workers/versions-and-deployments/rollbacks/#rolling-back-from-a-split-deployment)

Retry when **Active Deployment** lists only the live version. If Void still reports the pair, wait and retry.

Cloudflare can reject a rollback after a secret change (error `10220`).

A newer inactive upload can still block a first link or a static deploy that inherits secrets after rollback. If Void reports a version-history or secret inheritance error, inspect the active and latest Worker versions in Cloudflare before retrying.

#### Scope and limitations

Void deploys native apps, static sites, SPAs, supported SSGs, and Cloudflare builds from TanStack Start, React Router, SvelteKit, Nuxt, Analog, and Astro. The vinext adapters are experimental.
Expand Down
8 changes: 6 additions & 2 deletions packages/deploy-cloudflare/src/existing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isVoidSecretOnlyDraft,
isWranglerSecretOnlyDraft,
} from './secret-draft.ts';
import { cloudflareStagingPairGuidance } from './staging-guidance.ts';

export type ExistingCloudflareWorker = {
versionId: string;
Expand All @@ -24,6 +25,7 @@ type ExistingCloudflareWorkerOptions = {
runWrangler: CloudflareCommandRunner;
configPath: string;
workerName: string;
accountId?: string;
versionId?: string;
};

Expand Down Expand Up @@ -97,7 +99,8 @@ async function readCloudflareWorker(
typeof versions[0].version_id !== 'string'
) {
throw new Error(
'The existing Worker must have one deployed version receiving all traffic. Finish any gradual rollout before linking.',
cloudflareStagingPairGuidance(deployment, options)?.diagnostic ??
'The existing Worker must have one deployed version receiving all traffic. Finish any gradual rollout before linking.',
);
}
const versionId = versions[0].version_id;
Expand Down Expand Up @@ -135,7 +138,8 @@ async function readCloudflareWorker(
: await read({ type: 'deployments.status', ...target });
if (after === null || !isDeepStrictEqual(deployment.versions, after.versions)) {
throw new Error(
'Cloudflare production changed while linking. Retry from the current deployment.',
cloudflareStagingPairGuidance(after, options)?.diagnostic ??
'Cloudflare production changed while linking. Retry from the current deployment.',
);
}
return {
Expand Down
23 changes: 23 additions & 0 deletions packages/deploy-cloudflare/src/staging-guidance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { isRecord } from '@void/deploy-core';
import { readConfigEntryString } from './config.ts';

export function cloudflareStagingPairGuidance(
deployment: Record<string, unknown> | null | undefined,
{ accountId, workerName }: { accountId?: string; workerName: string },
): { diagnostic: string } | undefined {
const versions = deployment?.versions;
const entries: Array<unknown> = Array.isArray(versions) && versions.length === 2 ? versions : [];
const liveEntry = entries.find((entry) => isRecord(entry) && entry.percentage === 100);
const candidateEntry = entries.find((entry) => isRecord(entry) && entry.percentage === 0);
const id = readConfigEntryString(deployment, 'id');
const live = readConfigEntryString(liveEntry, 'version_id')?.trim();
const candidate = readConfigEntryString(candidateEntry, 'version_id')?.trim();
if (accountId && id?.trim() && live && candidate && live !== candidate) {
return {
diagnostic: `Cloudflare Worker "${workerName}" in account ${accountId} has ${live} at 100% and ${candidate} at 0%. A failed deploy can leave this pair.
Pause other deploys and let any in progress finish. Open this Worker's Deployments tab in the Cloudflare dashboard.
If Active Deployment still shows this exact pair and the 0% version is unused, including by version overrides, roll back to the live version. Retry once it's the only version listed. If Void still reports the pair, wait and retry.
https://developers.cloudflare.com/workers/versions-and-deployments/rollbacks/#rolling-back-from-a-split-deployment`,
};
}
}
14 changes: 11 additions & 3 deletions packages/deploy-cloudflare/src/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
VOID_SECRET_DRAFT_MESSAGE,
VOID_SECRET_DRAFT_SOURCE,
} from './secret-draft.ts';
import { cloudflareStagingPairGuidance } from './staging-guidance.ts';

const DURABLE_MIGRATION_VERSION_ERROR = 10_211;
const CLOUDFLARE_SECRET_CHANGED_ERROR = 10_220;
Expand Down Expand Up @@ -117,7 +118,6 @@ export type CloudflareVersionActivation =
| 'triggers-failed';
status?: number;
cloudflareErrorCode?: number;
/** Bounded uploader error text with terminal escapes removed and common credential formats redacted. */
diagnostic?: string;
/** Wrangler reached Cloudflare's Containers entitlement endpoint and received a 403. */
containersUnavailable?: boolean;
Expand Down Expand Up @@ -822,6 +822,7 @@ function isCandidatePreviewUrl(
}

export type CloudflareVersionActivationOptions = {
accountId?: string;
/** Human-readable deployment phases; the caller owns terminal rendering. */
onProgress?: (message: string) => void;
/** Reuse an inspected Worker without republishing its domains or other triggers. */
Expand Down Expand Up @@ -1040,6 +1041,7 @@ async function activateCloudflareVersionWithSecretsFile(
reason: 'deployment-changed',
activeVersionId: owner.kind === 'ok' ? owner.versionId : null,
rollback: 'not-needed',
...cloudflareStagingPairGuidance(owner.deployment, options),
};
}
}
Expand Down Expand Up @@ -1334,6 +1336,7 @@ async function activateCloudflareVersionWithSecretsFile(
reason: 'deployment-changed',
activeVersionId: owner.kind === 'ok' ? owner.versionId : null,
rollback: 'not-needed',
...cloudflareStagingPairGuidance(owner.deployment, options),
};
}
}
Expand Down Expand Up @@ -1505,6 +1508,7 @@ async function activateCloudflareVersionWithSecretsFile(
activeVersionId: ownerAfterWait.kind === 'ok' ? ownerAfterWait.versionId : null,
rollback: 'not-needed',
triggerRepair: 'succeeded',
...cloudflareStagingPairGuidance(ownerAfterWait.deployment, options),
};
}
}
Expand Down Expand Up @@ -1743,6 +1747,7 @@ async function activateCloudflareVersionWithSecretsFile(
activeVersionId: ownerBeforeActivation.versionId,
rollback: 'not-needed',
triggerRepair: 'succeeded',
...cloudflareStagingPairGuidance(ownerBeforeActivation.deployment, options),
};
}

Expand Down Expand Up @@ -2370,6 +2375,7 @@ async function bootstrapFirstDeployment(
reason: 'deployment-changed',
activeVersionId: current.kind === 'ok' ? current.versionId : null,
rollback: 'not-needed',
...cloudflareStagingPairGuidance(current.deployment, options),
};
}
}
Expand Down Expand Up @@ -2788,7 +2794,7 @@ async function readCurrentProductionVersion(
Parameters<typeof activateCloudflareVersion>[0],
'configPath' | 'runWrangler' | 'workerName'
>,
): Promise<CurrentProductionVersion> {
): Promise<CurrentProductionVersion & { deployment?: Record<string, unknown> }> {
const result = await options.runWrangler(
{ type: 'deployments.status', configPath: options.configPath, workerName: options.workerName },
{ capture: true },
Expand Down Expand Up @@ -2838,6 +2844,7 @@ async function readCurrentProductionVersion(
kind: 'ok',
versionId: active?.versionId ?? null,
versions,
deployment,
};
} catch {
return { kind: 'failed', status: result.status };
Expand Down Expand Up @@ -2915,7 +2922,7 @@ async function stageCandidateForReadiness(
async function verifyReadinessStagingOwner(
options: Pick<
Parameters<typeof activateCloudflareVersion>[0],
'configPath' | 'runWrangler' | 'workerName'
'accountId' | 'configPath' | 'runWrangler' | 'workerName'
>,
previousVersionId: string | null,
versionId: string,
Expand Down Expand Up @@ -2952,6 +2959,7 @@ async function verifyReadinessStagingOwner(
previousVersionId,
activeVersionId: owner.versionId,
rollback: 'not-needed',
...cloudflareStagingPairGuidance(owner.deployment, options),
};
}
return null;
Expand Down
88 changes: 88 additions & 0 deletions packages/deploy-cloudflare/test/unit/existing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,94 @@ describe('existing Worker preservation', () => {
expect(runWrangler.mock.calls[1][0]).toContain('original');
});

describe('staging-pair guidance', () => {
const live = { version_id: 'original', percentage: 100 };
const candidate = { version_id: 'staged', percentage: 0 };
const pair = { id: 'deployment', versions: [candidate, live] };

function inspect(...deployments: Array<object>) {
const runWrangler = operationFixture((args: Array<string>) => ({
status: 0,
stderr: '',
stdout: JSON.stringify(
args[0] === 'deployments'
? deployments.shift()
: {
id: 'original',
resources: {
bindings: worker.bindings,
script: worker.script,
script_runtime: worker.runtime,
},
},
),
}));
return readExistingCloudflareWorker({
runWrangler,
accountId: 'test-account',
workerName: 'site',
configPath: '/app/wrangler.jsonc',
});
}

it('explains the staging pair on the first Worker status read', async () => {
await expect(inspect(pair)).rejects.toThrow(
'Cloudflare Worker "site" in account test-account has original at 100% and staged at 0%',
);
});

it('trims both IDs in the staging-pair guidance', async () => {
const padded = {
...pair,
versions: [
{ ...live, version_id: ' original ' },
{ ...candidate, version_id: ' staged ' },
],
};
await expect(inspect(padded)).rejects.toThrow(
'Cloudflare Worker "site" in account test-account has original at 100% and staged at 0%',
);
});

it('explains the staging pair when it appears during Worker inspection', async () => {
await expect(inspect({ versions: [live] }, pair)).rejects.toThrow(
'Cloudflare Worker "site" in account test-account has original at 100% and staged at 0%',
);
});

it.each([
{
name: 'gradual rollout',
...pair,
versions: [
{ ...live, percentage: 90 },
{ ...candidate, percentage: 10 },
],
},
{ name: 'missing deployment ID', versions: [live, candidate] },
{
name: 'extra version',
...pair,
versions: [live, candidate, { ...candidate, version_id: 'extra' }],
},
{
name: 'duplicate version',
...pair,
versions: [live, { ...candidate, version_id: live.version_id }],
},
{
name: 'version IDs that match after trimming',
...pair,
versions: [live, { ...candidate, version_id: ' original ' }],
},
{ name: 'string percentage', ...pair, versions: [live, { ...candidate, percentage: '0' }] },
])('keeps the generic refusal for $name', async ({ name: _name, ...deployment }) => {
await expect(inspect(deployment)).rejects.toThrow(
'The existing Worker must have one deployed version receiving all traffic.',
);
});
});

it('does not infer an active migration tag from an explicitly requested candidate', async () => {
const runWrangler = operationFixture(
(_args: Array<string>, _options: { capture: boolean }) => ({
Expand Down
Loading