Heroku管道-将env变量转入生产阶段 [英] Heroku pipeline - staging env variable carried into production
问题描述
我最近将一个React/Express应用程序部署到了Heroku,但是我遇到了环境变量的问题,这些环境变量是内置应用程序和Heroku部署管道的一部分-简而言之,升级到production
时,应用程序的staging
版本将被保留-我可以正确设置环境变量的唯一方法是将应用程序直接推送到production
,这确实违反了部署管道的目的第一名.这是该情况的摘要:
I've recently deployed a React/Express app to Heroku, but I'm having an issue with environment variables that are part of the built app and the Heroku deployment pipeline - in a nutshell, the values of environment variables from the app's staging
release are being carried over when promoting to production
- the only way that I can get the environment variables to set correctly is to push the app directly to production
, which really defeats the purpose of the deployment pipeline in the first place. Here's a summary of the scenario:
有问题的环境变量是API_URL
,它在webpack.config.js
中被引用,如下所示:
The environment variable in question is API_URL
, which is referenced in webpack.config.js
like so:
plugins: [
new webpack.DefinePlugin({
'API_URL': JSON.stringify(process.env.API_URL || 'http://localhost:4000/api/v1')
})
]
API本身是另一个具有staging
和production
版本的Heroku应用程序,因此API_URL
环境变量的值在我的React应用程序Heroku配置中分别设置为https://staging-api-12345.herokuapp.com/api/v1
和https://production-api-12345.herokuapp.com/api/v1
.
The API is itself another Heroku app with staging
and production
releases, and so the values of the API_URL
environment variable are set in my React app Heroku configuration as https://staging-api-12345.herokuapp.com/api/v1
and https://production-api-12345.herokuapp.com/api/v1
, respectively.
当我将React应用程序推至staging
时,它可以完美运行-但是,当我将应用程序升级至production
并首次调用API时,它仍然指向https://staging-api-12345.herokuapp.com/api/v1
.好的,我知道为什么会这样-该应用是在被推送到staging
时生成的...因此,在升级到production
之后,我尝试重新构建该应用,但是那没有用,它仍然使用了
When I push my React app up to staging
, it works perfectly - however, when I promote the app to production
and make the first call to the API, it is still pointing to https://staging-api-12345.herokuapp.com/api/v1
. Ok well, I understand why that is the case - the app was built when being pushed to staging
... so I tried rebuilding the app after promoting to production
, but that did not work, it still used the staging
environment variables.
使用Heroku部署管道时,是否有办法强制重新构建应用程序插件,使其能够捕获不同的环境变量?
When using the Heroku deployment pipeline, is there a way to force the app slug to rebuild so that it will catch the different environment variables?
推荐答案
您无法重建该段,流水线的重点是在应用之间移动相同的段.
You cannot rebuild the slug, the main point of pipelines is to move the same slug between apps.
您需要做的是在运行时而不是在构建过程中获取API_URL
.您可以将所有env放在一个文件中,例如env.js
What you need to do is to fetch API_URL
at runtime and not during build process. You can put all envs in one file, for example env.js
export const API_URL = process.env.API_URL;
export const OTHER_VAR = process.env.OTHER_VAR;
然后将您需要的内容导入其他文件中
And then just import what you need in other files
import { API_URL, OTHER_VAR } from 'env.js';
这篇关于Heroku管道-将env变量转入生产阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!