Gitlab Node Deployments via Heroku

June 11, 2019

One of the sweet features of using Gitlab is baked in Continuous Integration/ Continuous Deployment (CI/CD) for all your code. This allows for better developer productivity as well as timely error tracking and bug reporting.

Heroku is a popular Platform as a Service model for deploying apps. Most of their their CI/CD is centered around Github integrations. And plenty of tutorials are available for Ruby and Python apps.

This is for deploying a simple Node application using Express.

You would first need to find your Heroku API key, go to this link: https://dashboard.heroku.com/account

Add this as a variable in your Gitlab's Settings -> CI/CD -> Variables -> Expand section.

Here is how your .gitlab-ci.yml file should look (it should be located or created in the root of  your Node application):

image: node:8.7.0

before_script:  
  - apt-get update -qq && apt-get install

stages:
  - build
  - test
  - deploy

cache:
  paths:
    - node_modules/

install_dependencies:
  stage: build
  script:
    - npm install
  artifacts:
    paths:
      - node_modules/

code_testing:
  stage: test
  script: npm test

deploy_heroku:
  stage: deploy
  image: ruby:latest
  script:
    - apt-get update -qy
    - apt-get install -y ruby-dev
    - gem install dpl
    - dpl --provider=heroku --app=televeda-livestream --api-key=$HEROKU_API_KEY
  only:
    - master

If everything is configured correctly, this will deploy all changes to your master branch directly via Heroku. Make sure to change the YOUR_HEROKU_APP_NAME variable above with the correct  name of your app.

Additional Reading: