Environments
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.toml
file.
Review the following environments flow:
- You have created a Worker application named
my-worker
. - You create an environment, for example,
dev
, in the Worker’swrangler.toml
configuration file. - In
wrangler.toml
, you configure thedev
environment by adding bindings and/or routes. - You deploy the Worker using
npx wrangler deploy -e dev
. - In the background, Wrangler creates a new Worker named
my-worker-dev
. - You can now change your
my-worker
Worker code and configuration, and choose which environment to deploy your changes to.
Environments are used with the --env
or -e
flag on wrangler dev
, npx wrangler deploy
, and wrangler secret
.
To create an environment:
- Open your Worker’s
wrangler.toml
file. - Add
[env.<NAME>]
and change<NAME>
to the desired name of your environment. - Repeat step 2 to create multiple environments.
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
.
Review the layout of an example [env.dev]
environment that sets up a custom dev.example.com
route:
You cannot specify multiple environments with the same name.
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
.
After you have configured your environment, run npx wrangler deploy
in your Worker project directory for the changes to take effect.
Non-inheritable keys are configurable at the top-level, but cannot be inherited by environments and must be specified for each environment.
Bindings and environment variables must be specified per each environment in your wrangler.toml
file.
Review the following example wrangler.toml
file:
You may assign environment-specific secrets by running the command wrangler secret put <KEY> -env
.
The following wrangler.toml
file adds two environments, [env.staging]
and [env.production]
, to the wrangler.toml
file. If you are deploying to a Custom Domain or route, you must provide a route
or routes
key for each environment.
In order to use environments with this configuration, you can pass the name of the environment via the --env
flag.
With this configuration, Wrangler will behave in the following manner:
Any defined environment variables (the vars
key) are exposed as global variables to your Worker.
With this configuration, the ENVIRONMENT
variable can be used to call specific code depending on the given environment:
To deploy your code to your *.workers.dev
subdomain, include workers_dev = true
in the desired environment. Your wrangler.toml
file may look like this:
With this configuration, Wrangler will behave in the following manner: