Skip to content

Instantly share code, notes, and snippets.

@tparveen
Forked from cklanac/cicd-config-clicker.md
Created February 24, 2018 23:18
Show Gist options
  • Save tparveen/6b739244e8c39ec15de82639d2423bd7 to your computer and use it in GitHub Desktop.
Save tparveen/6b739244e8c39ec15de82639d2423bd7 to your computer and use it in GitHub Desktop.
CICD Travis CI and Heroku: (CLI-only and normal versions)

CICD Travis and Heroku

Setup Continuous Integration with Travis CI

Install Travis CLI

Install Travis CI's CLI:

Make sure your repo is ready to go

On your local machine:

  • Run npm test to ensure tests are working correctly locally

  • Add properly configured .travis.yml (see below)

  • Using no database:

language: node_js
node_js: node
  • Using MongoDB
language: node_js
node_js: node
services:
  - mongodb
  • Using PostgreSQL - you need to create the database and create the tables. The easiest way to do the latter is to provide a .sql script file with the CREATE TABLE commands
language: node_js
node_js: node
services:
  - postgresql
before_script:
  - psql -U postgres -c 'CREATE DATABASE "[YOUR-TEST-DATABASE]";'
  - psql -U postgres -f ./db/create-tables.sql -d [YOUR-TEST-DATABASE]
  • Commit and push repo to Github

Activate Travis integration on your repo

On Travis - activate integration:

  • Go to Profile: User (in upper-right) > Accounts
  • Click "Sync Account"
  • Activate repo

On GitHub - verify integration and test:

  • Go to Settings > Integrations & Services
    • There should be an entry for "Travis CI" under Services
    • Click the "edit" button, then click "Test service" to test integration

On Travis:

  • Watch build complete successfully :-)

Setup Continuous Deployment to Heroku

Install Heroku CLI

Install Heroku CLI:

Problems installing? Try the Heroku NPM package npm install -g heroku-cli

On Command line

Configure .travis.yml to deploy to Heroku:

  • Go to project:
    • Run: CD <YOUR PROJECT>
  • Login to Heroku
    • Run: heroku login (and enter your UN/PW)
  • Create an app
    • Run: heroku create <app-name>
  • Login to travis
    • Run: travis login (and enter your UN/PW for GitHub)
  • Add Heroku info to .travis.yml, run:
    • Run: travis setup heroku
  • Follow prompts, make sure the app name and repo are correct
  • Your .travis.yml should look like this:
language: node_js
node_js: node
services:
- <DATABASE-SERVICE-NAME>
before_script:
  # If using postgres then create database and create tables
  - psql -U postgres -c 'CREATE DATABASE "noteful-test";'
  - psql -U postgres -f ./db/noteful.4.sql -d noteful-test
deploy:
provider: heroku
api_key:
  secure: oOa1TMdgeY5+rySYW0HY30j+ot+KUqs1H...
app: <APP-NAME-ON-HEROKU>
on:
  repo: <GITHUB-USERNAME>/<REPO-NAME>
  • Ensure tests are still working
    • Run: npm test
  • Commit and push changes to GitHub
    • Run git commit -am "setup CICD"
  • Changes should deploy to GitHub > Travis CI > Heroku

On Heroku

Skip this section if you are not using a DB.

  • Add connection string to (Environment) Config Vars
    • Go to: Personal Apps > PROJECT NAME > Settings

    • Click "Reveal Config Vars"

    • Using Heroku Mongo or Postgres Add-On:

      • Browse to your Heroku App.
      • Click "Resources"
      • In the Add-ons input: type postgres or mlab
        • Select: the free plan and click Provision
        • Heroku will create "Config Vars" for the database
          • Postgres: DATABASE_URL
          • Mongo: MONGODB_URI
    • If using ElephantSQL or MLab manually then:

      • Browse to your Heroku App.
        • Click "Settings"
        • Click "Reveal Config Vars"
        • Enter appropriate var for database
          • DATABASE_URL = postgres://<UN>:<PW>@stampy.db.elephantsql.com:<PORT>/<DB>
          • MONGODB_URI = mongodb://<UN>:<PW>@<SEREVER>.mlab.com:<PORT>/<DB>

Extras

Add a Travis CI badge to your repo

  • On Travis CI, find your project and click on the badge
  • Change the dropdown menu to "Markdown" and copy the output
  • Add the badge code to your readme.md file, commit and push
  • The code looks something like this:
[![Build Status](https://travis-ci.org/<USERNAME>/<REPO-NAME>.svg?branch=master)](https://travis-ci.org/<USERNAME>/<REPO-NAME>)

Troubleshooting

Issue: Running travis setup heroku returns the wrong repo name.

Example:

travis setup heroku
Deploy only from username/OLD-REPO-NAME? |yes|
Encrypt API key? |yes|

Reason: Travis CLI add a "slug" entry to the .git/config

[travis]
  slug = username/OLD-REPO-NAME

Fix: Remove the [travis] and the slug from .git/config If a deploy property exists in .travis.yml, remove it rerun travis setup heroku Alternatively, run travis setup heroku --force to override existing deploy property

Remove the "deploy" property from the Travis.yml

CICD CLI Ninja Instructions

These are the command line only instructions

“For those who like that sort of thing, this is the sort of thing they like.”

Before starting, make sure you have Travis and Heroku command line clients installed

Install Travis CI's CLI

On a Mac: gem install travis

Install Heroku CLI

On a Mac: brew install heroku

  • Navigate to your project directory
cd ~/Projects/my-project

Checkout master

git checkout master

Set up Continuous Integration with Travis CI

Login to Travis (w/ GitHub Credentials)

travis login

Enable Travis CI to GitHub integration

travis enable

Generates a .travis.yml for Node

travis init node --node-js node

Add and commit .travis.yml then push to master

git add -A
git commit -m 'add .travis.yml'
git push origin master

View Logs (may need to run multiple times)

travis logs

Login to Heroku

heroku login

Create an app on Heroku

heroku create [APP-NAME]

Configure .travis.yml with Heroku deployment info

travis setup heroku

Commit and push

git commit -am 'setup heroku'
git push origin master

View Logs (may need to run multiple times)

travis logs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment