Skip to content

Commit 7e80340

Browse files
Fix wrangler deploy failing for new workers containing environment variables or bindings (#11504)
Co-authored-by: James Opstad <[email protected]>
1 parent 7b24eb0 commit 7e80340

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

‎.changeset/social-baboons-show.md‎

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+
Fix `wrangler deploy` failing for new workers containing environment variables or bindings

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { spawnSync } from "node:child_process";
44
import { randomFillSync } from "node:crypto";
55
import * as fs from "node:fs";
66
import * as path from "node:path";
7-
import { findWranglerConfig, ParseError } from "@cloudflare/workers-utils";
7+
import {
8+
APIError,
9+
findWranglerConfig,
10+
ParseError,
11+
} from "@cloudflare/workers-utils";
812
import { normalizeString } from "@cloudflare/workers-utils/test-helpers";
913
import { sync } from "command-exists";
1014
import * as esbuild from "esbuild";
@@ -15633,6 +15637,61 @@ export default{
1563315637
`);
1563415638
});
1563515639

15640+
it("should handle the remote secrets fetching check for new workers", async () => {
15641+
writeWorkerSource();
15642+
writeWranglerConfig(
15643+
{
15644+
compatibility_date: "2024-04-24",
15645+
main: "./index.js",
15646+
vars: {
15647+
MY_SECRET: 123,
15648+
},
15649+
observability: {
15650+
enabled: true,
15651+
},
15652+
},
15653+
"./wrangler.json"
15654+
);
15655+
mockSubDomainRequest();
15656+
mockUploadWorkerRequest({ wranglerConfigPath: "./wrangler.json" });
15657+
15658+
msw.use(
15659+
http.get(
15660+
`*/accounts/:accountId/workers/scripts/:scriptName/secrets`,
15661+
() => {
15662+
const workerNotFoundAPIError = new APIError({
15663+
status: 404,
15664+
text: "A request to the Cloudflare API (/accounts/xxx/workers/scripts/yyy/secrets) failed.",
15665+
});
15666+
15667+
workerNotFoundAPIError.code = 10007;
15668+
throw workerNotFoundAPIError;
15669+
},
15670+
{ once: true }
15671+
)
15672+
);
15673+
15674+
await runWrangler("deploy");
15675+
15676+
expect(fetchSecrets).toHaveBeenCalled();
15677+
expect(std.warn).toMatchInlineSnapshot(`""`);
15678+
expect(std.out).toMatchInlineSnapshot(`
15679+
"
15680+
⛅️ wrangler x.x.x
15681+
──────────────────
15682+
Total Upload: xx KiB / gzip: xx KiB
15683+
Worker Startup Time: 100 ms
15684+
Your Worker has access to the following bindings:
15685+
Binding Resource
15686+
env.MY_SECRET (123) Environment Variable
15687+
15688+
Uploaded test-name (TIMINGS)
15689+
Deployed test-name triggers (TIMINGS)
15690+
https://test-name.test-sub-domain.workers.dev
15691+
Current Version ID: Galaxy-Class"
15692+
`);
15693+
});
15694+
1563615695
it("should not fetch remote secrets in dry-run mode", async () => {
1563715696
writeWorkerSource();
1563815697
writeWranglerConfig(

‎packages/wrangler/src/utils/fetch-secrets.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { APIError } from "@cloudflare/workers-utils";
12
import { fetchResult } from "../cfetch";
23
import { requireAuth } from "../user";
34
import { useServiceEnvironments } from "./useServiceEnvironments";
@@ -20,7 +21,15 @@ export async function fetchSecrets(
2021
const secrets = await fetchResult<{ name: string; type: string }[]>(
2122
config,
2223
url
23-
);
24+
).catch((e) => {
25+
if (e instanceof APIError && e.code === 10007) {
26+
// The worker was not found this means that this is the workers' first deployment
27+
// so there are obviously no secrets
28+
return [];
29+
}
30+
31+
throw e;
32+
});
2433

2534
return secrets;
2635
}

0 commit comments

Comments
 (0)