App Engine中的环境变量用于本地开发

时间:2021-05-23 22:58:37

What's the best way to set environment variables when developing nodejs on a local machine for App Engine Flex environment? If they're set in app.yaml then they don't get set during local development. Is there a way to force this, or should I use something like dotenv and keep track of the same environment variables in 2 places?

在App Engine Flex环境的本地计算机上开发nodejs时,设置环境变量的最佳方法是什么?如果它们在app.yaml中设置,那么它们在本地开发期间不会被设置。有没有办法强迫这个,或者我应该使用像dotenv这样的东西并在2个地方跟踪相同的环境变量?

2 个解决方案

#1


2  

Sensitive data (e.g. API Keys) should not be committed to source code.

不应将敏感数据(例如API密钥)提交给源代码。

The way I went around that is storing a .env file in Google Storage. Then you can use @google-cloud/storage to download it in production (using a prestart hook) and dotenv to load variables into memory.

我的方法就是将.env文件存储在Google Storage中。然后你可以使用@ google-cloud / storage在生产中下载(使用预启动挂钩)和dotenv将变量加载到内存中。

You may find a full guide here: http://gunargessner.com/gcloud-env-vars/

您可以在这里找到完整的指南:http://gunargessner.com/gcloud-env-vars/


PS: I would go for Aidan's answer for storing any data that is not sensitive. I have myself used dotenv satisfactorily in the past. Similar to that, there's nconf, the package that gcloud itself uses for examples. Pretty neat!

PS:我会选择Aidan的答案来存储任何不敏感的数据。我过去曾经令人满意地使用了dotenv。与此类似,有nconf,gcloud本身用于示例的包。很简约!

#2


0  

Option 1:

require('dotenv').config({path: '/custom/project/root/app.yaml'})

Option 2:

Maintain both a .env file and a .yaml file with the same keys but different values (local and GAE, respectively). In app.yaml, I make sure not to deploy my .env file by adding the following line:

使用相同的键但不同的值(分别为local和GAE)维护.env文件和.yaml文件。在app.yaml中,我确保不要通过添加以下行来部署我的.env文件:

skip_files : .env

skip_files:.env

You will then need to add a check on ('dotenv').config() to make sure that it doesn't error or overwrite your process variables if no .env file is detected.

然后,您需要在('dotenv')。config()上添加一个检查,以确保在没有检测到.env文件的情况下它不会出错或覆盖您的过程变量。

#1


2  

Sensitive data (e.g. API Keys) should not be committed to source code.

不应将敏感数据(例如API密钥)提交给源代码。

The way I went around that is storing a .env file in Google Storage. Then you can use @google-cloud/storage to download it in production (using a prestart hook) and dotenv to load variables into memory.

我的方法就是将.env文件存储在Google Storage中。然后你可以使用@ google-cloud / storage在生产中下载(使用预启动挂钩)和dotenv将变量加载到内存中。

You may find a full guide here: http://gunargessner.com/gcloud-env-vars/

您可以在这里找到完整的指南:http://gunargessner.com/gcloud-env-vars/


PS: I would go for Aidan's answer for storing any data that is not sensitive. I have myself used dotenv satisfactorily in the past. Similar to that, there's nconf, the package that gcloud itself uses for examples. Pretty neat!

PS:我会选择Aidan的答案来存储任何不敏感的数据。我过去曾经令人满意地使用了dotenv。与此类似,有nconf,gcloud本身用于示例的包。很简约!

#2


0  

Option 1:

require('dotenv').config({path: '/custom/project/root/app.yaml'})

Option 2:

Maintain both a .env file and a .yaml file with the same keys but different values (local and GAE, respectively). In app.yaml, I make sure not to deploy my .env file by adding the following line:

使用相同的键但不同的值(分别为local和GAE)维护.env文件和.yaml文件。在app.yaml中,我确保不要通过添加以下行来部署我的.env文件:

skip_files : .env

skip_files:.env

You will then need to add a check on ('dotenv').config() to make sure that it doesn't error or overwrite your process variables if no .env file is detected.

然后,您需要在('dotenv')。config()上添加一个检查,以确保在没有检测到.env文件的情况下它不会出错或覆盖您的过程变量。