On a recent Ruby on Rails project deployed on Heroku, I needed to require a privately hosted gem on Gitlab. Without the proper authentication with Gitlab, this will result in the following errors trying to push to Heroku.
remote: fatal: could not read Username for 'https://gitlab.com': No such device or address
remote:
remote: Git error: command `git clone 'https://gitlab.com/YOUR_ACCOUNT/YOUR_GEM.git'
To get this working, we need to provide authentication details to Bundler. We'll do this using Gitlab's Personal Access Tokens.
Create a Personal Access Token on Gitlab
First you need to navigate to https://gitlab.com/profile/personal_access_tokens and create a token with read_repository access.
You will see this token once, so be sure to save it somewhere secure.
Configure Bundler to use your Token
You could add the following in your Gemfile:
gem 'YOUR_GEM', git: "https://oauth2:YOUR_TOKEN@gitlab.com/YOUR_ACCOUNT/YOUR_GEM.git"
However, that would be exposing your access token in your main project's Git repository. Instead, you can set a bundler config variable and leave the entry in your Gemfile as:
gem 'YOUR_GEM', git: "https://gitlab.com/YOUR_ACCOUNT/YOUR_GEM.git"
Local Config
To do it locally in your project:
$ bundle config --local gitlab.com oauth2:YOUR_TOKEN
This will set a variable in .bundle/config that bundler will use to authenticate with Gitlab.
BUNDLE_GITLAB__COM: "oauth2:YOUR_TOKEN"
Make sure that the .bundle directory is added to your .gitignore file so this isn't stored in your repo.
Heroku Config
Heroku supports the above bundler configuration feature as environment variables. Simply add your Gitlab token using the following command (note: this will restart your app!):
heroku config:add BUNDLE_GITLAB__COM=oauth2:YOUR_TOKEN
Deploy to Heroku
Now that it's all setup, deploy away!
git push heroku master

Top comments (0)