Skip to content

Instantly share code, notes, and snippets.

@masonforest
Created November 9, 2012 22:28
Star You must be signed in to star a gist
Embed
What would you like to do?
Installing a Gem on Heroku from a Private GitHub Repo

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:x-oauth-basic@github.com/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']}:x-oauth-basic@github.com/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!

@postmodern
Copy link

Storing the OAuth token in the ENV variable is NOT more secure. When you run bundle install it will save the fully qualified repository URLs into Gemfile.lock, which will then be committed and pushed to heroku. Also the repo OAuth scope is read-write, not read-only.

@siassaj
Copy link

siassaj commented May 6, 2015

I've built an alternative, today it's rough around the edges but we're gonna use it in production for a while so it should evolve.
https://github.com/siassaj/heroku-buildpack-git-deploy-keys

@adis-io
Copy link

adis-io commented May 26, 2015

Created separate user and gave read-only access.

@sebboh
Copy link

sebboh commented Jun 16, 2016

I've forked this gist with instructions on how to do this without committing your OAuth token to source control:
https://gist.github.com/sebboh/f1dfe4f096746c45f3e9ea06a09743a0

@simon0191
Copy link

simon0191 commented Feb 24, 2017

I created a buildpack to solve this problem using a custom ssh key stored as an environment variable: https://github.com/simon0191/custom-ssh-key-buildpack

Feedback is welcome :)

@esampaio
Copy link

@nfedyashev
Copy link

@esampaio thanks for the link! Much easier indeed

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