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
An **environment** is a separate, isolated instance of your Worker application. For example, you can have a `staging` and a `production` environment running simultaneously.
11
18
12
-
Each environment deploys to its own unique URL and can have different configuration — such as different resources that your bindings connect to, different environment variables, and different secrets. This isolation lets you test changes in one environment without affecting users in another.
19
+
Each environment deploys to its own unique URL and can have different configuration — such as different resources that your [bindings](/workers/runtime-apis/bindings/) connect to, different [environment variables](/workers/configuration/environment-variables/), and different [secrets](/workers/configuration/secrets/). This isolation lets you test changes in one environment without affecting users in another.
13
20
14
-
You first define environments in Wrangler configuration, and then deploy them either manually or automatically through our Git integration.
21
+
You first define environments in [Wrangler configuration](/workers/wrangler/configuration/), and then deploy them either manually or automatically through our [Git integration](/workers/ci-cd/builds/).
15
22
16
23
## Configuration
17
24
18
-
You define environments in your Wrangler configuration file under the `env` field:
25
+
You define environments in your [Wrangler configuration file](/workers/wrangler/configuration/) under the `env` field:
19
26
20
27
<WranglerConfig>
21
28
@@ -427,51 +434,83 @@ Because environments run as isolated copies of your application, all of your bin
427
434
Each environment configuration must explicitly define:
-[Secrets](/workers/configuration/secrets/) (managed separately via Wrangler or the dashboard)
432
439
- Any other configuration specific to that environment
433
440
434
441
## Deployment workflows
435
442
436
443
All environments must be defined in your Wrangler configuration file. Once configured, you can deploy environments through two main workflows:
437
444
438
-
### Manual deployment with Wrangler
439
-
440
-
Deploy to specific environments using Wrangler, the Cloudflare API, or Infrastructure as Code tools like Terraform:
441
-
442
-
```bash
443
-
# Deploy to the default environment
444
-
npx wrangler deploy
445
-
446
-
# Deploy to a specific environment
447
-
npx wrangler deploy --env staging
448
-
npx wrangler deploy --env production
449
-
```
450
-
451
445
### Automated deployment with Git integration
452
446
453
-
When you connect your repository to Cloudflare, Workers Builds automatically creates isolated preview environments for every pull request and can deploy specific branches to designated environments.
447
+
When you connect your repository to Cloudflare, [Workers Builds](/workers/ci-cd/builds/) automatically creates isolated preview environments for every pull request and can deploy specific branches to designated environments.
454
448
455
449
#### Pull request previews
456
450
457
451
Every pull request automatically gets its own isolated environment. When you open a PR:
458
452
459
453
- Cloudflare automatically builds and deploys your code
When using the API, you don't create "environments" as a separate entity. Instead, each environment is deployed as its own Worker script. Cloudflare uses a naming convention where each environment becomes a Worker named `<worker-name>-<environment-name>`. For example, if your base Worker is named `my-api`:
476
+
477
+
- The production environment deploys as a Worker named `my-api`
478
+
- The staging environment deploys as a separate Worker named `my-api-staging`
479
+
480
+
To deploy environments programmatically using the [Cloudflare API](/api/resources/workers/subresources/scripts/methods/update/), you upload each environment as a separate Worker script and specify tags to identify the relationship:
481
+
482
+
**Deploy to production:**
483
+
484
+
```bash
485
+
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/my-api" \
-`service`: The base Worker name (same across all environments)
505
+
-`environment`: The specific environment being deployed
506
+
507
+
This approach means each environment runs as a completely separate Worker with its own isolated execution context, routes, and bindings. The tagging system helps Cloudflare understand the relationship between these separate Workers for management purposes.
508
+
470
509
## How bindings behave in environments
471
510
472
511
Each binding type has different requirements for environment isolation. Understanding these patterns helps prevent data leakage between environments.
473
512
474
-
### KV namespaces
513
+
### [KV namespaces](/kv/)
475
514
476
515
**Use different `id` values** to keep data separate between environments:
477
516
@@ -503,7 +542,7 @@ Each binding type has different requirements for environment isolation. Understa
503
542
504
543
</WranglerConfig>
505
544
506
-
### D1 databases
545
+
### [D1 databases](/d1/)
507
546
508
547
**Use different `database_id` values** to maintain separate data stores:
509
548
@@ -537,7 +576,7 @@ Each binding type has different requirements for environment isolation. Understa
537
576
538
577
</WranglerConfig>
539
578
540
-
### R2 buckets
579
+
### [R2 buckets](/r2/)
541
580
542
581
**Use different `bucket_name` values** to keep files separate:
543
582
@@ -569,7 +608,7 @@ Each binding type has different requirements for environment isolation. Understa
569
608
570
609
</WranglerConfig>
571
610
572
-
### Queues
611
+
### [Queues](/queues/)
573
612
574
613
**Use different `queue` names** to prevent message mixing:
575
614
@@ -605,7 +644,7 @@ Each binding type has different requirements for environment isolation. Understa
605
644
606
645
</WranglerConfig>
607
646
608
-
### Hyperdrive
647
+
### [Hyperdrive](/hyperdrive/)
609
648
610
649
**Use different `id` values** to connect to separate databases:
611
650
@@ -637,7 +676,7 @@ Each binding type has different requirements for environment isolation. Understa
0 commit comments