You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/workers/wrangler/environments.mdx
+59-33Lines changed: 59 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,36 @@
2
2
pcx_content_type: concept
3
3
title: Environments
4
4
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.
7
6
---
8
7
9
8
import { WranglerConfig } from"~/components";
10
9
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/).
14
11
15
12
Review the following environments flow:
16
13
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.
25
16
26
-
## Configuration
17
+
<WranglerConfig>
27
18
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
+
```
29
29
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>
33
31
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.
35
33
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:
37
35
38
36
<WranglerConfig>
39
37
@@ -46,12 +44,12 @@ route = "dev.example.com"
46
44
```
47
45
48
46
</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`.
49
48
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
53
50
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
+
:::
55
53
56
54
## Non-inheritable keys and environments
57
55
@@ -61,8 +59,6 @@ After you have configured your environment, run `npx wrangler deploy` in your Wo
61
59
62
60
Review the following example Wrangler file:
63
61
64
-
65
-
66
62
<WranglerConfig>
67
63
68
64
```toml
@@ -85,7 +81,41 @@ kv_namespaces = [
85
81
86
82
</WranglerConfig>
87
83
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.
89
119
90
120
---
91
121
@@ -95,8 +125,6 @@ You may assign environment-specific [secrets](/workers/configuration/secrets/) b
95
125
96
126
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.
97
127
98
-
99
-
100
128
<WranglerConfig>
101
129
102
130
```toml
@@ -118,7 +146,7 @@ routes = [
118
146
119
147
</WranglerConfig>
120
148
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.
122
150
123
151
With this configuration, Wrangler will behave in the following manner:
124
152
@@ -168,8 +196,6 @@ if (ENVIRONMENT === "staging") {
168
196
169
197
To deploy your code to your `*.workers.dev` subdomain, include `workers_dev = true` in the desired environment. Your Wrangler file may look like this:
170
198
171
-
172
-
173
199
<WranglerConfig>
174
200
175
201
```toml
@@ -206,6 +232,6 @@ Published my-worker
206
232
207
233
:::caution
208
234
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`.
0 commit comments