Skip to content

Commit

Permalink
add dotenv support to wrangler secret bulk (#7674)
Browse files Browse the repository at this point in the history
* add dotenv support to wrangler secret bulk

* remove x-versions arg

* remove old --x-versions flag

* PR feedback

---------

Co-authored-by: Thomas Ankcorn <tankcorn@cloudflare.com>
  • Loading branch information
Ankcorn and Thomas Ankcorn authored Jan 9, 2025
1 parent 7c8ae1c commit 45d1d1e
Show file tree
Hide file tree
Showing 10 changed files with 507 additions and 197 deletions.
15 changes: 15 additions & 0 deletions .changeset/silver-plums-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"wrangler": minor
---

Add support for env files to wrangler secret bulk i.e. `.dev.vars`

Run `wrangler secret bulk .dev.vars` to add the env file

```env
//.dev.vars
KEY=VALUE
KEY_2=VALUE
```

This will upload the secrets KEY and KEY_2 to your worker
185 changes: 185 additions & 0 deletions fixtures/import-npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/wrangler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
"command-exists": "^1.2.9",
"concurrently": "^8.2.2",
"devtools-protocol": "^0.0.1182435",
"dotenv": "^16.0.0",
"dotenv": "^16.3.1",
"execa": "^6.1.0",
"express": "^4.18.1",
"find-up": "^6.3.0",
Expand Down
33 changes: 31 additions & 2 deletions packages/wrangler/src/__tests__/pages/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ describe("wrangler pages secret", () => {
await expect(
runWrangler(`pages secret bulk --project some-project-name`)
).rejects.toMatchInlineSnapshot(
`[Error: 🚨 Please provide a JSON file or valid JSON pipe]`
`[Error: 🚨 No content found in file or piped input.]`
);
});

Expand Down Expand Up @@ -566,7 +566,6 @@ describe("wrangler pages secret", () => {
text: "secret_text",
},
]);

await runWrangler(
"pages secret bulk ./secret.json --project some-project-name"
);
Expand All @@ -579,6 +578,36 @@ describe("wrangler pages secret", () => {
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should create secret bulk w/ env file", async () => {
writeFileSync(
".env",
`SECRET_1=secret-1\nSECRET_2=secret-2\nSECRET_3=secret-3`
);

mockProjectRequests([
{
name: "SECRET_1",
text: "secret-1",
},
{
name: "SECRET_2",
text: "secret-2",
},
{
name: "SECRET_3",
text: "secret-3",
},
]);
await runWrangler("pages secret bulk .env --project some-project-name");

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Pages project \\"some-project-name\\" (production)
Finished processing secrets JSON file:
✨ 3 secrets successfully uploaded"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should create secret bulk: preview", async () => {
writeFileSync(
"secret.json",
Expand Down
66 changes: 44 additions & 22 deletions packages/wrangler/src/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,10 @@ describe("wrangler secret", () => {
`"🌀 Creating the secrets for the Worker \\"script-name\\" "`
);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Please provide a JSON file or valid JSON pipe[0m
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 No content found in file, or piped input.[0m
"
`);
"
`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

Expand All @@ -782,13 +782,13 @@ describe("wrangler secret", () => {

await runWrangler(`secret bulk --name script-name`);
expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret1
✨ Successfully created secret for key: password
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret1
✨ Successfully created secret for key: password
Finished processing secrets JSON file:
✨ 2 secrets successfully uploaded"
`);
Finished processing secrets file:
✨ 2 secrets successfully uploaded"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});
Expand All @@ -807,12 +807,34 @@ describe("wrangler secret", () => {
await runWrangler("secret bulk ./secret.json --name script-name");

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret-name-1
✨ Successfully created secret for key: secret-name-2
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret-name-1
✨ Successfully created secret for key: secret-name-2
Finished processing secrets JSON file:
✨ 2 secrets successfully uploaded"
Finished processing secrets file:
✨ 2 secrets successfully uploaded"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
});

it("should create secrets from a env file", async () => {
writeFileSync(
".env",
`SECRET_NAME_1=secret_text\nSECRET_NAME_2=secret_text`
);

mockBulkRequest();

await runWrangler("secret bulk ./.env --name script-name");

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: SECRET_NAME_1
✨ Successfully created secret for key: SECRET_NAME_2
Finished processing secrets file:
✨ 2 secrets successfully uploaded"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand Down Expand Up @@ -989,13 +1011,13 @@ describe("wrangler secret", () => {
await runWrangler("secret bulk ./secret.json --name script-name");

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret-name-2
✨ Successfully created secret for key: secret-name-3
✨ Successfully created secret for key: secret-name-4
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret-name-2
✨ Successfully created secret for key: secret-name-3
✨ Successfully created secret for key: secret-name-4
Finished processing secrets JSON file:
✨ 3 secrets successfully uploaded"
Finished processing secrets file:
✨ 3 secrets successfully uploaded"
`);
expect(std.err).toMatchInlineSnapshot(`""`);
expect(std.warn).toMatchInlineSnapshot(`""`);
Expand Down Expand Up @@ -1050,7 +1072,7 @@ describe("wrangler secret", () => {
✨ Successfully created secret for key: secret-name-1
✨ Successfully created secret for key: secret-name-2
Finished processing secrets JSON file:
Finished processing secrets file:
✨ 2 secrets successfully uploaded"
`);
});
Expand All @@ -1065,7 +1087,7 @@ describe("wrangler secret", () => {
"wrangler secret:bulk [json]
POSITIONALS
json The JSON file of key-value pairs to upload, in form {\\"key\\": value, ...} [string]
json The file of key-value pairs to upload, as JSON in form {\\"key\\": value, ...} or .dev.vars file in the form KEY=VALUE [string]
GLOBAL FLAGS
-c, --config Path to Wrangler configuration file [string]
Expand Down
Loading

0 comments on commit 45d1d1e

Please sign in to comment.