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

Add WRANGLER_CI_OVERRIDE_NAME for Workers CI #7990

Merged
merged 1 commit into from
Feb 5, 2025
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
5 changes: 5 additions & 0 deletions .changeset/cool-monkeys-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Add WRANGLER_CI_OVERRIDE_NAME for Workers CI
81 changes: 81 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
printBundleSize,
printOffendingDependencies,
} from "../deployment-bundle/bundle-reporter";
import { clearOutputFilePath } from "../output";
import { writeAuthConfigFile } from "../user";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockAuthDomain } from "./helpers/mock-auth-domain";
Expand Down Expand Up @@ -90,6 +91,7 @@ describe("deploy", () => {
afterEach(() => {
vi.unstubAllGlobals();
clearDialogs();
clearOutputFilePath();
});

it("should output log file with deployment details", async () => {
Expand Down Expand Up @@ -175,6 +177,85 @@ describe("deploy", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should successfully override name with WRANGLER_CI_OVERRIDE_NAME", async () => {
vi.stubEnv("WRANGLER_CI_OVERRIDE_NAME", "test-name");
writeWorkerSource();
writeWranglerConfig({
name: "name-to-override",
workers_dev: true,
});
mockServiceScriptData({
scriptName: "test-name",
script: { id: "test-name", tag: "abc123" },
});
mockUploadWorkerRequest();
mockSubDomainRequest();
mockGetWorkerSubdomain({ enabled: false });
mockUpdateWorkerSubdomain({ enabled: true });

await runWrangler("deploy ./index.js");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should successfully override name with WRANGLER_CI_OVERRIDE_NAME and outputs the correct output file", async () => {
vi.stubEnv("WRANGLER_OUTPUT_FILE_DIRECTORY", "override-output");
vi.stubEnv("WRANGLER_OUTPUT_FILE_PATH", "");
vi.stubEnv("WRANGLER_CI_OVERRIDE_NAME", "test-name");
writeWorkerSource();
writeWranglerConfig({
name: "override-name",
workers_dev: true,
});
mockUploadWorkerRequest();
mockSubDomainRequest();
mockGetWorkerSubdomain({ enabled: true });

await runWrangler("deploy ./index.js --env staging");
expect(std.out).toMatchInlineSnapshot(`
"Total Upload: xx KiB / gzip: xx KiB
Worker Startup Time: 100 ms
Uploaded test-name (TIMINGS)
Deployed test-name triggers (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Version ID: Galaxy-Class"
`);
expect(std.err).toMatchInlineSnapshot(`""`);

const outputFilePaths = fs.readdirSync("override-output");

expect(outputFilePaths.length).toEqual(1);
expect(outputFilePaths[0]).toMatch(/wrangler-output-.+\.json/);
const outputFile = fs.readFileSync(
path.join("override-output", outputFilePaths[0]),
"utf8"
);
const entries = outputFile
.split("\n")
.filter(Boolean)
.map((e) => JSON.parse(e));

expect(entries.find((e) => e.type === "deploy")).toMatchObject({
targets: ["https://test-name.test-sub-domain.workers.dev"],
// Omitting timestamp for matching
// timestamp: ...
type: "deploy",
version: 1,
version_id: "Galaxy-Class",
worker_name: "test-name",
worker_tag: "tag:test-name",
worker_name_overridden: true,
wrangler_environment: "staging",
});
});

it("should resolve wrangler.toml relative to the entrypoint", async () => {
fs.mkdirSync("./some-path/worker", { recursive: true });
fs.writeFileSync(
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/__tests__/output.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ describe("writeOutput()", () => {
worker_tag: "ABCDE12345",
version_id: "1234",
targets: undefined,
worker_name_overridden: false,
wrangler_environment: undefined,
});

const outputFile = readFileSync(WRANGLER_OUTPUT_FILE_PATH, "utf8");
Expand All @@ -104,6 +106,8 @@ describe("writeOutput()", () => {
worker_tag: "ABCDE12345",
version_id: "1234",
targets: undefined,
worker_name_overridden: false,
wrangler_environment: undefined,
},
]);
});
Expand Down Expand Up @@ -151,6 +155,8 @@ describe("writeOutput()", () => {
worker_tag: "ABCDE12345",
version_id: "1234",
targets: undefined,
worker_name_overridden: false,
wrangler_environment: undefined,
});

const outputFilePaths = readdirSync("output");
Expand All @@ -172,6 +178,8 @@ describe("writeOutput()", () => {
worker_tag: "ABCDE12345",
version_id: "1234",
targets: undefined,
worker_name_overridden: false,
wrangler_environment: undefined,
},
]);
});
Expand Down
16 changes: 15 additions & 1 deletion packages/wrangler/src/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import path from "node:path";
import { getAssetsOptions, validateAssetsArgsAndConfig } from "../assets";
import { configFileName, readConfig } from "../config";
import { getEntry } from "../deployment-bundle/entry";
import { getCIOverrideName } from "../environment-variables/misc-variables";
import { UserError } from "../errors";
import { run } from "../experimental-flags";
import { logger } from "../logger";
Expand Down Expand Up @@ -336,7 +337,18 @@ async function deployWorker(args: DeployArgs) {
}

const beforeUpload = Date.now();
const name = getScriptName(args, config);
let name = getScriptName(args, config);

const ciOverrideName = getCIOverrideName();
let workerNameOverridden = false;
if (ciOverrideName !== undefined && ciOverrideName !== name) {
logger.warn(
`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.`
);
name = ciOverrideName;
workerNameOverridden = true;
}

assert(
name,
'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>"`'
Expand Down Expand Up @@ -390,6 +402,8 @@ async function deployWorker(args: DeployArgs) {
worker_tag: workerTag,
version_id: versionId,
targets,
wrangler_environment: args.env,
worker_name_overridden: workerNameOverridden,
});

metrics.sendMetricsEvent(
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/environment-variables/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type VariableNames =
| "WRANGLER_OUTPUT_FILE_DIRECTORY"
| "WRANGLER_OUTPUT_FILE_PATH"
| "WRANGLER_CI_MATCH_TAG"
| "WRANGLER_CI_OVERRIDE_NAME"
| "WRANGLER_BUILD_CONDITIONS"
| "WRANGLER_BUILD_PLATFORM"
| "WRANGLER_UNENV_RESOLVE_PATHS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ export const getCIMatchTag = getEnvironmentVariableFactory({
variableName: "WRANGLER_CI_MATCH_TAG",
});

/**
* `WRANGLER_CI_OVERRIDE_TAG` specifies a worker tag
*
* If this is set, Wrangler will override the worker name with this tag
*/
export const getCIOverrideName = getEnvironmentVariableFactory({
variableName: "WRANGLER_CI_OVERRIDE_NAME",
});

/**
* `WRANGLER_BUILD_CONDITIONS` specifies the "build conditions" to use when importing packages at build time.
*
Expand Down
8 changes: 8 additions & 0 deletions packages/wrangler/src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ interface OutputEntryDeployment extends OutputEntryBase<"deploy"> {
version_id: string | null;
/** A list of URLs that represent the HTTP triggers associated with this deployment */
targets: string[] | undefined;
/** set if the worker's name was overridden */
worker_name_overridden: boolean;
/** wrangler environment used */
wrangler_environment: string | undefined;
}

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

interface OutputEntryVersionDeployment
Expand Down
15 changes: 14 additions & 1 deletion packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { validateNodeCompatMode } from "../deployment-bundle/node-compat";
import { loadSourceMaps } from "../deployment-bundle/source-maps";
import { confirm } from "../dialogs";
import { getMigrationsToUpload } from "../durable";
import { getCIOverrideName } from "../environment-variables/misc-variables";
import { UserError } from "../errors";
import { logger } from "../logger";
import { verifyWorkerMatchesCITag } from "../match-tag";
Expand Down Expand Up @@ -339,7 +340,17 @@ export const versionsUploadCommand = createCommand({
const cliAlias = collectKeyValues(args.alias);

const accountId = args.dryRun ? undefined : await requireAuth(config);
const name = getScriptName(args, config);
let name = getScriptName(args, config);

const ciOverrideName = getCIOverrideName();
let workerNameOverridden = false;
if (ciOverrideName !== undefined && ciOverrideName !== name) {
logger.warn(
`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.`
);
name = ciOverrideName;
workerNameOverridden = true;
}

assert(
name,
Expand Down Expand Up @@ -393,6 +404,8 @@ export const versionsUploadCommand = createCommand({
worker_tag: workerTag,
version_id: versionId,
preview_url: versionPreviewUrl,
wrangler_environment: args.env,
worker_name_overridden: workerNameOverridden,
});
},
});
Expand Down
Loading