Skip to content

Instantly share code, notes, and snippets.

@cklanac
Last active June 8, 2023 04:11
Show Gist options
  • Save cklanac/789e902b2f03963fabaf9032f35b19d7 to your computer and use it in GitHub Desktop.
Save cklanac/789e902b2f03963fabaf9032f35b19d7 to your computer and use it in GitHub Desktop.
CICD: Travis CI and Heroku Instructions: 2 Versions - Point-n-Click and CLI Ninja

Setup Travis and Heroku CICD: Point-n-Click

Continuous Integration with Travis CI

Configure travis.yml

  • Run npm test to ensure tests are working correctly locally
  • Create .travis.yml file in repo
  • Add the following to configure for node
language: node_js
node_js: node
  • Commit your changes, but don't push to GitHub yet.

Activate Github <=> Travis integration on your repo

Activate - On Travis:

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

Push to trigger Git Hook

  • Push your changes to Github to trigger the git hook
  • Watch Travis the build and test

Continuous Deployment to Heroku

Configure Heroku Pipeline on Heroku

  • Go to: Personal Apps > PROJECT NAME > Deploy
  • Under "Deployment method" choose GitHub
  • Under "Connect to GitHub" choose the account and repo to deploy
  • Under "Automatic deploys" choose the branch to deploy from
  • Important - Check "Wait for CI to pass before deploy" checkbox
  • Click "Enable Automatic Deploys"
  • Commit and push to trigger the process

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>)

Setup Travis and Heroku CICD: CLI Ninja

These are the command line only instructions

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

Install Travis and Heroku CLI

Ensure Travis and Heroku command line clients installed

  • travis --version
  • heroku --version

If they are not installed, then install using the following directions:

Continuous Integration with Travis CI

Navigate to the project directory and checkout the correct branch

cd ~/Projects/my-project
git checkout master

Login to Travis (w/ GitHub Credentials)

travis login

Enable Travis CI to GitHub integration

travis enable

Generate a .travis.yml for Node

travis init node --node-js node

Note: if setting up a database, please see database config below

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

Continuous Deployment to Heroku

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

CICD Configure Mongo or Postgres Database

Configure travis.yml

  • Node with no database:
language: node_js
node_js: node
  • Node with MongoDB
language: node_js
node_js: node
services: mongodb
  • Node with PostgreSQL
  • Travis needs a database and tables. The following assumes the .sql script contains 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]

Configure Environment Variables on Heroku

On Heroku

  • Add connection string to (Environment) Config Vars
    • Go to: Personal Apps > PROJECT NAME > Settings
    • Click "Reveal Config Vars"
    • If using Heroku Add-On for Mongo or Postgres then:
      • 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 stand-alone ElephantSQL or MLab 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>

Troubleshooting

Problem: 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 adds a "slug" to the .git/config. The slug may be outdated if you have renamed the repo.

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

Solution: Remove the slug from .git/config and rerun

  • run git config --unset travis.slug to remove the slug
  • rerun travis setup heroku Alternatively, you can run travis setup heroku --store-repo [REPO-NAME] to store a new slug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment