Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update CI messaging #6777

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changeset/serious-houses-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: Update CI messaging
57 changes: 51 additions & 6 deletions packages/wrangler/src/__tests__/match-tag.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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.]`
);
});

Expand All @@ -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.]`
);
});

Expand All @@ -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"\`]`
);
});

Expand All @@ -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.]`
);
});

Expand All @@ -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.]`
);
});

Expand All @@ -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"\`]`
);
});
});
Expand Down
6 changes: 5 additions & 1 deletion packages/wrangler/src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 14 additions & 4 deletions packages/wrangler/src/match-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ 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();

Expand All @@ -15,6 +17,14 @@ export async function 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 {
Expand All @@ -27,11 +37,11 @@ export async function verifyWorkerMatchesCITag(
// 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."
);
}
}
Expand All @@ -40,7 +50,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.`
);
}
}
6 changes: 5 additions & 1 deletion packages/wrangler/src/versions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading