Skip to content

Commit

Permalink
Update CI messaging (#6777)
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Sep 20, 2024
1 parent 3e75612 commit 9649dbc
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 12 deletions.
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
26 changes: 22 additions & 4 deletions packages/wrangler/src/match-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,54 @@ 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 {
const worker = await fetchResult<ServiceMetadataRes>(
`/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."
);
}
}
Expand All @@ -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.`
);
}
}
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

0 comments on commit 9649dbc

Please sign in to comment.