node.js 如何将使用 grunt 的节点应用程序部署到 heroku

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13784600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:45:25  来源:igfitidea点击:

How to deploy node app that uses grunt to heroku

node.jsherokunpmgruntjs

提问by ValeriiVasin

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-copy, grunt-contrib-mincss(列为我的应用程序的 npm 依赖项)。

Also I don't commit npm_modulesfolder and publicfolder, where all generated files are. And I can't figure out how to build my app (I have grunt buildcommand) after deploy and setup my server (it's already looking for publicfolder).

此外,我不提交所有生成的文件所在的npm_modules文件夹和public文件夹。grunt build在部署和设置我的服务器(它已经在寻找public文件夹)之后,我无法弄清楚如何构建我的应用程序(我有命令)。

I saw some stuff like grunt-heroku-deploy, but it seems me a bad idea to commit before upload. Maybe there are some gentle decisions... Any thoughts?

我看到了一些类似的东西grunt-heroku-deploy,但在上传之前提交似乎是个坏主意。也许有一些温和的决定......有什么想法吗?

回答by smithclay

npm has a support for a postinstallstep (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 installfor your package. It's configured in the scriptsproperty of package.json. The scriptsproperty allows to run custom scripts (including grunt) when certain things happen in a package's lifecycle.

如果您查看 npm 文档,您可以设置一系列脚本以在任何npm install人为您的包运行之前或之后运行。它在 的scripts属性中配置package.json。该scripts属性允许grunt在包的生命周期中发生某些事情时运行自定义脚本(包括)。

For example, to echo some text and run the gruntcommand whenever anyone (including Heroku) runs npm install, add this to your package.json:

例如,要grunt在任何人(包括 Heroku)运行时回显一些文本并运行命令npm install,请将其添加到您的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 postinstallscript, check the error output if the gruntcommand doesn't execute.
  • gruntand grunt-climust be listed as a dependencyin your package.jsonso it gets installed by Heroku. Listing them under devDependenciesis not sufficient since Heroku won't install those. 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).
  • 您可能需要更改postinstall脚本中grunt 二进制文件的路径,如果grunt命令未执行,请检查错误输出。
  • grunt并且grunt-cli必须dependency在您的目录中列为 apackage.json以便由 Heroku 安装。将它们列在下面devDependencies是不够的,因为 Heroku 不会安装它们。另请注意,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 gruntpackage no longer contains the gruntbinary, which is now part of the grunt-clipackage. The answer has been updated to reflect this.

从 0.4 开始,grunt包不再包含grunt二进制文件,现在它是grunt-cli包的一部分。答案已更新以反映这一点。

回答by Mark G.

This looks like it will largely be solved when the Heroku Platorm API slugand releasefeatures make it into the mainline. 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.

当 Heroku Platorm APIslugrelease功能进入主线时,这看起来将在很大程度上得到解决。那时,您可以在本地(或在 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 hook 感到满意。:(

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 scriptI 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. (I really am not a fan of bash deployment scripts, so I'm reallylooking forward to the platform API additions)

只是为了好玩:由于这还没有最终确定,这是我拼凑的一个 bash 脚本,您可以暂时使用它在部署分支上构建代码,提交它,将其部署到 heroku,然后删除部署分支。(我真的不是 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

回答by Jed Richards

Grunt (et al.) is a build tool, not (really) something you should be packaging up and running on production. 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. As already mentioned Heroku will do an npm installon your app after its pushed which should be enough on its own to finally prepare your app.

Grunt(等人)是一个构建工具,而不是(真的)你应该打包并在生产中运行的东西。另一种方法是在仅将构建的文件推送到 Heroku 之前,使用 Grunt 在本地(或更好地在 CI 服务器上)准备您的项目。如前所述,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. So that when I do a grunt deployit optimises and copies the relevant files to the Heroku repo, tidies it up (git add -Aetc.) and then git push heroku master(or whatever).

我已经设置了它,以便 Grunt 派生/构建的 Heroku 应用程序位于与我的主应用程序源代码存储库完全独立的 Git 存储库中。因此,当我执行grunt deploy它时,它会优化并将相关文件复制到 Heroku 存储库,整理它(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 这样易于理解和稳定的实时环境中。

回答by ValeriiVasin

Heroku buildpackworks fine for me. Great stuff.

Heroku buildpack对我来说很好用。好东西。

回答by rob

To get this working with grunt 4.0 I followed the instructions here 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. 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.

为了让它与 grunt 4.0 一起工作,我按照此处的说明进行操作https://discussion.heroku.com/t/grunt-on-heroku/98/2。我必须做的唯一更改是删除 grunt 的路径,因为使用 unix 样式的斜杠会使它在 Windows 中失败,反之亦然。幸运的是,您甚至不需要指定路径,因为 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

  2. add a postinstall step to your package.json that looks like this: "postinstall": "grunt prod"

  1. 确保你在 package.json 中本地安装了 grunt 和 grunt-cli,即使 grunt 告诉你全局安装 cli:$: npm i -S grunt grunt-cli

  2. 将安装后步骤添加到您的 package.json 中,如下所示: "postinstall": "grunt prod"

回答by Bruno

Check out this tutorial: https://medium.com/p/c227cb1ddc56. It explains how you can deploy a grunt app on Heroku with a custom buildpack.

查看本教程:https: //medium.com/p/c227cb1ddc56。它解释了如何使用自定义构建包在 Heroku 上部署 grunt 应用程序。

回答by Dan Kohn

The npm postinstall step is probably your best option, since you can invoke grunt from there. But you should also check out a custom buildpack, such as heroku-buildpack-nodejs-grunt.

npm postinstall 步骤可能是您最好的选择,因为您可以从那里调用 grunt。但是您还应该查看自定义构建包,例如heroku-buildpack-nodejs-grunt

回答by Jason Swett

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 buildfor you right on Heroku.

解决方案基本上是使用多构建包,并grunt build在 Heroku 上为您运行 Node/Grunt构建包。

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/