具有角度2环境的Heroku流水线 [英] Heroku Pipeline with Angular 2 Environments

查看:87
本文介绍了具有角度2环境的Heroku流水线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Angular 2 CLI构建了我的Angular 2项目,并能够使用此 tutorial

现在,我想为应用程序的不同环境(dev,staging,production等)创建一个管道。



在我的package.json中,我有postinstall:ng build -prod我的代码,这是我的应用程序运行的。有没有一种方法可以使我的Heroku设置中的 -prod 更改基于 CONFIG_VARS ?例如,对于开发环境,或者postinstall:ng build -staging,例如postinstall:ng build -dev 作为临时环境。或者我需要设置不同的项目?

解决方案

简短的回答是:不,你需要做一些不同的事情。 $ b

说明:
当您建立Heroku slug时,npm postinstall脚本运行,当您执行git push到管道中的第一个Heroku应用程序时。随后,当您通过您的Heroku管道(例如从开发到分段到制作)推广发布时,预建的Heroku slu is按原样提升,并且不会重建。



因此,假设您在开发应用程序中设置了一个配置变量,它会将您传递给ng build的参数设置为dev。这意味着当你将git推送到你的开发应用程序时,会使用开发选项构建slug。这对于开发应用程序来说很好。然而,当你随后提倡升级和生产时,你将会推广使用开发选项构建的预制嵌块,这不是你想要的。



因此,要获得所需的功能,您需要考虑采取不同的方法。



执行此操作的一种方法是运行ng在npm prestart阶段创建脚本。这应该有效,并且使您能够使用Heroku config vars来修改您的Angular2应用,具体取决于它们所部署的Heroku流水线阶段。但是,我通常不会推荐这种方法。这会导致每次运行npm start时都会运行ng build,这在Heroku上很常见(即至少每24小时一次,加上每次Heroku dynos无论何种原因重新启动时)。这会导致您的应用程序在每次重新启动dynos时所经历的停机时间超过必要的时间。通常情况下,这不是一个好主意。



相反,更好的选择可能是让您的Angular2应用在初始化时查询您的服务器,并让服务器返回任何管道阶段根据常规的Heroku配置变量,您的Angular2应用需要的特定值。

I've built my Angular 2 project using the Angular 2 CLI and was able to deploy the application to Heroku using this tutorial.

Now, I'd like to create a pipeline for the different environments of the applications (dev, staging, production, etc...).

In my package.json I have "postinstall": "ng build -prod" which creates a production build of my code, which my application runs off of. Is there a way that I could have -prod change based on CONFIG_VARS that I would have in my Heroku setup? For example it would say "postinstall": "ng build -dev" for a dev environment or "postinstall": "ng build -staging" for a staging environment. Or would I need to set up my project differently?

解决方案

The short answer is: No, you will need to do something different.

Explanation: The npm postinstall script runs when the Heroku slug is built, when you do a git push to the first Heroku app in your pipeline. Subsequently, when you "promote" releases through your Heroku pipeline (e.g. from "development" to "staging" to "production"), the pre-built Heroku slug is promoted "as is", and is NOT rebuilt.

Hence, let's say you have a config var set up in your "development" app that would set the argument that you pass to "ng build" to "dev". That means that when you git push to your "development" app, the slug will get built with the "dev" options. That is fine for the "development" app. HOWEVER, when you subsequently promote to "staging" and "production", you will be promoting the pre-built slug that was built with the "dev" options, which is NOT what you want.

So, to get the functionality you want you will need to consider a different approach.

One way to do it would be to run your "ng build" script in the "npm prestart" phase. That should work, and enable you to use Heroku config vars to modify your Angular2 app depending on the Heroku pipeline phase they are deployed on. However, I typically would NOT recommend this approach. This will cause your "ng build" to run every time "npm start" is run, which is quite often on Heroku (i.e. at least once every 24 hours or so, plus every time your Heroku dynos restart for whatever reason). This would cause your app to experience longer downtime than necessary EVERY time your dynos restart. Not a good idea, typically.

Instead, a better option may be to have your Angular2 app query your server when it initializes, and have the server return whatever pipeline-phase specific values your Angular2 app needs, based on regular Heroku config vars.

这篇关于具有角度2环境的Heroku流水线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆