14

I have a rails app which requires a gem. I host this gem on bitbucket in a private repository.

In my Gemfile I added the gem like following:

gem "my-gem", :git => "git@bitbucket.org:my-username/my-gem.git", :branch => 'master'

I want to deploy my rails app on heroku with

git push heroku master

Now I always get following error

Fetching git@bitbucket.org:my-username/my-git-repo.git
Host key verification failed.
fatal: The remote end hung up unexpectedly

I understand the error, because the repository is set to private. But how can I solve this problem?

I already read this question: Deploying to Heroku using git on bitbucket, but I don´t really get the answer :)..

Community
  • 1
  • 1
Matthias
  • 4,355
  • 2
  • 25
  • 34

4 Answers4

9

Bitbucket allows for HTTP basic auth on repository URLs similar to github. Specify the URL for the gem as https://username:password@bitbucket.org/username/gemrepo.git.

It does mean having your username and password in your Gemfile, which itself is version controlled, and that's not a good practice, but on the other hand that's what Heroku recommends, so...

zelanix
  • 3,326
  • 1
  • 25
  • 35
Nitzan Shaked
  • 13,460
  • 5
  • 45
  • 54
  • 1
    I just tried it. I created a bitbucket account and a dummy repositry, and was able to clone using `git clone https://nitzanshaked:mypassword@bitbucket.org/nitzanshaked/test-repo.git`.I assume that if you use that as the URL for your gem it should work. Note that I used 'https://', not 'git+https://'. – Nitzan Shaked Aug 08 '13 at 16:22
  • Ok I tried it with "git" instead of "https". Thank you very much! Works perfect! But I don´t like to put the password into the Gemfile, but anyway, thanks for your reply. – Matthias Aug 12 '13 at 08:31
  • 4
    To avoid security issues associated with storing YOUR bitbucket password in Gemfile, just create a new user and grant it read access to that private gem repository. See [this post](http://www.itsudo.com/gems/2013/10/20/private-gems-on-bitbucket.html) for details. – misza222 Oct 20 '13 at 12:15
8

The proper way to achieve this is using bundle config, which saves the configuration on your home directory .bundle/config so it stays outside the repo.

bundle config bitbucket.org user:pwd

And then on Heroku you have to set a simple configuration in a special way:

heroku config:set BUNDLE_BITBUCKET__ORG=user:pwd

And in your Gemfile you just use the URL without the credentials.

gem 'gemname', git: "https://bitbucket.org/User/gemname.git"

Zequez
  • 3,399
  • 2
  • 31
  • 42
  • Thats really simple and clearest way to achieve what we need. Thanks! – Evgeniy B Jan 29 '17 at 21:21
  • This definitely looks like the way to go with Heroku deployments. Thanks! – Jared White Mar 26 '17 at 18:16
  • Finally, a working answer. The key is not to use the bundler "bitbucket:" method, which seems to only work on public repos. But I had to include the whole url in the config command: bundler config "https://bitbucket.org/User/mygem.git" user:password – JosephK Jan 09 '20 at 07:02
6

I had the same problem, but I ended up doing the following as a workaround to providing the Bitbucket password in the Gemfile.

The basic idea is to clone the gem from Bitbucket into a local directory, add it to your app and package it into vendor/cache so Heroku can use it. My exact steps are below:

  1. Clone your gem to a local directory:

    git clone git@bitbucket.org:me/my_private_gem.git /home/me/my_private_gem

  2. Add the gem to your Gemfile as a 'fake' Bitbucket repo:

    gem 'my_private_gem', :git => 'git@bitbucket.org:me/my_private_gem.git', :branch => 'master' # this repo will not be used

  3. Configure Bundler to work against the local repository (where you cloned the gem in step 1):

    bundle config local.my_private_gem /home/me/my_private_gem

  4. Run bundle install as usual, you should see something like this:

    Using my_private_gem (0.0.1) from git@bitbucket.org:me/my_private_gem.git (at /home/me/my_private_gem)

  5. Package all your gems into /vendor

    bundle package --all

  6. Add /vendor to your repo

    git add vendor && git commit -m 'add my_private_gem to /vendor/cache'

  7. Push to Heroku (don't forget to commit your updated Gemfile and Gemfile.lock first), you should see something like the following:

    Using my_private_gem (0.0.1) from git://github.com/my_private_gem/my_private_gem.git (at /tmp/build_19fmj3tup0zy2/vendor/cache/my_private_gem-8bc6f436e2c8)

References:

  • This worked for me. But after updating the gem locally, rather than doing `bundle install` to update, I needed to do `bundle install --local`. This is mentioned in the referenced [Bundler - package](http://bundler.io/v1.3/bundle_package.html). – Vic Oct 14 '15 at 22:59
2

I would suggest to use ENV vars instead of a new user like:

https://#{ENV['BITBUCKET_USER']}:#{ENV['BITBUCKET_PWD']}....

Then set them using:

heroku config:add BITBUCKET_X=value

For your development environment you can use the dotenv gem to define the credentials.

See also: How can I specify a gem to pull from a private github repository?

Community
  • 1
  • 1
Flori
  • 671
  • 7
  • 12