Skip to content

Commit d0123c1

Browse files
committed
updates links, adds API deployments
1 parent 0fe33df commit d0123c1

File tree

1 file changed

+77
-48
lines changed

1 file changed

+77
-48
lines changed

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

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@ head: []
55
description: Deploy multiple isolated copies of your Worker application with independent configurations, resources, and deployment URLs.
66
---
77

8-
import { WranglerConfig, Render, TabItem, Tabs, Steps } from "~/components";
8+
import {
9+
WranglerConfig,
10+
Render,
11+
TabItem,
12+
Tabs,
13+
Steps,
14+
PackageManagers,
15+
} from "~/components";
916

1017
An **environment** is a separate, isolated instance of your Worker application. For example, you can have a `staging` and a `production` environment running simultaneously.
1118

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.
1320

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/).
1522

1623
## Configuration
1724

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:
1926

2027
<WranglerConfig>
2128

@@ -427,51 +434,83 @@ Because environments run as isolated copies of your application, all of your bin
427434
Each environment configuration must explicitly define:
428435

429436
- Environment variables (`vars`)
430-
- All resource bindings (D1 databases, KV namespaces, R2 buckets, etc.)
431-
- Secrets (managed separately via Wrangler or the dashboard)
437+
- All resource bindings ([D1 databases](/d1/), [KV namespaces](/kv/), [R2 buckets](/r2/), etc.)
438+
- [Secrets](/workers/configuration/secrets/) (managed separately via Wrangler or the dashboard)
432439
- Any other configuration specific to that environment
433440

434441
## Deployment workflows
435442

436443
All environments must be defined in your Wrangler configuration file. Once configured, you can deploy environments through two main workflows:
437444

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-
451445
### Automated deployment with Git integration
452446

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.
454448

455449
#### Pull request previews
456450

457451
Every pull request automatically gets its own isolated environment. When you open a PR:
458452

459453
- Cloudflare automatically builds and deploys your code
460-
- You receive two URLs instantly:
461-
- **Preview URL**: `https://<hash>-<worker-name>.<subdomain>.workers.dev` (e.g., `https://0a8a97c0-my-api.korinne.workers.dev`)
462-
- **Branch URL**: `https://<branch-name>-<worker-name>.<subdomain>.workers.dev` (e.g., `https://new-feature-my-api.korinne.workers.dev`)
454+
- You receive two URLs:
455+
- **Preview URL**: A unique URL for each commit - `https://<hash>-<worker-name>.<subdomain>.workers.dev`
456+
- **Branch URL**: A stable URL that always shows the latest commit on that branch - `https://<branch-name>-<worker-name>.<subdomain>.workers.dev`
463457

464458
These preview environments are perfect for:
465459

466460
- Testing changes before merging
467461
- Sharing work with teammates for review
468462
- Running integration tests against PR-specific endpoints
469463

464+
### Manual deployment
465+
466+
#### Wrangler
467+
468+
Deploy to specific environments using [Wrangler](/workers/wrangler/):
469+
470+
<PackageManagers type="exec" pkg="wrangler" args="deploy --env staging" />
471+
<PackageManagers type="exec" pkg="wrangler" args="deploy --env production" />
472+
473+
#### API
474+
475+
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" \
486+
-H "Authorization: Bearer {api_token}" \
487+
-H "Content-Type: application/javascript" \
488+
--data-binary "@worker.js" \
489+
--form 'metadata={"main_module":"worker.js","tags":["service=my-api","environment=production"]}'
490+
```
491+
492+
**Deploy to staging:**
493+
494+
```bash
495+
curl -X PUT "https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/my-api-staging" \
496+
-H "Authorization: Bearer {api_token}" \
497+
-H "Content-Type: application/javascript" \
498+
--data-binary "@worker.js" \
499+
--form 'metadata={"main_module":"worker.js","tags":["service=my-api","environment=staging"]}'
500+
```
501+
502+
The tags identify:
503+
504+
- `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+
470509
## How bindings behave in environments
471510

472511
Each binding type has different requirements for environment isolation. Understanding these patterns helps prevent data leakage between environments.
473512

474-
### KV namespaces
513+
### [KV namespaces](/kv/)
475514

476515
**Use different `id` values** to keep data separate between environments:
477516

@@ -503,7 +542,7 @@ Each binding type has different requirements for environment isolation. Understa
503542

504543
</WranglerConfig>
505544

506-
### D1 databases
545+
### [D1 databases](/d1/)
507546

508547
**Use different `database_id` values** to maintain separate data stores:
509548

@@ -537,7 +576,7 @@ Each binding type has different requirements for environment isolation. Understa
537576

538577
</WranglerConfig>
539578

540-
### R2 buckets
579+
### [R2 buckets](/r2/)
541580

542581
**Use different `bucket_name` values** to keep files separate:
543582

@@ -569,7 +608,7 @@ Each binding type has different requirements for environment isolation. Understa
569608

570609
</WranglerConfig>
571610

572-
### Queues
611+
### [Queues](/queues/)
573612

574613
**Use different `queue` names** to prevent message mixing:
575614

@@ -605,7 +644,7 @@ Each binding type has different requirements for environment isolation. Understa
605644

606645
</WranglerConfig>
607646

608-
### Hyperdrive
647+
### [Hyperdrive](/hyperdrive/)
609648

610649
**Use different `id` values** to connect to separate databases:
611650

@@ -637,7 +676,7 @@ Each binding type has different requirements for environment isolation. Understa
637676

638677
</WranglerConfig>
639678

640-
### Analytics Engine datasets
679+
### [Analytics Engine datasets](/analytics/analytics-engine/)
641680

642681
**Use different `dataset` names** to keep metrics separate:
643682

@@ -669,7 +708,7 @@ Each binding type has different requirements for environment isolation. Understa
669708

670709
</WranglerConfig>
671710

672-
### Routes
711+
### [Routes](/workers/configuration/routing/routes/)
673712

674713
**Must use unique `pattern` values.** Each URL pattern can only point to one Worker.
675714

@@ -696,7 +735,7 @@ Each binding type has different requirements for environment isolation. Understa
696735

697736
</WranglerConfig>
698737

699-
### Service bindings
738+
### [Service bindings](/workers/runtime-apis/bindings/service-bindings/)
700739

701740
**Must specify the target environment** by appending the environment name to the `service` field.
702741

@@ -727,7 +766,7 @@ Each binding type has different requirements for environment isolation. Understa
727766

728767
</WranglerConfig>
729768

730-
### Workflows
769+
### [Workflows](/workflows/)
731770

732771
**Must have unique `name` values.** Workflows are account-level resources that environments reference by name.
733772

@@ -760,7 +799,7 @@ Each binding type has different requirements for environment isolation. Understa
760799

761800
</WranglerConfig>
762801

763-
### Vectorize indexes
802+
### [Vectorize indexes](/vectorize/)
764803

765804
**Must have unique `index_name` values per account.** Cannot be changed after creation.
766805

@@ -791,7 +830,7 @@ Each binding type has different requirements for environment isolation. Understa
791830

792831
</WranglerConfig>
793832

794-
### Durable Objects
833+
### [Durable Objects](/durable-objects/)
795834

796835
**Automatically isolated.** Each environment gets its own instances with separate storage:
797836

@@ -813,7 +852,7 @@ Each binding type has different requirements for environment isolation. Understa
813852

814853
</WranglerConfig>
815854

816-
### Tail consumers
855+
### [Tail consumers](/workers/observability/logs/tail-workers/)
817856

818857
**Automatically isolated.** Route logs to the specified Worker:
819858

@@ -842,7 +881,7 @@ Each binding type has different requirements for environment isolation. Understa
842881

843882
</WranglerConfig>
844883

845-
### AI binding
884+
### [AI binding](/workers-ai/)
846885

847886
**Can share configuration.** Provides access to the same AI models across all environments:
848887

@@ -858,7 +897,7 @@ Each binding type has different requirements for environment isolation. Understa
858897

859898
</WranglerConfig>
860899

861-
### Browser Rendering
900+
### [Browser Rendering](/browser-rendering/)
862901

863902
**Can share configuration.** Provides access to the same browser rendering service:
864903

@@ -874,7 +913,7 @@ Each binding type has different requirements for environment isolation. Understa
874913

875914
</WranglerConfig>
876915

877-
### mTLS certificates
916+
### [mTLS certificates](/workers/runtime-apis/bindings/mtls/)
878917

879918
**Can share configuration.** Can use the same certificate or different ones per environment:
880919

@@ -920,13 +959,3 @@ Each binding type has different requirements for environment isolation. Understa
920959
```
921960

922961
</WranglerConfig>
923-
924-
## Common use cases
925-
926-
Environments enable several development and deployment patterns:
927-
928-
- **Development/Staging/Production workflow** - Test changes in isolated environments before promoting to production
929-
- **Feature branch deployments** - Create temporary environments for testing new features
930-
- **Regional deployments** - Run different configurations for different geographic regions
931-
- **A/B testing** - Deploy variations of your application for experimentation
932-
- **Client-specific deployments** - Maintain separate environments for different customers or tenants

0 commit comments

Comments
 (0)