Local Gem Causes Heroku Push to Fail - ruby-on-rails

I have a Gem on my local machine that I declare in my Gem file like this:
group :assets do
gem 'my_gem', path: "/Users/me/path/to/my_gem"
end
This works great locally, but when I push to staging on Heroku, the build fails because the gem isn't available.
Currently I'm having to comment/uncomment this gem between deploys which is a real pain.
I've also tried adding it to my development group, but this doesn't help.
Why is Heroku looking for this gem?

Bundler always needs to resolve all of the gems in your Gemfile. You shouldn't commit a Gemfile that contains a local path.
Instead, push your gem to a git repository that is reachable from Heroku and point to that in your Gemfile.
For development, you can use a local path override: http://bundler.io/v1.3/git.html#local

you can try placing the gem in vendor/gems directory, create it if it doesn't exist.then in your Gemfile do like this:
gem 'rails_multisite', path: 'vendor/gems/rails_multisite'
and make sure you run bundle update so Heroku can Pickup the changes

Related

How can I ignore a gem source on Heroku?

I have a Rails app with some private gems used for testing locally; I do not need to access them in production, on Heroku. They are loaded in the gemfile as source block, e.g:
group :development, :test do
source "https://myprivaterepo" do
gem "mycustomgem", "~> 1.0"
end
end
When I try to deploy to Heroku, the build fails ('could not fetch specs from https://myprivaterepo') because Heroku cannot access the gem source. I have set a BUNDLE_WITHOUT config var to ignore development and test gems, but this still doesn't prevent the build trying to fetch gems from this source. How can I prevent this, so that Heroku just ignores these gems and their source completely?
Using Ruby 2.2.4, Rails 4.2, Heroku-16 stack.
try this: heroku config:set BUNDLE_WITHOUT="development:test"
or
gem 'mycustomgem', source: "https://myprivaterepo.com" if ENV['RAILS_ENV'] != 'production'

Specifying the same gem twice for development/test and production but different paths

Sometimes you make a gem that is specific to a project. This helps abstract and pull some of the "responsibility" out of the main Rails app and into a more modular place.
The gem would be located here on you application:
gem 'example_gem', path: './example_gem'
You bundle and everything is OK. Now, you git init the gem and store it in its own repo on github. You try doing this to keep it developer friendly:
group :development, :test do
gem 'example_gem', path: './example_gem'
end
group :production do
gem 'example_gem', github: 'company/example_gem'
end
You pat yourself on the back for increasing your workflow, but after running bundle you get:
Your Gemfile lists the gem example_gem (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
You cannot specify the same gem twice coming from different sources.
You specified that example_gem (>= 0) should come from source at ./example_gem and git://github.com/company/example_gem.git
The workflow here is to be able to edit the gem in development and when you're done, commit those changes and push them to Github. BUT, when on development, you don't want to have to do a git commit, git push, and a bundle update on your main app just to see a small change.
Does anyone know of a better way to solve this issue?
Yes, there is a better way: First, have the gem as a git gem in all environments
gem :example_gem, :git => 'git#github.com:foo/example_gem', :branch => :master #you need to set a branch
Then in your app's folder run
bundle config --local local.example_gem /path/to/gem
This will edit .bundle/config to set this option (make sure this file isn't checked into source control!) and tells bundler to get the gem from that path.
At the point when you are going to push your commits you have to be a little bit careful: if your app depends on not yet committed changes to the gem then clearly things will break. In addition as you commit to the repository for the example gem the rails app's Gemfile.lock will get updated. If you push a Gemfile.lock that references a commit that only exists in your copy of the example_gem repo then other users will be stuck.
this works for me.
foo = "https://github.com/me/my-development-version-gem.git"
group :production do
foo = "https://github.com/me/my-production-version-gem.git"
end
gem "foo", :git => foo
in my case i need different branchs for envs to engine

Local override for {gem} at /path/to/local/git/repo is using branch develop but Gemfile specifies develop

I'm running bundler v1.3.0.pre.2 and trying to get bundler to bundle a local git repository holding a gem, into a rails app.
Gemfile :
gem 'mygem', :github => 'myrepo/mygem', :branch => 'develop'
Config :
bundle config local.mygem /path/to/local/git/repo
Bundle install throws the error :
Local override for mygem at /path/to/local/git/repo is using branch
develop but Gemfile specifies develop
Is this a bug ? I see that the branch names are exactly the same. What could be wrong ? Got the same error for Bundler v1.2.3 as well
You can use a local gem and even the branch you are working on like this
gem 'rspec-rails', path: '~/forks/rspec-rails', branch: 'feature-branch'
Just change github to path, Then
bundle install
Also as far as bundler config goes, although it is in the docs, I have never needed to change the local config like you did above.
With this method I have never needed to remove Gemfile.lock, it just picks up the changes to your local repo.
Although I don't recommend it, you could disable the branch checking feature used by bundler to make sure you are using the correct branch when developing
bundle config disable_local_branch_check true
If you just need to point to a branch within a local repository you're using like a gem, in gemfile:
gem 'gem-name', :path=> 'relative/path/to/directory'
Then create bundler configuration for that "gem" before bundle-ing
(Keep in mind the repository name may be different than the gem's name, although this is not the norm).
bundle config local.repository-name relative/path/to/directory
Magically, whatever branch is currently checked out locally will be active when you start a local server. You'll likely need to restart your server if you need to make changes or change branches (unless you have some type of automatic reloading feature).
One gotcha is when a configuration setting is already established (let's say via the mechanism above), and you need to use/reference a remote git gem - you don't need bundler configuration to use a remote git gem (if you have an active configuration, you'll get the local override error).
Check to make sure you don't have local settings for given gem:
bundle config local.gem-name
If you do, remove configuration for that gem:
bundle config --delete local.gem-name
Then point to remote repo and branch, in gemfile:
gem 'gem-name', :path => 'https://github.com/reponame.git', :branch => 'branch_name'
More information:
Git Gems: http://bundler.io/v1.7/git.html
"bundle config": http://bundler.io/v1.7/man/bundle-config.1.html
This GitHub issue suggests that the workaround may be to remove Gemfile.lock and do bundle install again.
The branch you reference in your Gemfile needs to be the same as you have checked out in the local repository. Note that after you commit in the local repository you will need to run bundle install on your main application so that it rebuilds the Gemfile.lock to include the new revision hash and commit it. I recommend using the gem below as it automates this process for you and also aids in other scenarios. See the gem page for exact detail.
https://github.com/EPI-USE-Labs/git-bundle
Full detail of what happens:
When you use a git repository in your gemfile you can use bundle local overrides for development which will store revision hashes in your Gemfile.lock. On production these exact revision hashes will be checked out when it runs bundle install.
Gemfile:
gem 'example', git: 'https://github.com/your_name/example.git', branch: :master
Bundle config shell command:
bundle config local.example /path/to/local/git/repository
Gemfile.lock (auto generated):
GIT
remote: https://github.com/your_name/example.git
revision: b9270e61abb89e1ff77fb8cfacb463e4d04388ad
branch: master

Change in Gemfile isn't reflected in Gemfile.lock

I am suffering from the infamous SQLite3 Vs Heroku error while trying to deploy a simple Rails app.
Initially my Gemfile looked like
gem 'sqlite3'
...
After googling on the topic, I updated it to look like this:
group :development, :test do
gem 'sqlite3'
end
...
and then did a bundle install and surprisingly (at least for me),
$ git status --short
M Gemfile
The Gemfile.lock did NOT change!
Now heroku keeps giving this SQLite error again and again because
the Gemfile.lock is same as before and bundle install keeps failing
on their server!
Where am I going wrong?
Damn! It was a git issue.
I was working on a branch named 'deploy' and trying to push the 'master' branch and hence bundle install failed every time!
$git push heroku deploy:master
This made it work :)

How to view certain file inside gem on heroku

Is there any way to open file inside ruby gem that installed on heroku server ?
The gems will be the same as what you have defined in your Gemfile - that's the great thing about bundler..
bundle open gem_name

Resources