如何在 netlify.toml 配置中插入环境变量
How to interpolate environment variables within netlify.toml config
我想根据环境代理到不同的 api - 我已经尝试了以下主题的一些变体,但没有任何运气。如果可能的话,正确的做法是什么?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
这行不通。
虽然上面的配置在我将 URI 硬编码到 to
字段时有效,但当我尝试插入环境变量时它就失败了。
回答我自己的问题 - 它不受支持,您必须自己手动插入环境变量作为 Netlify 构建的一部分。
是的。这是可能的。这是详细的文档:https://www.netlify.com/docs/continuous-deployment/#deploy-contexts
在我的例子中,我需要为生产和所有其他分支设置一个 REACT_APP_API_URL
分开。这是我使用的:
[context.production.environment]
REACT_APP_API_URL = "https://api.test.im"
[context.deploy-preview.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
[context.branch-deploy.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
它不受支持,但 Netlify 在其文档中建议了一种解决方法 (https://www.netlify.com/docs/netlify-toml-reference):
Using Environment Variables directly as values ($VARIABLENAME) in your
netlify.toml file is not supported. However, the following workflow
can be used to substitute values based on environment variables in the
file, assuming you are only trying to change headers or redirects. The
rest of the file is read BEFORE your build - but those sections are
read AFTER the build process.
- Add a placeholder like
API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers
sections.
- Create an Build Environment Variable, for example API_KEY,
with the desired value. You can do this in the toml file or in our UI
in the Build and Deploy Settings section of your configuration. You
might use the latter to keep sensitive values out of your repository.
- Add a command like this one to your build command: sed -i
s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.
我想根据环境代理到不同的 api - 我已经尝试了以下主题的一些变体,但没有任何运气。如果可能的话,正确的做法是什么?
[build.environment]
API_URI="https://dev-api.foo.com/:splat"
[context.production.environment]
API_URI="https://prod-api.foo.com/:splat"
[[redirects]]
from = "/api/*"
to = "$API_URI"
status = 200
force = true
这行不通。
虽然上面的配置在我将 URI 硬编码到 to
字段时有效,但当我尝试插入环境变量时它就失败了。
回答我自己的问题 - 它不受支持,您必须自己手动插入环境变量作为 Netlify 构建的一部分。
是的。这是可能的。这是详细的文档:https://www.netlify.com/docs/continuous-deployment/#deploy-contexts
在我的例子中,我需要为生产和所有其他分支设置一个 REACT_APP_API_URL
分开。这是我使用的:
[context.production.environment]
REACT_APP_API_URL = "https://api.test.im"
[context.deploy-preview.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
[context.branch-deploy.environment]
REACT_APP_API_URL = "https://api-staging.test.im"
它不受支持,但 Netlify 在其文档中建议了一种解决方法 (https://www.netlify.com/docs/netlify-toml-reference):
Using Environment Variables directly as values ($VARIABLENAME) in your netlify.toml file is not supported. However, the following workflow can be used to substitute values based on environment variables in the file, assuming you are only trying to change headers or redirects. The rest of the file is read BEFORE your build - but those sections are read AFTER the build process.
- Add a placeholder like API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections.
- Create an Build Environment Variable, for example API_KEY, with the desired value. You can do this in the toml file or in our UI in the Build and Deploy Settings section of your configuration. You might use the latter to keep sensitive values out of your repository.
- Add a command like this one to your build command: sed -i s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.