gpt4 book ai didi

netlify - 如何在 netlify.toml 配置中插入环境变量

转载 作者:行者123 更新时间:2023-12-02 10:25:15 27 4
gpt4 key购买 nike

我想根据环境代理到不同的 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/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.

  1. Add a placeholder like API_KEY_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections.
  2. 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.
  3. Add a command like this one to your build command: sed -i s/API_KEY_PLACEHOLDER/$API_KEY/g netlify.toml && normal build command.

关于netlify - 如何在 netlify.toml 配置中插入环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com