How to deploy a node application that uses grunt for heroku

I use grunt as well as grunt plugins such as grunt-contrib-copy , grunt-contrib-mincss (which are listed as npm dependencies for my application).

Also, I do not npm_modules and public folders, where all the generated files are. And I can’t understand how to create my application (I have the grunt build ) after deploying and configuring my server (it is already looking for the public folder).

I saw some things like grunt-heroku-deploy , but it seems like a bad idea for me to complete a transaction before downloading. Maybe there are gentle solutions ... Any thoughts?

+56
npm gruntjs heroku
Dec 09
source share
8 answers

npm supports the postinstall step (among many others), which may be exactly what you are looking for.

node.js heroku buildpack runs this command when you click on the hero to resolve build dependencies:

 $ npm install --production 

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

If you look at the npm documentation, you can set up a series of scripts to run before or after someone runs npm install for your package. It is configured in the scripts property of package.json . The scripts property allows you to run your own scripts (including grunt ) when certain things happen in the package life cycle.

For example, to repeat the text and run the grunt command when someone (including Heroku) runs npm install , add this to your package.json :

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

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

Important reservations:

  • You may need to change the path to the grunt binary in the postinstall script, check the error output if the grunt command fails.
  • grunt and grunt-cli must be listed as dependency in your package.json , so Heroku will install it. Listing them in devDependencies not enough, since Heroku will not install them. Also note that Heroku will not install it as a global package, so you will have to use the relative path (as configured above) to run it on Heroku.

If that doesn't work (you probably have to play around with relative paths a bit), you might want to write your own buildpack for Heroku .

Update

Starting with version 0.4, the grunt package no longer contains the grunt binary, which is now part of the grunt-cli . The answer has been updated to reflect this.

+84
Dec 10 '12 at 6:32
source share

It looks like this will be pretty much resolved when the Heroku Platorm API slug and release functions go into the trunk. At this point, you can create your code locally (or on the ci server), pack it and send it to the hero via an API call and release it from there.

This is still in beta and was only announced on December 19, 2013.

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

I have never been very pleased with how many people looked normal while checking your generated code on git or on the hook for installing NPM. :(

Plus, from a philosophical perspective, assembling during release is just another potential point of failure.




Just for fun . Since this is not finished yet, here is the bash script that I put together, you can use for now to create your code on the deployment branch, commit it, deploy it to the hero, and then delete the deployment branch. (I'm really not a fan of bash deployment scripts, so I'm looking forward to adding platform APIs)

 #!/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 
+24
Jan 10 '14 at 23:54
source share

Grunt (and others) is a building tool, and not (really) something that you need to package and run in production. Another approach would be to use Grunt to prepare the project locally (or better on the CI server) before just popping the embedded files into Heroku. As already mentioned, Heroku will do npm install in your application after it is clicked, which should be enough to finally prepare your application.

I have it configured so that the created Grunch / built Heroku application lives in a completely separate Git repository in my main application source code repository. So when I do grunt deploy , it optimizes and copies the corresponding files to the Heroku repository, picks it up ( git add -A , etc.), and then git push heroku master (or something else).

It seems like a cleaner separation of problems if your servers live only for running a ready-made application package.

YMMV, of course, and the accepted answer above is completely suitable as well ... especially in a well-understood and stable living environment such as Heroku.

+23
Feb 24 '13 at 10:33
source share

Heroku buildpack works great for me. Great stuff.

+6
Dec 09 '12 at 12:29
source share

To work 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 grunt path, as using unix styles will cause windows to crash and vice versa. Fortunately, you don’t even need to specify a path, since NPM will look for grunt in the node_modules / folder. Bin 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 cli globally: $: npm i -S grunt grunt-cli

  • add the postinstall step to your package.json, which looks like this: "postinstall": "grunt prod"

+2
Sep 27 '13 at 3:46
source share

Check out this tutorial: https://medium.com/p/c227cb1ddc56 . It explains how you can deploy your grunt application to Heroku using a special buildpack.

+1
Jan 05 '14 at 8:34
source share

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

0
Nov 02 '13 at 17:21
source share

This post is specific to Rails, but I don’t understand why you cannot use it with any internal infrastructure and just replace Ruby buildpack with what you are using.

The solution basically consists in using multiple buildpacks and has a Node / Grunt buildpack grunt build for you right on Heroku.

Significantly, this solution does not verify the assembly of artifacts in version control. (Yay !!!)

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

0
Aug 01 '14 at 12:43 on
source share



All Articles