I'm using grunt and also grunt plugins like grunt-contrib-copy
, grunt-contrib-mincss
(that listed as npm dependencies for my application).
Also I don't commit npm_modules
folder and public
folder, where all generated files are. 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).
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 (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 install
on your app after its pushed which should be enough on its own to finally prepare your app.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 deploy
it optimises and copies the relevant files to the Heroku repo, tidies it up (git add -A
etc.) and thengit push heroku master
(or whatever).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.
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.
The solution is basically to use multi buildpacks, and have the Node/Grunt buildpack run
grunt build
for you right on Heroku.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/
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.
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
add a postinstall step to your package.json that looks like this:
"postinstall": "grunt prod"
Check out this tutorial: https://medium.com/p/c227cb1ddc56. It explains how you can deploy a grunt app on Heroku with a custom buildpack.
npm has a support for a
postinstall
step (among many others) that might be just what you're looking for.The node.js heroku buildpack runs this command when you push to heroku to resolve build dependencies:
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. It's configured in thescripts
property ofpackage.json
. Thescripts
property allows to run custom scripts (includinggrunt
) when certain things happen in a package's lifecycle.For example, to echo some text and run the
grunt
command whenever anyone (including Heroku) runsnpm install
, add this to yourpackage.json
:https://npmjs.org/doc/scripts.html
Important caveats:
postinstall
script, check the error output if thegrunt
command doesn't execute.grunt
andgrunt-cli
must be listed as adependency
in yourpackage.json
so it gets installed by Heroku. Listing them underdevDependencies
is 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).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.
Update
As of 0.4, the
grunt
package no longer contains thegrunt
binary, which is now part of thegrunt-cli
package. The answer has been updated to reflect this.Heroku buildpack works fine for me. Great stuff.