在 Netlify 函数中设置环境变量的正确方法是什么?
What's the proper way of setting environment variable in Netlify functions?
在netlify中设置环境变量的正确方法是什么?我希望能够根据环境为变量设置不同的值。
伪代码:
let host;
if (process.env.GATSBY_CUSTOM_CONTEXT === 'production') {
host = process.env.PRODUCTION_HOST
} else if (process.env.GATSBY_CUSTOM_CONTEXT === 'development') {
host = process.env.DEVELOPMENT_HOST
}
我试过通过 CLI 传递环境变量,比如 GATSBY_CUSTOM_CONTEXT=production gatsby build
,我也试过对 cross-env
使用相同的命令。
我的其他尝试使用了 netlify.toml
:
[build]
base = "/"
publish = "public"
command = "yarn build"
functions = "src/functions"
[context.production]
[context.production.environment]
GATSBY_CUSTOM_CONTEXT = "production"
所有这些选项在本地与 netlify dev
一起使用,但在生产中 GATSBY_CUSTOM_CONTEXT
始终是 undefined
。
很遗憾,目前似乎不支持您查找的内容。尽管他们提供了另一种方法。
我在他们的文档中找到了这个片段:
CALLING ENVIRONMENT VARIABLES
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 in
the file with environment variable values, 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 HEADER_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections.
- Create an environment variable, for example PROD_API_LOCATION, with the desired value. You can create environment variables in the
toml file or in our UI. You might use the latter to keep sensitive
values out of your repository.
- Prepend a replacement command to your build command. Here’s an example for a site using yarn build to build: sed -i
s/HEADER_PLACEHOLDER/${PROD_API_LOCATION}/g netlify.toml && yarn build
您无法在 Netlify 函数中解析环境变量的原因是 在您提出问题时,Netlify 不会从 netlify.toml
文件.
您必须将它们放入 app.netlify.com
仪表板站点设置中的管理面板。
在netlify中设置环境变量的正确方法是什么?我希望能够根据环境为变量设置不同的值。
伪代码:
let host;
if (process.env.GATSBY_CUSTOM_CONTEXT === 'production') {
host = process.env.PRODUCTION_HOST
} else if (process.env.GATSBY_CUSTOM_CONTEXT === 'development') {
host = process.env.DEVELOPMENT_HOST
}
我试过通过 CLI 传递环境变量,比如 GATSBY_CUSTOM_CONTEXT=production gatsby build
,我也试过对 cross-env
使用相同的命令。
我的其他尝试使用了 netlify.toml
:
[build]
base = "/"
publish = "public"
command = "yarn build"
functions = "src/functions"
[context.production]
[context.production.environment]
GATSBY_CUSTOM_CONTEXT = "production"
所有这些选项在本地与 netlify dev
一起使用,但在生产中 GATSBY_CUSTOM_CONTEXT
始终是 undefined
。
很遗憾,目前似乎不支持您查找的内容。尽管他们提供了另一种方法。
我在他们的文档中找到了这个片段:
CALLING ENVIRONMENT VARIABLES
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 in the file with environment variable values, 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 HEADER_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections.
- Create an environment variable, for example PROD_API_LOCATION, with the desired value. You can create environment variables in the toml file or in our UI. You might use the latter to keep sensitive values out of your repository.
- Prepend a replacement command to your build command. Here’s an example for a site using yarn build to build: sed -i s/HEADER_PLACEHOLDER/${PROD_API_LOCATION}/g netlify.toml && yarn build
您无法在 Netlify 函数中解析环境变量的原因是 在您提出问题时,Netlify 不会从 netlify.toml
文件.
您必须将它们放入 app.netlify.com
仪表板站点设置中的管理面板。