简体   繁体   English

在Netlify函数中设置环境变量的正确方法是什么?

[英]What's the proper way of setting environment variable in Netlify functions?

What's the proper way of setting environment variables in netlify? 在netlify中设置环境变量的正确方法是什么? I would like to be able to set different values for the variables depending on the environment. 我希望能够根据环境为变量设置不同的值。

Pseudo code: 伪代码:

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
}

I have tried passing env variable thru CLI, like GATSBY_CUSTOM_CONTEXT=production gatsby build and I also tried using same command with cross-env . 我试过通过CLI传递env变量,比如GATSBY_CUSTOM_CONTEXT=production gatsby build ,我也尝试使用相同的命令和cross-env

My other attempt used netlify.toml : 我的另一个尝试使用netlify.toml

[build]
  base = "/"
  publish = "public"
  command = "yarn build"
  functions = "src/functions"

[context.production]
  [context.production.environment]
    GATSBY_CUSTOM_CONTEXT = "production"

All of these options worked with netlify dev locally, but in production GATSBY_CUSTOM_CONTEXT is always undefined . 所有这些选项都在本地使用netlify dev ,但在生产中GATSBY_CUSTOM_CONTEXT始终undefined

The reason you can't resolve the environment variables in your Netlify functions is because as of the time of your question, Netlify does not transfer the environment variables from the netlify.toml file. 您无法在Netlify函数中解析环境变量的原因是因为截至您提问时, Netlify不会从netlify.toml文件传输环境变量。

You must put them into the admin panel in your site settings in the app.netlify.com dashboard. 您必须将它们放入app.netlify.com仪表板中站点设置的管理面板中。

Unfortunately, what you're looking to doesn't seem to be currently supported. 不幸的是,您目前所期待的似乎并不支持。 Though they provide an alternative approach. 虽然他们提供了另一种方法。

I found this snippet on their docs: 我在他们的文档上找到了这个片段:

CALLING ENVIRONMENT VARIABLES 呼吁环境变量

Using environment variables directly as values ($VARIABLENAME) in your netlify.toml file is not supported. 不支持在netlify.toml文件中直接使用环境变量作为值($ VARIABLENAME)。 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. 在构建之前读取文件的其余部分 - 但是在构建过程之后会读取这些部分。

  1. Add a placeholder like HEADER_PLACEHOLDER somewhere in the netlify.toml redirects or headers sections. 在netlify.toml重定向或标题部分的某处添加一个占位符,如HEADER_PLACEHOLDER。
  2. Create an environment variable, for example PROD_API_LOCATION, with the desired value. 使用所需的值创建环境变量,例如PROD_API_LOCATION。 You can create environment variables in the toml file or in our UI. 您可以在toml文件或UI中创建环境变量。 You might use the latter to keep sensitive values out of your repository. 您可以使用后者将敏感值保留在存储库之外。
  3. Prepend a replacement command to your build command. 在构建命令前添加替换命令。 Here's an example for a site using yarn build to build: sed -is/HEADER_PLACEHOLDER/${PROD_API_LOCATION}/g netlify.toml && yarn build 以下是使用纱线构建来构建网站的示例:sed -is / HEADER_PLACEHOLDER / $ {PROD_API_LOCATION} / g netlify.toml && yarn build

Taken from here: https://www.netlify.com/docs/netlify-toml-reference/ 摘自此处: https//www.netlify.com/docs/netlify-toml-reference/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM