From 9649dbc74d022fa5fdb065cf3e7a8d6d791f0a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Somhairle=20MacLe=C3=B2id?= Date: Fri, 20 Sep 2024 12:22:53 +0100 Subject: [PATCH] Update CI messaging (#6777) --- .changeset/serious-houses-film.md | 5 ++ .../wrangler/src/__tests__/match-tag.test.ts | 57 +++++++++++++++++-- packages/wrangler/src/deploy/index.ts | 6 +- packages/wrangler/src/match-tag.ts | 26 +++++++-- packages/wrangler/src/versions/index.ts | 6 +- 5 files changed, 88 insertions(+), 12 deletions(-) create mode 100644 .changeset/serious-houses-film.md diff --git a/.changeset/serious-houses-film.md b/.changeset/serious-houses-film.md new file mode 100644 index 000000000000..5c200ed2721b --- /dev/null +++ b/.changeset/serious-houses-film.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +chore: Update CI messaging diff --git a/packages/wrangler/src/__tests__/match-tag.test.ts b/packages/wrangler/src/__tests__/match-tag.test.ts index b19b45cee210..7f2c66e84619 100644 --- a/packages/wrangler/src/__tests__/match-tag.test.ts +++ b/packages/wrangler/src/__tests__/match-tag.test.ts @@ -1,3 +1,4 @@ +import { mkdir } from "node:fs/promises"; import { http, HttpResponse } from "msw"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { verifyWorkerMatchesCITag } from "../match-tag"; @@ -87,7 +88,7 @@ describe("match-tag", () => { await expect( verifyWorkerMatchesCITag("some-account-id", "b-worker") ).rejects.toMatchInlineSnapshot( - `[Error: Your Worker's name (b-worker) does not match what is expected by the CI system]` + `[Error: The name in \`wrangler.toml\` (b-worker) must match the name of your Worker. Please update the name field in your wrangler.toml.]` ); }); @@ -97,7 +98,7 @@ describe("match-tag", () => { await expect( verifyWorkerMatchesCITag("some-account-id", "network-error-worker") ).rejects.toMatchInlineSnapshot( - `[Error: Wrangler cannot validate that your Worker name matches what is expected by the CI system]` + `[Error: Wrangler cannot validate that your Worker name matches what is expected by the build system. Please retry the build.]` ); }); @@ -107,7 +108,18 @@ describe("match-tag", () => { await expect( verifyWorkerMatchesCITag("some-account-id", "my-worker") ).rejects.toMatchInlineSnapshot( - `[Error: Your Worker's name (my-worker) does not match what is expected by the CI system]` + `[Error: The name in \`wrangler.toml\` (my-worker) must match the name of your Worker. Please update the name field in your wrangler.toml.]` + ); + }); + + it("throws validation error if account_id mismatches", async () => { + vi.stubEnv("WRANGLER_CI_MATCH_TAG", "abc123a"); + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "some-other-account-id"); + mockWorker("my-worker", "abc123b"); + await expect( + verifyWorkerMatchesCITag("some-account-id", "my-worker") + ).rejects.toMatchInlineSnapshot( + `[Error: The \`account_id\` in \`wrangler.toml\` must match the \`account_id\` for this account. Please update your wrangler.toml with \`account_id = "some-other-account-id"\`]` ); }); @@ -124,7 +136,7 @@ describe("match-tag", () => { await expect( runWrangler("deploy ./index.js") ).rejects.toMatchInlineSnapshot( - `[Error: Your Worker's name (b-worker) does not match what is expected by the CI system]` + `[Error: The name in \`wrangler.toml\` (b-worker) must match the name of your Worker. Please update the name field in your wrangler.toml.]` ); }); @@ -135,7 +147,7 @@ describe("match-tag", () => { await expect( runWrangler("deploy ./index.js") ).rejects.toMatchInlineSnapshot( - `[Error: Wrangler cannot validate that your Worker name matches what is expected by the CI system]` + `[Error: Wrangler cannot validate that your Worker name matches what is expected by the build system. Please retry the build.]` ); }); @@ -146,7 +158,40 @@ describe("match-tag", () => { await expect( runWrangler("deploy ./index.js") ).rejects.toMatchInlineSnapshot( - `[Error: Your Worker's name (my-worker) does not match what is expected by the CI system]` + `[Error: The name in \`wrangler.toml\` (my-worker) must match the name of your Worker. Please update the name field in your wrangler.toml.]` + ); + }); + it("throws validation error if account_id mismatches", async () => { + vi.stubEnv("WRANGLER_CI_MATCH_TAG", "abc123a"); + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "some-other-account-id"); + mockWorker("my-worker", "abc123a"); + writeWranglerToml({ + name: "my-worker", + account_id: "some-account-id", + }); + await expect( + runWrangler("deploy ./index.js") + ).rejects.toMatchInlineSnapshot( + `[Error: The \`account_id\` in \`wrangler.toml\` must match the \`account_id\` for this account. Please update your wrangler.toml with \`account_id = "some-other-account-id"\`]` + ); + }); + + it("throws validation error if account_id mismatches w/ custom wrangler.toml path", async () => { + vi.stubEnv("WRANGLER_CI_MATCH_TAG", "abc123a"); + vi.stubEnv("CLOUDFLARE_ACCOUNT_ID", "some-other-account-id"); + mockWorker("my-worker", "abc123a"); + await mkdir("path"); + writeWranglerToml( + { + name: "my-worker", + account_id: "some-account-id", + }, + "path/config.toml" + ); + await expect( + runWrangler("deploy -c path/config.toml ./index.js") + ).rejects.toMatchInlineSnapshot( + `[Error: The \`account_id\` in \`path/config.toml\` must match the \`account_id\` for this account. Please update your wrangler.toml with \`account_id = "some-other-account-id"\`]` ); }); }); diff --git a/packages/wrangler/src/deploy/index.ts b/packages/wrangler/src/deploy/index.ts index f262e5a3f977..292b483a49f5 100644 --- a/packages/wrangler/src/deploy/index.ts +++ b/packages/wrangler/src/deploy/index.ts @@ -331,7 +331,11 @@ export async function deployHandler(args: DeployArgs) { if (!args.dryRun) { assert(accountId, "Missing account ID"); - await verifyWorkerMatchesCITag(accountId, name); + await verifyWorkerMatchesCITag( + accountId, + name, + path.relative(entry.directory, config.configPath ?? "wrangler.toml") + ); } const { sourceMapSize, versionId, workerTag, targets } = await deploy({ config, diff --git a/packages/wrangler/src/match-tag.ts b/packages/wrangler/src/match-tag.ts index 4329ba0a6525..fa327a81a85a 100644 --- a/packages/wrangler/src/match-tag.ts +++ b/packages/wrangler/src/match-tag.ts @@ -2,19 +2,36 @@ import { fetchResult } from "./cfetch"; import { getCIMatchTag } from "./environment-variables/misc-variables"; import { FatalError } from "./errors"; import { logger } from "./logger"; +import { getCloudflareAccountIdFromEnv } from "./user/auth-variables"; import type { ServiceMetadataRes } from "./init"; export async function verifyWorkerMatchesCITag( accountId: string, - workerName: string + workerName: string, + configPath?: string ) { const matchTag = getCIMatchTag(); + logger.debug( + `Starting verifyWorkerMatchesCITag() with tag: ${matchTag}, name: ${workerName}` + ); + // If no tag is provided through the environment, nothing needs to be verified if (!matchTag) { + logger.debug( + "No WRANGLER_CI_MATCH_TAG variable provided, aborting verifyWorkerMatchesCITag()" + ); return; } + const envAccountID = getCloudflareAccountIdFromEnv(); + + if (accountId !== envAccountID) { + throw new FatalError( + `The \`account_id\` in \`${configPath ?? "wrangler.toml"}\` must match the \`account_id\` for this account. Please update your wrangler.toml with \`account_id = "${envAccountID}"\`` + ); + } + let tag; try { @@ -22,16 +39,17 @@ export async function verifyWorkerMatchesCITag( `/accounts/${accountId}/workers/services/${workerName}` ); tag = worker.default_environment.script.tag; + logger.debug(`API returned with tag: ${tag} for worker: ${workerName}`); } catch (e) { logger.debug(e); // code: 10090, message: workers.api.error.service_not_found if ((e as { code?: number }).code === 10090) { throw new FatalError( - `Your Worker's name (${workerName}) does not match what is expected by the CI system` + `The name in \`${configPath ?? "wrangler.toml"}\` (${workerName}) must match the name of your Worker. Please update the name field in your wrangler.toml.` ); } else { throw new FatalError( - "Wrangler cannot validate that your Worker name matches what is expected by the CI system" + "Wrangler cannot validate that your Worker name matches what is expected by the build system. Please retry the build." ); } } @@ -40,7 +58,7 @@ export async function verifyWorkerMatchesCITag( `Failed to match Worker tag. The API returned "${tag}", but the CI system expected "${matchTag}"` ); throw new FatalError( - `Your Worker's name (${workerName}) does not match what is expected by the CI system` + `The name in \`${configPath ?? "wrangler.toml"}\` (${workerName}) must match the name of your Worker. Please update the name field in your wrangler.toml.` ); } } diff --git a/packages/wrangler/src/versions/index.ts b/packages/wrangler/src/versions/index.ts index 047fc8f29630..e5dc5b8bf1ee 100644 --- a/packages/wrangler/src/versions/index.ts +++ b/packages/wrangler/src/versions/index.ts @@ -268,7 +268,11 @@ export async function versionsUploadHandler( if (!args.dryRun) { assert(accountId, "Missing account ID"); - await verifyWorkerMatchesCITag(accountId, name); + await verifyWorkerMatchesCITag( + accountId, + name, + path.relative(entry.directory, config.configPath ?? "wrangler.toml") + ); } if (!args.dryRun) {