Skip to content

Instantly share code, notes, and snippets.

@sebboh
Forked from masonforest/gist:4048732
Last active February 27, 2024 17:10
Show Gist options
  • Save sebboh/f1dfe4f096746c45f3e9ea06a09743a0 to your computer and use it in GitHub Desktop.
Save sebboh/f1dfe4f096746c45f3e9ea06a09743a0 to your computer and use it in GitHub Desktop.
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.

This method does not add your OAuth token to Gemfile.lock. It uses bundle config to store your credentials, and allows you to configure Heroku to use environment variables when deploying.

  1. Generate an OAuth token from GitHub

  • You only need to check the repo scope when configuring the token permissions
  1. Update Gemfile to use your private repository

gem 'my_private_repo', git: 'https://github.com/username/my_private_repo.git'
  1. Configure bundler with your OAuth token

bundle config github.com <your_github_oauth_token>

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

  1. Finally add BUNDLE_GITHUB__COM to your Heroku environment

$ heroku config:add BUNDLE_GITHUB__COM=<your_github_oauth_token>

You now have a private gem installed on Heroku!

Optional - configure bundler to use your local repo in development

bundle config has another option to work against your local git repository without updating your Gemfile The catch is that you need to specify the git branch when configuring the gem

gem 'my_private_repo', git: 'https://github.com/username/my_private_repo.git', branch: 'master'

Then run bundle config local.ecto </path/to/your/local/repo>

More information here - see the section "Local git repositories" at the end

@rydena021
Copy link

This was super helpful. Cheers!

@dineshvaitage
Copy link

This is awesome!!!

@i2chris
Copy link

i2chris commented Feb 10, 2023

When I run

bundle config github.com github_pat_123123kjlkj12lk3j1lk2asdasdas8m1BAL12nBXToAvcvjHplGSiyQ6xNL7asfasfsda

and then

bundle update

It asks me for the password for the repo. I've used the fine grained personal access token. Maybe I'm supposed to use something else @sebboh ?

@sebboh
Copy link
Author

sebboh commented Feb 11, 2023

@i2chris this gist is pretty old, so I don't know if this method still works.
the current bundle config docs suggest this:

export BUNDLE_GITHUB__COM=abcd0123generatedtoken:x-oauth-basic

(also, if that's the actual token you used, you should probably delete it and create a new one)

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