Skip to content

Commit 9b48f5e

Browse files
committed
update wrangler environments page
1 parent 1424de6 commit 9b48f5e

File tree

1 file changed

+59
-33
lines changed

1 file changed

+59
-33
lines changed

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

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,36 @@
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/).
1411

1512
Review the following environments flow:
1613

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`.
14+
1. You have created a Worker, named `my-worker` for example.
15+
2. Create an environment, for example `dev`, in the Worker's [Wrangler configuration file](/workers/wrangler/configuration/), by adding a `[env.<ENV_NAME>]` section.
2516

26-
## Configuration
17+
<WranglerConfig>
2718

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

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.
30+
</WranglerConfig>
3331

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`.
32+
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.
3533

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

3836
<WranglerConfig>
3937

@@ -46,12 +44,12 @@ route = "dev.example.com"
4644
```
4745

4846
</WranglerConfig>
47+
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`.
4948

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`.
49+
:::note
5350

54-
After you have configured your environment, run `npx wrangler deploy` in your Worker project directory for the changes to take effect.
51+
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).
52+
:::
5553

5654
## Non-inheritable keys and environments
5755

@@ -61,8 +59,6 @@ After you have configured your environment, run `npx wrangler deploy` in your Wo
6159

6260
Review the following example Wrangler file:
6361

64-
65-
6662
<WranglerConfig>
6763

6864
```toml
@@ -85,7 +81,41 @@ kv_namespaces = [
8581

8682
</WranglerConfig>
8783

88-
You may assign environment-specific [secrets](/workers/configuration/secrets/) by running the command [`wrangler secret put <KEY> -env`](/workers/wrangler/commands/#put).
84+
### Service bindings
85+
86+
To use a [service binding](/workers/wrangler/configuration/#service-bindings) that targets a Worker in a specific environment, you need to include the environment in `service` field. This should be in the format `<worker-name>-<environment-name>`.
87+
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 binding in the `staging` environment points to `worker-a-staging`, whereas the top-level service binding points to `worker-a`.
88+
89+
<WranglerConfig>
90+
91+
```toml
92+
name = "worker-a"
93+
94+
vars = { FOO = "<top-level-var>" }
95+
[env.staging.vars]
96+
FOO = "<staging-var>"
97+
```
98+
99+
</WranglerConfig>
100+
101+
<WranglerConfig>
102+
103+
```toml
104+
name = "worker-b"
105+
106+
services = { binding = "<BINDING_NAME>", service = "worker-a" }
107+
108+
# Note how `service = "worker-a-staging"`
109+
env.staging.service ={ binding = "<BINDING_NAME>", service = "worker-a-staging" }
110+
```
111+
112+
</WranglerConfig>
113+
114+
### Secrets
115+
116+
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>`.
117+
118+
Like other environment variables, secrets are [non-inheritable](/workers/wrangler/configuration/#non-inheritable-keys) and must be defined per environment.
89119

90120
---
91121

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

96126
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.
97127

98-
99-
100128
<WranglerConfig>
101129

102130
```toml
@@ -118,7 +146,7 @@ routes = [
118146

119147
</WranglerConfig>
120148

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

123151
With this configuration, Wrangler will behave in the following manner:
124152

@@ -168,8 +196,6 @@ if (ENVIRONMENT === "staging") {
168196

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

171-
172-
173199
<WranglerConfig>
174200

175201
```toml
@@ -206,6 +232,6 @@ Published my-worker
206232

207233
:::caution
208234

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.
235+
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`.
210236

211237
:::

0 commit comments

Comments
 (0)