合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
# 环境环境 * [概念](https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments/#concepts) * [用法](https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments/#usage) * [例子](https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments/#examples) * [自定义Webpack配置](https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments/#custom-webpack-configurations) * [环境变量](https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments/#environment-variables) * [无效的配置](https://developers.cloudflare.com/workers/tooling/wrangler/configuration/environments/#invalid-configurations) 环境是一项功能,允许您将同一项目以多个名称部署到多个位置。这些环境被用于与`--env`或`-e`上标志`wrangler build`,`wrangler preview`和`wrangler publish`。 ## 概念 “顶级配置”是指您在`wrangler.toml`“环境配置”顶部指定的配置值,是指您`[env.name]`在`wrangler.toml` 这是一个例子`wrangler.toml`来说明 ~~~toml # top level configuration type = "webpack" name = "my-worker-dev" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "dev.example.com/*" # environment configuration [env.staging] name = "my-worker-staging" route = "staging.example.com/*" # environment configuration [env.production] name = "my-worker" route = "example.com/*" ~~~ ## 用法 环境最常见的用例是在生产环境之前部署到暂存子域。`wrangler publish`将查看您的顶级配置,您可以在其下方指定其他环境。如果未指定这些环境,则每个环境都将从顶级配置中继承这些值,并带有以下警告。 * `type`将始终从顶级配置继承;您不能为不同的环境指定不同的类型。 * 可以从顶层被继承的字段是`account_id`,`zone_id`,`workers_dev`,和`webpack_config`。`kv_namespaces`并且`route`必须为每个环境定义,并且不会被继承。 * `name`被继承。如果排除在环境配置之外,以`my-worker`环境命名的Worker项目`[env.dev]`将变为`my-worker-dev`。 ### 例子 #### 顶级配置 ##### 路线 这个`wrangler.toml`没有定义的环境,并且发布`my-worker`到`example.com/*` ~~~toml type = "webpack" name = "my-worker" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "example.com/*" ~~~ ~~~console $ wrangler publish ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to example.com/* ~~~ ##### worker.dev 这个`wrangler.toml`没有定义的环境,并且发布`my-worker`到`my-worker.<your-subdomain>.workers.dev` ~~~toml type = "webpack" name = "my-worker" account_id = "12345678901234567890" workers_dev = true # this field specifies that the worker should be deployed to workers.dev ~~~ ~~~console $ wrangler publish ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to https://my-worker.<your-subdomain>.workers.dev ~~~ #### 介绍环境 这`wrangler.toml`为基本情况增加了两个环境。 ~~~toml type = "webpack" name = "my-worker-dev" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "dev.example.com/*" [env.staging] name = "my-worker-staging" route = "staging.example.com/*" [env.production] name = "my-worker" route = "example.com/*" ~~~ 为了使用具有此配置的环境,您可以通过该`--env`标志传递环境的名称。 使用此配置,牧马人将以以下方式运行: ~~~console $ wrangler publish ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to dev.example.com/* ~~~ ~~~console $ wrangler publish --env staging ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to staging.example.com/* ~~~ ~~~console $ wrangler publish --env production ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to example.com/* ~~~ #### 与worker.dev的暂存环境 为了将代码部署到worker.dev,必须将其包含`workers_dev = true`在所需的环境中。您`wrangler.toml`可能看起来像这样: ~~~toml name = "my-worker" type = "webpack" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "example.com/*" [env.staging] workers_dev = true ~~~ 使用此配置,牧马人将以以下方式运行: ~~~console $ wrangler publish ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to example.com/* ~~~ ~~~console $ wrangler publish --env staging ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to https://my-worker-staging.<your-subdomain>.workers.dev ~~~ #### worker.dev作为一流目标 如果只想部署到worker.dev,则可以像下面这样配置Wrangler: ~~~toml name = "my-worker-dev" type = "webpack" account_id = "12345678901234567890" workers_dev = true [env.production] name = "my-worker" [env.staging] name = "my-worker-staging" ~~~ 使用此配置,牧马人将以以下方式运行: ~~~console $ wrangler publish ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to https://my-worker-dev.<your-subdomain>.workers.dev ~~~ ~~~console $ wrangler publish --env staging ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to https://my-worker-staging.<your-subdomain>.workers.dev ~~~ ~~~console $ wrangler publish --env production ✨ Built successfully, built project size is 523 bytes. ✨ Successfully published your script to https://my-worker.<your-subdomain>.workers.dev ~~~ ### 自定义Webpack配置 您可以为不同的环境指定不同的Webpack配置。 ~~~toml name = "my-worker-dev" type = "webpack" account_id = "12345678901234567890" workers_dev = true webpack_config = "webpack.dev.js" [env.production] name = "my-worker" webpack_config = "webpack.config.js" [env.staging] name = "my-worker-staging" ~~~ 您的默认`wrangler build`,`wrangler preview`以及`wrangler publish`命令将所有与建设`webpack.dev.js`,如意志`wrangler build -e staging`,`wrangler preview -e staging`和`wrangler publish -e staging`。`wrangler build -e production`,`wrangler preview -e production`和`wrangler publish -e production`都会使用您的`webpack.config.js`文件。 ### 环境变量 您可以为不同的环境指定不同的KV命名空间,机密和文本变量。 ~~~toml name = "my-worker" type = "webpack" account_id = "12345678901234567890" workers_dev = true kv-namespaces = [ { binding = "KV", id = "06779da6940b431db6e566b4846d64db" } ] [env.production] kv-namespaces = [ { binding = "KV", id = "bd46d6484b665e6bd134b0496ad97760" } ] vars = { FOO = "some text", } ~~~ 注意:通过`-e/--env <environment_name>`在使用[`wrangler secret create`](https://developers.cloudflare.com/workers/tooling/wrangler/secret/)命令时传递标志,可以将秘密变量分配给特定的环境。 ## 无效的配置 ### 多种类型 您不能为每种环境指定类型,必须在的顶层指定类型`wrangler.toml`。 ~~~toml name = "my-worker" type = "webpack" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "example.com/*" workers_dev = true [env.staging] type = "rust" ~~~ 牧马人不会对此配置错误,它将根据`webpack`类型进行构建。 ### 多个环境使用相同的名称 您不能使用相同的名称指定多个环境。如果允许这样做,则发布每个环境都将覆盖您以前部署的工作程序,并且行为不明确。 ~~~toml name = "my-worker" type = "webpack" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "example.com/*" [env.staging] name = "my-worker" workers_dev = true ~~~ ~~~console $ wrangler publish Error: ⚠️ Each name in your `wrangler.toml` must be unique, this name is duplicated: my-worker ~~~ ~~~console $ wrangler publish --env staging Error: ⚠️ Each name in your `wrangler.toml` must be unique, this name is duplicated: my-worker ~~~ ### 定义worker\_dev和路线 ~~~toml name = "my-worker" type = "webpack" account_id = "12345678901234567890" zone_id = "09876543210987654321" route = "example.com/*" workers_dev = true [env.staging] workers_dev = true route = "staging.example.com/*" ~~~ 牧马人将无法发布到在`route`旁边定义的环境`workers_dev = true`。 ~~~console $ wrangler publish Error: ⚠️ Your environment should only include `workers_dev` or `route`. If you are trying to publish to workers.dev, remove `route` from your wrangler.toml, if you are trying to publish to your own domain, remove `workers_dev`. ~~~ ~~~console $ wrangler publish --env staging Error: ⚠️ Your environment should only include `workers_dev` or `route`. If you are trying to publish to workers.dev, remove `route` from your wrangler.toml, if you are trying to publish to your own domain, remove `workers_dev`. ~~~