Skip to content

Instantly share code, notes, and snippets.

@jsteiner
Forked from masonforest/gist:4048732
Created February 6, 2014 16:22
Show Gist options
  • Save jsteiner/8847497 to your computer and use it in GitHub Desktop.
Save jsteiner/8847497 to your computer and use it in GitHub Desktop.

Installing a Gem on Heroku from a Private GitHub Repo

Sometimes you want to use a gem on Heroku that is in a private repository on GitHub.

Using git over http you can authenticate to GitHub using basic authentication. However, we don't want to embed usernames and passwords in Gemfiles. Instead, we can use authentication tokens.

  1. Get an OAuth Token from GitHub

First you will need to get an OAuth Token from GitHub using your own username and "note"

$ curl -u 'masonforest' -d '{"scopes":["repo"],"note":"Ventana Example App"}' https://api.github.com/authorizations
  1. Authenticate bundler to GitHub via OAuth Token

Add this line to your Gemfile replacing "your_token" with the token you got from step 1. In this example we are installing the 'ventana' gem:

gem 'ventana', git: "https://your_token:[email protected]/thoughtbot/ventana.git"

EXPERIMENTAL ALTERNATIVE: Storing the OAuth token in an environment variable (more secure)

For additional security you can store your OAuth token in an environment variable. This way your token is not included in your codebase which is insecure. However this technique relies on on a Heroku labs feature which can change/be revoked at any time.

Change the line in your Gemfile to

gem 'ventana', git: "https://#{ENV['GITHUB_TOKEN']}:[email protected]/thoughtbot/ventana.git"

Then set the your access token locally using the token you got from above:

$ export GITHUB_TOKEN=your_token

Now bundle and if everything works locally you are ready to deploy to Heroku!

Environment variables such as GITHUB_TOKEN are not available at build time to Heroku by default.

To make them available, you will need to enable the "user-env-compile" feature

$ heroku labs:enable user-env-compile -a ventana-demo

Finally add the GITHUB_TOKEN to your Heroku environment

$ heroku config:add GITHUB_TOKEN=your_token

You now have a private gem installed on Heroku!

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