Orsay
Orsay

Reputation: 1130

How to use private repo gem in Rails app?

I would like to load a private gem in my rails app (I'm using docker and rails 5).

So first I add the gem in my gemfile :

gem 'my_gem', '0.1.4', git: "https://#{ENV['GITHUB_TOKEN']}@github.com/Orga/my_gem"

Then when I try to docker-compose build, this is what I get :

fatal: could not read Username for 'https://github.com': No such device or address

So I tried docker-compose run web bundle install. They ask me for my login. I've got no error, but the gemfile is updated as below :

GIT
  remote: https://@github.com/Orga/my_gem
  revision: 391ae38ff06dfd360eb42a09256f4a5463fba559
  specs:
    my_gem (0.1.4)
      rails (>= 5.0.0.beta3, < 5.1)
      roo (~> 2.1.0)

GEM
  remote: https://rubygems.org/
  ...
  1. This line seems really weird to me : remote: https://@github.com/Orga/my_gem . So is it the good way to load a private gem ?
  2. When I bundle install it asks for my login, so it will probably cause trouble in production right ?

EDIT

When I put direclty the token into the gemfile everything works. So the env var is not called properly.

I tried :

.env

GITHUB_TOKEN=...

gemfile

gem 'my_gem', '0.1.4', git: "https://#{ENV['GITHUB_TOKEN']}@github.com/Orga/my_gem"

AND also tried

secrets.yml

development:
  github_token: ...

gemfile

gem 'my_gem', '0.1.4', git: "https://#{Rails.application.secrets.github_token}@github.com/Orga/my_gem"

Upvotes: 2

Views: 1480

Answers (1)

Ben Toogood
Ben Toogood

Reputation: 479

The best and most secure way of doing this is using bundle configuration

In your Gemfile, you should have:

source 'https://gem.fury.io/youraccount/' do
  gem "your_service", "~> 1.0.0"
end

And then, if you need to authenticate, this can be achieved by running the following terminal command:

bundle config https://gem.fury.io/youraccount/ yourauthtoken

I hope this helps answer your question!

Ben

Upvotes: 2

Related Questions