简体   繁体   English

如何部署使用grunt到heroku的节点应用程序

[英]How to deploy node app that uses grunt to heroku

I'm using grunt and also grunt plugins like grunt-contrib-copy , grunt-contrib-mincss (that listed as npm dependencies for my application). 我正在使用grunt和grunt插件,如grunt-contrib-copygrunt-contrib-mincss (我的应用程序列为npm依赖项)。

Also I don't commit npm_modules folder and public folder, where all generated files are. 此外,我不提交npm_modules文件夹和public文件夹,其中所有生成的文件都是。 And I can't figure out how to build my app (I have grunt build command) after deploy and setup my server (it's already looking for public folder). 在部署和设置我的服务器之后,我无法弄清楚如何构建我的应用程序(我有grunt build命令)(它已经在寻找public文件夹)。

I saw some stuff like grunt-heroku-deploy , but it seems me a bad idea to commit before upload. 我看到了一些像grunt-heroku-deploy这样的东西,但在上传之前提交我似乎是一个坏主意。 Maybe there are some gentle decisions... Any thoughts? 也许有一些温和的决定......有什么想法吗?

npm has a support for a postinstall step (among many others) that might be just what you're looking for. npm支持postinstall步骤(以及许多其他步骤),这可能正是您正在寻找的。

The node.js heroku buildpack runs this command when you push to heroku to resolve build dependencies: 当您推送到heroku以解析构建依赖项时,node.js heroku buildpack会运行此命令:

$ npm install --production

https://devcenter.heroku.com/articles/nodejs-support#build-behavior https://devcenter.heroku.com/articles/nodejs-support#build-behavior

If you take a look at the npm documentation, you can setup a series of scripts to run either before or after anyone runs npm install for your package. 如果你看一下npm文档,你可以设置一系列脚本,在任何人为你的包运行npm install之前或之后运行。 It's configured in the scripts property of package.json . 它在package.jsonscripts属性中配置。 The scripts property allows to run custom scripts (including grunt ) when certain things happen in a package's lifecycle. scripts属性允许在包的生命周期中发生某些事情时运行自定义脚本(包括grunt )。

For example, to echo some text and run the grunt command whenever anyone (including Heroku) runs npm install , add this to your package.json : 例如,要响应某些文本并在任何人(包括Heroku)运行npm install时运行grunt命令,请将其添加到package.json

{
  ...
  "scripts": {
    "postinstall": "echo postinstall time; ./node_modules/grunt-cli/bin/grunt <your task name>"
  },
  ...
}

https://npmjs.org/doc/scripts.html https://npmjs.org/doc/scripts.html

Important caveats: 重要提示:

  • You might have to change the path to the grunt binary in the postinstall script, check the error output if the grunt command doesn't execute. 您可能必须在postinstall脚本中更改grunt二进制文件的路径,如果grunt命令未执行,请检查错误输出。
  • grunt and grunt-cli must be listed as a dependency in your package.json so it gets installed by Heroku. 必须将gruntgrunt-cli列为package.jsondependency ,以便Heroku安装它。 Listing them under devDependencies is not sufficient since Heroku won't install those. devDependencies它们是不够的,因为Heroku不会安装它们。 Also, note that Heroku won't install it as a global package so to execute it on Heroku you're going to have to use a relative path (as it is configured above). 另外,请注意Heroku不会将其安装为全局包,因此要在Heroku上执行它,您将不得不使用相对路径(如上所述)。

If this doesn't work (you'll probably need to fiddle with the relative paths a bit), then you might want to consider writing your own custom buildpack for Heroku . 如果这不起作用(您可能需要稍微改变相对路径),那么您可能需要考虑为Heroku编写自己的自定义构建包

Update 更新

As of 0.4, the grunt package no longer contains the grunt binary, which is now part of the grunt-cli package. 从0.4开始, grunt包不再包含grunt二进制文件,它现在是grunt-cli包的一部分。 The answer has been updated to reflect this. 答案已更新,以反映这一点。

This looks like it will largely be solved when the Heroku Platorm API slug and release features make it into the mainline. 当Heroku Platorm API slugrelease功能进入主线时,看起来很可能会解决这个问题。 At that point, you can build your code locally (or on a ci server), package it up and send it to heroku via an API call and release it from there. 此时,您可以在本地(或在ci服务器上)构建代码,将其打包并通过API调用将其发送到heroku并从那里发布。

This is still in the beta period and was only announced on December 19, 2013. 这仍处于测试期,并且仅在2013年12月19日公布。

https://devcenter.heroku.com/articles/platform-api-deploying-slugs https://devcenter.heroku.com/articles/platform-api-deploying-slugs

I was never super happy with how many people seemed ok with checking in your generated code into git or the NPM postinstall hook. 对于将生成的代码签入git或NPM postinstall挂钩,有多少人似乎没问题,我从来都不高兴。 :( :(

Plus from a philosophical stance, doing a build during a release is simply another potential failure point. 除了哲学立场之外,在发布期间进行构建只是另一个潜在的失败点。


Just for fun : Since that's not finalized yet, here's a bash script I threw together you can use for the time being to build your code on a deployment branch, commit it, deploy it to heroku and then remove the deployment branch. 只是为了好玩 :既然还没有最终确定, 这里是一个bash脚本我把它放在一起你可以暂时用来在部署分支上构建你的代码,提交它,将它部署到heroku然后删除部署分支。 (I really am not a fan of bash deployment scripts, so I'm really looking forward to the platform API additions) (我真的不喜欢bash部署脚本,所以我真的期待平台API的增加)

#!/bin/bash
set -e 

# Delete current deploy branch
git branch -D deploy
# Create new deploy branch based on master
git checkout -b deploy
# Grunt comands to build our site
grunt build:production
# the dist/ directory is in my .gitignore, so forcibly add it
git add -f dist/
git commit -m "Deploying to Heroku"
# Push it up to heroku, the -f ensures that heroku won't complain
git push heroku -f deploy:master
# Switch it back to master
git checkout master

Grunt (et al.) is a build tool, not (really) something you should be packaging up and running on production. Grunt(等人)是一个构建工具,而不是(真的)你应该在生产中打包和运行的东西。 A different approach would be to use Grunt to prepare your project locally (or better on a CI server) before only pushing the built files to Heroku. 另一种方法是在仅将构建的文件推送到Heroku之前,使用Grunt在本地(或更好地在CI服务器上)准备项目。 As already mentioned Heroku will do an npm install on your app after its pushed which should be enough on its own to finally prepare your app. 正如已经提到的,Heroku将在推送后在你的应用程序上进行npm install ,这应该足以让自己最终准备你的应用程序。

I have it set up so that the Grunt derived/built Heroku app lives in a totally separate Git repo to my main app source code repo. 我已将其设置为使得Grunt派生/构建的Heroku应用程序存在于我的主应用程序源代码仓库中的完全独立的Git仓库中。 So that when I do a grunt deploy it optimises and copies the relevant files to the Heroku repo, tidies it up ( git add -A etc.) and then git push heroku master (or whatever). 因此,当我进行grunt deploy它会优化并将相关文件复制到Heroku repo,整理它( git add -A等),然后git push heroku master (或其他)。

It seems like a cleaner separation of concerns if your live servers are only responsible for running a pre-built app package. 如果您的实时服务器仅负责运行预构建的应用程序包,那么这似乎是一种更清晰的关注点分离。

YMMV of course, and the accepted answer above is totally valid too ... especially on a well understood and stable live environment like Heroku. YMMV当然,以及上面接受的答案也是完全有效的......尤其是在像Heroku这样一个易于理解和稳定的现场环境中。

Heroku buildpack works fine for me. Heroku buildpack对我来说很好。 Great stuff. 好东西。

To get this working with grunt 4.0 I followed the instructions here https://discussion.heroku.com/t/grunt-on-heroku/98/2 . 为了使用grunt 4.0,我按照https://discussion.heroku.com/t/grunt-on-heroku/98/2中的说明进行操作。 The only change I had to make was to remove the path to grunt as using unix style slashes would make it fail in windows and vice versa. 我必须做的唯一改变就是删除grunt的路径,因为使用unix样式的斜杠会使它在windows中失败,反之亦然。 Luckily you don't even need to specify the path as NPM will look for grunt in the node_modules/.bin folder https://npmjs.org/doc/scripts.html#path . 幸运的是,您甚至不需要指定路径,因为NPM将在node_modules / .bin文件夹https://npmjs.org/doc/scripts.html#path中查找grunt。

  1. make sure you have both grunt and grunt-cli installed locally in your package.json even if grunt tells you to install the cli globally: $: npm i -S grunt grunt-cli 确保你的package.json中本地安装了grunt和grunt-cli,即使grunt告诉你全局安装cli:$: npm i -S grunt grunt-cli

  2. add a postinstall step to your package.json that looks like this: "postinstall": "grunt prod" 在package.json中添加一个postinstall步骤,如下所示: "postinstall": "grunt prod"

Check out this tutorial: https://medium.com/p/c227cb1ddc56 . 查看本教程: https//medium.com/p/c227cb1ddc56 It explains how you can deploy a grunt app on Heroku with a custom buildpack. 它解释了如何使用自定义构建包在Heroku上部署grunt应用程序。

The npm postinstall step is probably your best option, since you can invoke grunt from there. npm postinstall步骤可能是你最好的选择,因为你可以从那里调用grunt。 But you should also check out a custom buildpack, such as heroku-buildpack-nodejs-grunt . 但是你也应该检查一个自定义的buildpack,比如heroku-buildpack-nodejs-grunt

This post is Rails-specific but I don't see why you couldn't use it with any back-end framework and just swap the Ruby buildpack with whatever you're using. 这篇文章是特定于Rails的,但我不明白为什么你不能在任何后端框架中使用它,只是将Ruby buildpack与你正在使用的任何东西交换。

The solution is basically to use multi buildpacks, and have the Node/Grunt buildpack run grunt build for you right on Heroku. 解决方案基本上是使用多个grunt build包,并在Heroku上为您提供Node / Grunt buildpack运行grunt build

Significantly, this solution does not have you check build artifacts into version control. 值得注意的是,此解决方案没有将构建工件检查到版本控制中。 (Yay!!!) (好极了!!!)

http://www.angularonrails.com/deploy-angular-rails-single-page-application-heroku/ http://www.angularonrails.com/deploy-angular-rails-single-page-application-heroku/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM