Skip to content

Commit e2397a6

Browse files
emily-shenhyperlint-ai[bot]
authored andcommitted
Update docs on Wrangler environments (#20873)
* update wrangler environments page * update environment variables page * update environments section in wrangler configuration page * fixup * Update src/content/docs/workers/configuration/environment-variables.mdx Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com> * Update src/content/docs/workers/wrangler/configuration.mdx * Update environment-variables.mdx * Update environment-variables.mdx * more fixups.. --------- Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
1 parent da19b72 commit e2397a6

File tree

4 files changed

+98
-74
lines changed

4 files changed

+98
-74
lines changed

src/content/docs/workers/configuration/environment-variables.mdx

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ pcx_content_type: configuration
33
title: Environment variables
44
head: []
55
description: Environment variables are a type of binding that allow you to attach text strings or JSON values to your Worker
6-
76
---
87

9-
import { Render, TabItem, Tabs, WranglerConfig } from "~/components"
8+
import { Render, TabItem, Tabs, WranglerConfig } from "~/components";
109

1110
## Background
1211

@@ -26,58 +25,56 @@ Refer to the following example on how to access the `API_HOST` environment varia
2625

2726
```js
2827
export default {
29-
async fetch(request, env, ctx) {
30-
return new Response(`API host: ${env.API_HOST}`);
31-
}
32-
}
28+
async fetch(request, env, ctx) {
29+
return new Response(`API host: ${env.API_HOST}`);
30+
},
31+
};
3332
```
3433

3534
</TabItem> <TabItem label="TypeScript" icon="seti:typescript">
3635

3736
```ts
3837
export interface Env {
39-
API_HOST: string;
38+
API_HOST: string;
4039
}
4140

4241
export default {
43-
async fetch(request, env, ctx): Promise<Response> {
44-
return new Response(`API host: ${env.API_HOST}`);
45-
}
42+
async fetch(request, env, ctx): Promise<Response> {
43+
return new Response(`API host: ${env.API_HOST}`);
44+
},
4645
} satisfies ExportedHandler<Env>;
4746
```
4847

4948
</TabItem> </Tabs>
5049

51-
`vars` is a non-inheritable key. [Non-inheritable keys](/workers/wrangler/configuration/#non-inheritable-keys) are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
50+
### Configuring different environments in Wrangler
51+
52+
[Environments in Wrangler](/workers/wrangler/environments) let you specify different configurations for the same Worker, including different values for `vars` in each environment.
53+
As `vars` is a [non-inheritable key](/workers/wrangler/configuration/#non-inheritable-keys), they are not inherited by environments and must be specified for each environment.
5254

53-
To define environment variables for different environments, refer to the example below:
55+
The example below sets up two environments, `staging` and `production`, with different values for `API_HOST`.
5456

5557
<WranglerConfig>
5658

5759
```toml
5860
name = "my-worker-dev"
5961

62+
# top level environment
63+
[vars]
64+
API_HOST = "api.example.com"
65+
6066
[env.staging.vars]
6167
API_HOST = "staging.example.com"
62-
API_ACCOUNT_ID = "staging_example_user"
63-
SERVICE_X_DATA = { URL = "service-x-api.dev.example", MY_ID = 123 }
6468

6569
[env.production.vars]
6670
API_HOST = "production.example.com"
67-
API_ACCOUNT_ID = "production_example_user"
68-
SERVICE_X_DATA = { URL = "service-x-api.prod.example", MY_ID = 456 }
6971
```
7072

7173
</WranglerConfig>
7274

73-
For local development with `wrangler dev`, variables in the [Wrangler configuration file](/workers/wrangler/configuration/) are automatically overridden by any values defined in a `.dev.vars` file located in the root directory of your worker. This is useful for providing values you do not want to check in to source control.
75+
To run Wrangler commands in specific environments, you can pass in the `--env` or `-e` flag. For example, you can develop the Worker in an environment called `staging` by running `npx wrangler dev --env staging`, and deploy it with `npx wrangler deploy --env staging`.
7476

75-
```shell
76-
API_HOST = "localhost:4000"
77-
API_ACCOUNT_ID = "local_example_user"
78-
```
79-
80-
Alternatively, you can specify per-environment values in the [Wrangler configuration file](/workers/wrangler/configuration/) and provide an `environment` value via the `env` flag when developing locally like so `wrangler dev --env=local`.
77+
Learn about [environments in Wrangler](/workers/wrangler/environments).
8178

8279
## Add environment variables via the dashboard
8380

@@ -94,14 +91,14 @@ To add environment variables via the dashboard:
9491

9592
:::caution[Plaintext strings and secrets]
9693

97-
9894
Select the **Secret** type if your environment variable is a [secret](/workers/configuration/secrets/).
9995

100-
10196
:::
10297

10398
<Render file="env_and_secrets" />
10499

100+
<Render file="secrets-in-dev" />
101+
105102
## Related resources
106103

107-
* Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.
104+
- Migrating environment variables from [Service Worker format to ES modules syntax](/workers/reference/migrate-to-module-workers/#environment-variables).

src/content/docs/workers/wrangler/configuration.mdx

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ kv_namespaces = [
5252

5353
## Environments
5454

55-
The configuration for a Worker can become complex when you define different [environments](/workers/wrangler/environments/), and each environment has its own configuration.
56-
There is a default (top-level) environment and named environments that provide environment-specific configuration.
55+
You can define different configurations for a Worker using Wrangler [environments](/workers/wrangler/environments/).
56+
There is a default (top-level) environment and you can create named environments that provide environment-specific configuration.
5757

58-
These are defined under `[env.name]` keys, such as `[env.staging]` which you can then preview or deploy with the `-e` / `--env` flag in the `wrangler` commands like `npx wrangler deploy --env staging`.
58+
These are defined under `[env.<name>]` keys, such as `[env.staging]` which you can then preview or deploy with the `-e` / `--env` flag in the `wrangler` commands like `npx wrangler deploy --env staging`.
5959

6060
The majority of keys are inheritable, meaning that top-level configuration can be used in environments. [Bindings](/workers/runtime-apis/bindings/), such as `vars` or `kv_namespaces`, are not inheritable and need to be defined explicitly.
6161

@@ -646,7 +646,7 @@ id = "<ID>"
646646

647647
</WranglerConfig>
648648

649-
### Images
649+
### Images
650650

651651
[Cloudflare Images](/images/transform-images/transform-via-workers/) lets you make transformation requests to optimize, resize, and manipulate images stored in remote sources.
652652

@@ -867,6 +867,7 @@ To bind other Workers to your Worker, assign an array of the below object to the
867867
- `service` <Type text="string" /> <MetaInfo text="required" />
868868

869869
- The name of the Worker.
870+
- To bind to a Worker in a specific [environment](/workers/wrangler/environments), you need to append the environment name to the Worker name. This should be in the format `<worker-name>-<environment-name>`. For example, to bind to a Worker called `worker-name` in its `staging` environment, `service` should be set to `worker-name-staging`.
870871

871872
- `entrypoint` <Type text="string" /> <MetaInfo text="optional" />
872873

@@ -1304,17 +1305,17 @@ A common example of using a redirected configuration is where a custom build too
13041305

13051306
<WranglerConfig>
13061307

1307-
```toml title="wrangler.toml"
1308-
name = "my-worker"
1309-
main = "src/index.ts"
1310-
[[kv_namespaces]]
1311-
binding = "<BINDING_NAME1>"
1312-
id = "<NAMESPACE_ID1>"
1313-
```
1308+
```toml title="wrangler.toml"
1309+
name = "my-worker"
1310+
main = "src/index.ts"
1311+
[[kv_namespaces]]
1312+
binding = "<BINDING_NAME1>"
1313+
id = "<NAMESPACE_ID1>"
1314+
```
13141315

13151316
</WranglerConfig>
13161317

1317-
Note that this configuration points `main` at the user's code entry-point.
1318+
Note that this configuration points `main` at the user's code entry-point.
13181319

13191320
- Then, the user runs a custom build, which might read the user's Wrangler configuration file to find the source code entry-point:
13201321

src/content/docs/workers/wrangler/environments.mdx

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,37 @@
22
pcx_content_type: concept
33
title: Environments
44
head: []
5-
description: Deploy the same Worker application with different configuration for
6-
each environment.
5+
description: Use environments to create different configurations for the same Worker application.
76
---
87

98
import { WranglerConfig } from "~/components";
109

11-
## Background
12-
13-
Wrangler allows you to deploy the same Worker application with different configuration for each environment. You must configure environments in your Worker application's Wrangler file.
10+
Wrangler allows you to use environments to create different configurations for the same Worker application. Environments are configured in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/).
11+
There is a default (top-level) environment and you can create named environments that provide environment-specific configuration.
1412

1513
Review the following environments flow:
1614

17-
1. You have created a Worker application named `my-worker`.
18-
2. You create an environment, for example, `dev`, in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/).
19-
3. In the Wrangler configuration file, you configure the `dev` environment by [adding bindings](/workers/runtime-apis/bindings/) and/or [routes](/workers/configuration/routing/routes/).
20-
4. You deploy the Worker using `npx wrangler deploy -e dev`.
21-
5. In the background, Wrangler creates a new Worker named `my-worker-dev`.
22-
6. You can now change your `my-worker` Worker code and configuration, and choose which environment to deploy your changes to.
23-
24-
Environments are used with the `--env` or `-e` flag on `wrangler dev`, `npx wrangler deploy`, and `wrangler secret`.
15+
1. Create a Worker, named `my-worker` for example.
16+
2. Create an environment, for example `dev`, in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/), by adding a `[env.<ENV_NAME>]` section.
2517

26-
## Configuration
18+
<WranglerConfig>
2719

28-
To create an environment:
20+
```json
21+
{
22+
"name": "my-worker",
23+
"env": {
24+
"<ENV_NAME>": {
25+
// environment-specific configuration goes here
26+
}
27+
}
28+
}
29+
```
2930

30-
1. Open your Worker's Wrangler file.
31-
2. Add `[env.<NAME>]` and change `<NAME>` to the desired name of your environment.
32-
3. Repeat step 2 to create multiple environments.
31+
</WranglerConfig>
3332

34-
Be careful when naming your environments that they do not contain sensitive information, such as, `migrating-service-from-company1-to-company2` or `company1-acquisition-load-test`.
33+
3. You can configure the `dev` environment with different values to the top-level environment. Refer [here](/workers/wrangler/configuration/#environments) for how different options are inherited - or not inherited - between environments.
3534

36-
Review the layout of an example `[env.dev]` environment that sets up a custom `dev.example.com` route:
35+
For example, to set a different route for a Worker in the `dev` environment:
3736

3837
<WranglerConfig>
3938

@@ -46,23 +45,21 @@ route = "dev.example.com"
4645
```
4746

4847
</WranglerConfig>
48+
4. Environments are used with the `--env` or `-e` flag on Wrangler commands. For example, you can develop the Worker in the `dev` environment by running `npx wrangler dev -e=dev`, and deploy it with `npx wrangler deploy -e=dev`.
4949

50-
You cannot specify multiple environments with the same name.
51-
52-
Wrangler appends the environment name to the top-level name to deploy a Worker. For example, a Worker project named `my-worker` with an environment `[env.dev]` would deploy a Worker named `my-worker-dev`.
50+
:::note
5351

54-
After you have configured your environment, run `npx wrangler deploy` in your Worker project directory for the changes to take effect.
52+
Cloudflare effectively creates a new Worker for you when you create a Worker with an environment. Wrangler appends the environment name to the top-level name. For example, a Worker project named `my-worker` with an environment `dev` would deploy a Worker named `my-worker-dev`. You must use this name when referencing a Worker in a particular environment, for example in a [service binding](/workers/wrangler/configuration/#service-bindings).
53+
:::
5554

5655
## Non-inheritable keys and environments
5756

5857
[Non-inheritable keys](/workers/wrangler/configuration/#non-inheritable-keys) are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
5958

60-
[Bindings](/workers/runtime-apis/bindings/) and [environment variables](/workers/configuration/environment-variables/) must be specified per each [environment](/workers/wrangler/environments/) in your [Wrangler configuration file](/workers/wrangler/configuration/).
59+
For example, [bindings](/workers/runtime-apis/bindings/) and [environment variables](/workers/configuration/environment-variables/) are non-inheritable, and must be specified per [environment](/workers/wrangler/environments/) in your [Wrangler configuration file](/workers/wrangler/configuration/).
6160

6261
Review the following example Wrangler file:
6362

64-
65-
6663
<WranglerConfig>
6764

6865
```toml
@@ -85,7 +82,41 @@ kv_namespaces = [
8582

8683
</WranglerConfig>
8784

88-
You may assign environment-specific [secrets](/workers/configuration/secrets/) by running the command [`wrangler secret put <KEY> -env`](/workers/wrangler/commands/#put).
85+
### Service bindings
86+
87+
To use a [service binding](/workers/wrangler/configuration/#service-bindings) that targets a Worker in a specific environment, you need to append the environment name to the target Worker name in the `service` field. This should be in the format `<worker-name>-<environment-name>`.
88+
In the example below, we have two Workers, both with a `staging` environment. `worker-b` has a service binding to `worker-a`. Note how the `service` field in the `staging` environment points to `worker-a-staging`, whereas the top-level service binding points to `worker-a`.
89+
90+
<WranglerConfig>
91+
92+
```toml
93+
name = "worker-a"
94+
95+
vars = { FOO = "<top-level-var>" }
96+
[env.staging.vars]
97+
FOO = "<staging-var>"
98+
```
99+
100+
</WranglerConfig>
101+
102+
<WranglerConfig>
103+
104+
```toml
105+
name = "worker-b"
106+
107+
services = { binding = "<BINDING_NAME>", service = "worker-a" }
108+
109+
# Note how `service = "worker-a-staging"`
110+
env.staging.service ={ binding = "<BINDING_NAME>", service = "worker-a-staging" }
111+
```
112+
113+
</WranglerConfig>
114+
115+
### Secrets
116+
117+
You may assign environment-specific [secrets](/workers/configuration/secrets/) by running the command [`wrangler secret put <KEY> -env`](/workers/wrangler/commands/#put). You can also create `dotenv` type files named `.dev.vars.<environment-name>`.
118+
119+
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
89120

90121
---
91122

@@ -95,8 +126,6 @@ You may assign environment-specific [secrets](/workers/configuration/secrets/) b
95126

96127
The following Wrangler file adds two environments, `[env.staging]` and `[env.production]`, to the Wrangler file. If you are deploying to a [Custom Domain](/workers/configuration/routing/custom-domains/) or [route](/workers/configuration/routing/routes/), you must provide a [`route` or `routes` key](/workers/wrangler/configuration/) for each environment.
97128

98-
99-
100129
<WranglerConfig>
101130

102131
```toml
@@ -118,7 +147,7 @@ routes = [
118147

119148
</WranglerConfig>
120149

121-
In order to use environments with this configuration, you can pass the name of the environment via the `--env` flag.
150+
You can pass the name of the environment via the `--env` flag to run commands in a specific environment.
122151

123152
With this configuration, Wrangler will behave in the following manner:
124153

@@ -168,8 +197,6 @@ if (ENVIRONMENT === "staging") {
168197

169198
To deploy your code to your `*.workers.dev` subdomain, include `workers_dev = true` in the desired environment. Your Wrangler file may look like this:
170199

171-
172-
173200
<WranglerConfig>
174201

175202
```toml
@@ -206,6 +233,6 @@ Published my-worker
206233

207234
:::caution
208235

209-
When you create a Worker via an environment, Cloudflare automatically creates an SSL certification for it. SSL certifications are discoverable and a matter of public record.
236+
When you create a Worker via an environment, Cloudflare automatically creates an SSL certification for it. SSL certifications are discoverable and a matter of public record. Be careful when naming your environments that they do not contain sensitive information, such as, `migrating-service-from-company1-to-company2` or `company1-acquisition-load-test`.
210237

211238
:::

src/content/partials/workers/secrets-in-dev.mdx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
{}
3-
43
---
54

65
When developing your Worker or Pages Function, create a `.dev.vars` file in the root of your project to define secrets that will be used when running `wrangler dev` or `wrangler pages dev`, as opposed to using environment variables in the [Wrangler configuration file](/workers/configuration/environment-variables/#compare-secrets-and-environment-variables). This works both in local and remote development modes.
@@ -12,6 +11,6 @@ SECRET_KEY="value"
1211
API_TOKEN="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9"
1312
```
1413

15-
To set different secrets for each environment, create files named `.dev.vars.<environment-name>`. When you use `wrangler <command> --env <environment-name>`, the corresponding environment-specific file will be loaded instead of the `.dev.vars` file, so the two files are not merged.
14+
To set different secrets for each environment, create files named `.dev.vars.<environment-name>`. When you use `wrangler <command> --env <environment-name>`, the corresponding environment-specific file will be loaded instead of the `.dev.vars` file.
1615

1716
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.

0 commit comments

Comments
 (0)