Skip to content

Deploying on Heroku

Gregor edited this page Oct 10, 2016 · 5 revisions

Choose between:

  • Manual deployment with Heroku Toolbelt or
  • Automatic deployment with GitLab CI

Manual deployment

Register with Heroku and create an app: http://heroku.com/

Download the Heroku Toolbelt: https://toolbelt.heroku.com/

You also need an accessible MongoDB instance - you can try MongoHQ which has an easy setup.

Add your db to the production env file in server/config/env/production.js: db: 'mongodb://' + (process.env.DB_PORT_27017_TCP_ADDR || 'localhost') + '/my-awesome-demo',

We will add your connection string and other environment variables next

git init #if no git yet
git add .
git commit -m "initial version"

heroku apps:create
heroku config:add NODE_ENV=production
heroku config:add DB_PORT_27017_TCP_ADDR=user:[email protected]:31426
# install devDependencies too, ugly hack
heroku config:set NPM_CONFIG_PRODUCTION=false

Then push your mean app to Heroku.

$ git push heroku master
$ heroku config:set NODE_ENV=production

Automatic deployment via GitLab CI

1. Heroku & GitLab

Create a GitLab account and project and enable shared runner (if not enabled by default). Register on Heroku and create an app.

3. Install dependencies

npm install --save cross-env

4. Add another script to package.json 'build:production'

"scripts": {
    "start:dev": "gulp",
    "start": "node server",
    "mocha": "node tools/test/run-mocha.js",
    "karma": "node node_modules/karma/bin/karma start karma.conf.js",
    "test": "gulp test",
    "test-e2e": "gulp e2e.test",
    "build:production": "cross-env NODE_ENV=production webpack",
    "postinstall": "node tools/scripts/postinstall.js",
    "snyk-protect": "snyk protect",
    "prepublish": "npm run snyk-protect"
  }

5. Add '.gitlab-ci.yml':

image: mwallasch/docker-ruby-node # required, because we need ruby, node and npm

variables:
  NODE_ENV: "development" # required, because we need to install devDependencies

stages:
  - deploy

deploy_job:
  stage: deploy
  script:
  - npm -v
  - npm install -qs # install all dependencies (and devDependencies)
  - npm run build:production # run webpack, set NODE_ENV=production
  - gem install dpl # install dpl...
  - dpl --skip_cleanup --provider=heroku --app=<app-name> --api-key=$HEROKU_API_KEY #...and deploy (skip_cleanup prevents deleting the previous generated bundled assets)
  only:
  - master

'HEROKU_API_KEY' is the API key you get from Heroku. Set this as project variable in GitLab.

6. Add another line to '.slugignore' because we don't want to deploy the devDependencies to Heroku:

...
/node_modules

This ensures that Heroku re-installs all dependencies with config production.

7. Modify the config files to use MongoDB connection url

This is necessary, because if you use a Heroku Add-on (like mLab) you'll get a connection string instead of seperate params.

/config
   /env
      default.js
      production.js

Modify this line:
   db: 'mongodb://' + (process.env.DB_PORT_27017_TCP_ADDR || 'localhost') + '/mean-prod',
to
   db: process.env.MONGODB_URI,

Now when you push to your repository, an automatic build process is started resulting in a deployment to Heroku.

Clone this wiki locally