首页 > 解决方案 > 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?

标签: npmwebpackenvironment-variablesbitbucketbitbucket-pipelines

解决方案


工作区、存储库和部署变量是要走的路。您需要哪一个取决于您希望这些变量涵盖的范围(命名非常不言自明:一些变量不应该在整个帐户中更改它们的值;其他变量只对特定存储库有意义;最后,一些值是特定于部署环境)。

只需在 Bitbucket UI 或使用 API 中定义变量,并引用它们的值bitbucket-pipelines.yml或您从中调用的任何脚本。您无需添加任何export语句。这是一个非常好的文档,解释了详细信息 - https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/ -User-defined variables部分与您的问题特别相关。

或者,如果您提交了.env文件,您可以按照这个StackOverflow 答案一次简单地导出其内容。

.env显然,如果它包含任何敏感值,请不要提交。


推荐阅读