npm - What is the best way to load environment variables in your build
问题描述
For a project I am working on I am setting up a BitBucket pipeline. Everything works great except for the environment vars. One of the usages of my env vars is the API_URL. I am loading the url by using process.env.API_URL
. Locally this works great since it loads the .env file by using dotenv-webpack
. However, in the pipeline I don't know what the best way is to load these vars. Should I add all the vars to Bitbucket and add export API_URL=$API_URL
for every var in the pipeline file or are there better ways to load the vars?
解决方案
工作区、存储库和部署变量是要走的路。您需要哪一个取决于您希望这些变量涵盖的范围(命名非常不言自明:一些变量不应该在整个帐户中更改它们的值;其他变量只对特定存储库有意义;最后,一些值是特定于部署环境)。
只需在 Bitbucket UI 或使用 API 中定义变量,并引用它们的值bitbucket-pipelines.yml
或您从中调用的任何脚本。您无需添加任何export
语句。这是一个非常好的文档,解释了详细信息 - https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/ -User-defined variables
部分与您的问题特别相关。
或者,如果您提交了.env
文件,您可以按照这个StackOverflow 答案一次简单地导出其内容。
.env
显然,如果它包含任何敏感值,请不要提交。
推荐阅读
- python - 用其他因变量求解未知变量
- ms-access - 在数据表或连续表单视图中的表单上,我们如何将第二个组合框中的可能值基于在第一个组合框中选择的值?
- postgresql - 在使用非 Equi 连接条件时避免使用嵌套循环连接
- airflow - 气流月度延迟是一天还是一个月?
- amazon-dynamodb - dynamodb 在 np dynamodb 或 np vogels 的帮助下,抛出 Invalid schema content 错误
- django - geography=True 的 Django PointField 无法保存数据:SQL 函数错误
- git - 如何在 VisualStudio 2019 中同步多个 repo?
- python - 如何使用python将json字符串作为命令行参数传递
- sql - Oracle - 通过简单查询显示增量 ID
- json - swift api json数值单位逗号控制