Skip to content

Commit b1966df

Browse files
authored
BANDA-631 Add worker name override for workers CI (#7990)
1 parent b2dca9a commit b1966df

File tree

8 files changed

+141
-2
lines changed

8 files changed

+141
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Add WRANGLER_CI_OVERRIDE_NAME for Workers CI

packages/wrangler/src/__tests__/deploy.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
printBundleSize,
1515
printOffendingDependencies,
1616
} from "../deployment-bundle/bundle-reporter";
17+
import { clearOutputFilePath } from "../output";
1718
import { writeAuthConfigFile } from "../user";
1819
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
1920
import { mockAuthDomain } from "./helpers/mock-auth-domain";
@@ -90,6 +91,7 @@ describe("deploy", () => {
9091
afterEach(() => {
9192
vi.unstubAllGlobals();
9293
clearDialogs();
94+
clearOutputFilePath();
9395
});
9496

9597
it("should output log file with deployment details", async () => {
@@ -177,6 +179,85 @@ describe("deploy", () => {
177179
expect(std.err).toMatchInlineSnapshot(`""`);
178180
});
179181

182+
it("should successfully override name with WRANGLER_CI_OVERRIDE_NAME", async () => {
183+
vi.stubEnv("WRANGLER_CI_OVERRIDE_NAME", "test-name");
184+
writeWorkerSource();
185+
writeWranglerConfig({
186+
name: "name-to-override",
187+
workers_dev: true,
188+
});
189+
mockServiceScriptData({
190+
scriptName: "test-name",
191+
script: { id: "test-name", tag: "abc123" },
192+
});
193+
mockUploadWorkerRequest();
194+
mockSubDomainRequest();
195+
mockGetWorkerSubdomain({ enabled: false });
196+
mockUpdateWorkerSubdomain({ enabled: true });
197+
198+
await runWrangler("deploy ./index.js");
199+
expect(std.out).toMatchInlineSnapshot(`
200+
"Total Upload: xx KiB / gzip: xx KiB
201+
Worker Startup Time: 100 ms
202+
Uploaded test-name (TIMINGS)
203+
Deployed test-name triggers (TIMINGS)
204+
https://test-name.test-sub-domain.workers.dev
205+
Current Version ID: Galaxy-Class"
206+
`);
207+
expect(std.err).toMatchInlineSnapshot(`""`);
208+
});
209+
210+
it("should successfully override name with WRANGLER_CI_OVERRIDE_NAME and outputs the correct output file", async () => {
211+
vi.stubEnv("WRANGLER_OUTPUT_FILE_DIRECTORY", "override-output");
212+
vi.stubEnv("WRANGLER_OUTPUT_FILE_PATH", "");
213+
vi.stubEnv("WRANGLER_CI_OVERRIDE_NAME", "test-name");
214+
writeWorkerSource();
215+
writeWranglerConfig({
216+
name: "override-name",
217+
workers_dev: true,
218+
});
219+
mockUploadWorkerRequest();
220+
mockSubDomainRequest();
221+
mockGetWorkerSubdomain({ enabled: true });
222+
223+
await runWrangler("deploy ./index.js --env staging");
224+
expect(std.out).toMatchInlineSnapshot(`
225+
"Total Upload: xx KiB / gzip: xx KiB
226+
Worker Startup Time: 100 ms
227+
Uploaded test-name (TIMINGS)
228+
Deployed test-name triggers (TIMINGS)
229+
https://test-name.test-sub-domain.workers.dev
230+
Current Version ID: Galaxy-Class"
231+
`);
232+
expect(std.err).toMatchInlineSnapshot(`""`);
233+
234+
const outputFilePaths = fs.readdirSync("override-output");
235+
236+
expect(outputFilePaths.length).toEqual(1);
237+
expect(outputFilePaths[0]).toMatch(/wrangler-output-.+\.json/);
238+
const outputFile = fs.readFileSync(
239+
path.join("override-output", outputFilePaths[0]),
240+
"utf8"
241+
);
242+
const entries = outputFile
243+
.split("\n")
244+
.filter(Boolean)
245+
.map((e) => JSON.parse(e));
246+
247+
expect(entries.find((e) => e.type === "deploy")).toMatchObject({
248+
targets: ["https://test-name.test-sub-domain.workers.dev"],
249+
// Omitting timestamp for matching
250+
// timestamp: ...
251+
type: "deploy",
252+
version: 1,
253+
version_id: "Galaxy-Class",
254+
worker_name: "test-name",
255+
worker_tag: "tag:test-name",
256+
worker_name_overridden: true,
257+
wrangler_environment: "staging",
258+
});
259+
});
260+
180261
it("should resolve wrangler.toml relative to the entrypoint", async () => {
181262
fs.mkdirSync("./some-path/worker", { recursive: true });
182263
fs.writeFileSync(

packages/wrangler/src/__tests__/output.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ describe("writeOutput()", () => {
8686
worker_tag: "ABCDE12345",
8787
version_id: "1234",
8888
targets: undefined,
89+
worker_name_overridden: false,
90+
wrangler_environment: undefined,
8991
});
9092

9193
const outputFile = readFileSync(WRANGLER_OUTPUT_FILE_PATH, "utf8");
@@ -104,6 +106,8 @@ describe("writeOutput()", () => {
104106
worker_tag: "ABCDE12345",
105107
version_id: "1234",
106108
targets: undefined,
109+
worker_name_overridden: false,
110+
wrangler_environment: undefined,
107111
},
108112
]);
109113
});
@@ -151,6 +155,8 @@ describe("writeOutput()", () => {
151155
worker_tag: "ABCDE12345",
152156
version_id: "1234",
153157
targets: undefined,
158+
worker_name_overridden: false,
159+
wrangler_environment: undefined,
154160
});
155161

156162
const outputFilePaths = readdirSync("output");
@@ -172,6 +178,8 @@ describe("writeOutput()", () => {
172178
worker_tag: "ABCDE12345",
173179
version_id: "1234",
174180
targets: undefined,
181+
worker_name_overridden: false,
182+
wrangler_environment: undefined,
175183
},
176184
]);
177185
});

packages/wrangler/src/deploy/index.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "node:path";
33
import { getAssetsOptions, validateAssetsArgsAndConfig } from "../assets";
44
import { configFileName, readConfig } from "../config";
55
import { getEntry } from "../deployment-bundle/entry";
6+
import { getCIOverrideName } from "../environment-variables/misc-variables";
67
import { UserError } from "../errors";
78
import { run } from "../experimental-flags";
89
import { logger } from "../logger";
@@ -336,7 +337,18 @@ async function deployWorker(args: DeployArgs) {
336337
}
337338

338339
const beforeUpload = Date.now();
339-
const name = getScriptName(args, config);
340+
let name = getScriptName(args, config);
341+
342+
const ciOverrideName = getCIOverrideName();
343+
let workerNameOverridden = false;
344+
if (ciOverrideName !== undefined && ciOverrideName !== name) {
345+
logger.warn(
346+
`Failed to match Worker name. Your config file is using the Worker name "${name}", but the CI system expected "${ciOverrideName}". Overriding using the CI provided Worker name. Workers Builds connected builds will attempt to open a pull request to resolve this config name mismatch.`
347+
);
348+
name = ciOverrideName;
349+
workerNameOverridden = true;
350+
}
351+
340352
assert(
341353
name,
342354
'You need to provide a name when publishing a worker. Either pass it as a cli arg with `--name <name>` or in your config file as `name = "<name>"`'
@@ -390,6 +402,8 @@ async function deployWorker(args: DeployArgs) {
390402
worker_tag: workerTag,
391403
version_id: versionId,
392404
targets,
405+
wrangler_environment: args.env,
406+
worker_name_overridden: workerNameOverridden,
393407
});
394408

395409
metrics.sendMetricsEvent(

packages/wrangler/src/environment-variables/factory.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type VariableNames =
2323
| "WRANGLER_OUTPUT_FILE_DIRECTORY"
2424
| "WRANGLER_OUTPUT_FILE_PATH"
2525
| "WRANGLER_CI_MATCH_TAG"
26+
| "WRANGLER_CI_OVERRIDE_NAME"
2627
| "WRANGLER_BUILD_CONDITIONS"
2728
| "WRANGLER_BUILD_PLATFORM"
2829
| "WRANGLER_UNENV_RESOLVE_PATHS"

packages/wrangler/src/environment-variables/misc-variables.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ export const getCIMatchTag = getEnvironmentVariableFactory({
101101
variableName: "WRANGLER_CI_MATCH_TAG",
102102
});
103103

104+
/**
105+
* `WRANGLER_CI_OVERRIDE_TAG` specifies a worker tag
106+
*
107+
* If this is set, Wrangler will override the worker name with this tag
108+
*/
109+
export const getCIOverrideName = getEnvironmentVariableFactory({
110+
variableName: "WRANGLER_CI_OVERRIDE_NAME",
111+
});
112+
104113
/**
105114
* `WRANGLER_BUILD_CONDITIONS` specifies the "build conditions" to use when importing packages at build time.
106115
*

packages/wrangler/src/output.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ interface OutputEntryDeployment extends OutputEntryBase<"deploy"> {
9393
version_id: string | null;
9494
/** A list of URLs that represent the HTTP triggers associated with this deployment */
9595
targets: string[] | undefined;
96+
/** set if the worker's name was overridden */
97+
worker_name_overridden: boolean;
98+
/** wrangler environment used */
99+
wrangler_environment: string | undefined;
96100
}
97101

98102
interface OutputEntryPagesDeployment extends OutputEntryBase<"pages-deploy"> {
@@ -138,6 +142,10 @@ interface OutputEntryVersionUpload extends OutputEntryBase<"version-upload"> {
138142
version_id: string | null;
139143
/** The preview URL associated with this version upload */
140144
preview_url: string | undefined;
145+
/** set if the worker's name was overridden */
146+
worker_name_overridden: boolean;
147+
/** wrangler environment used */
148+
wrangler_environment: string | undefined;
141149
}
142150

143151
interface OutputEntryVersionDeployment

packages/wrangler/src/versions/upload.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { validateNodeCompatMode } from "../deployment-bundle/node-compat";
3232
import { loadSourceMaps } from "../deployment-bundle/source-maps";
3333
import { confirm } from "../dialogs";
3434
import { getMigrationsToUpload } from "../durable";
35+
import { getCIOverrideName } from "../environment-variables/misc-variables";
3536
import { UserError } from "../errors";
3637
import { getFlag } from "../experimental-flags";
3738
import { logger } from "../logger";
@@ -352,7 +353,17 @@ export const versionsUploadCommand = createCommand({
352353
const cliAlias = collectKeyValues(args.alias);
353354

354355
const accountId = args.dryRun ? undefined : await requireAuth(config);
355-
const name = getScriptName(args, config);
356+
let name = getScriptName(args, config);
357+
358+
const ciOverrideName = getCIOverrideName();
359+
let workerNameOverridden = false;
360+
if (ciOverrideName !== undefined && ciOverrideName !== name) {
361+
logger.warn(
362+
`Failed to match Worker name. Your config file is using the Worker name "${name}", but the CI system expected "${ciOverrideName}". Overriding using the CI provided Worker name. Workers Builds connected builds will attempt to open a pull request to resolve this config name mismatch.`
363+
);
364+
name = ciOverrideName;
365+
workerNameOverridden = true;
366+
}
356367

357368
assert(
358369
name,
@@ -407,6 +418,8 @@ export const versionsUploadCommand = createCommand({
407418
worker_tag: workerTag,
408419
version_id: versionId,
409420
preview_url: versionPreviewUrl,
421+
wrangler_environment: args.env,
422+
worker_name_overridden: workerNameOverridden,
410423
});
411424
},
412425
});

0 commit comments

Comments
 (0)