Bundler: always use latest revision of git branch in Gemfile - ruby

I have a Gemfile with a private git repo in the following format:
gem 'magic_beans', :git => "git#git.example.com:magic_beans.git', :branch => 'super_beans'
When I bundle install, the Gemfile.lock locks it to a specific SHA revision.
Can I get bundler to always check and use the latest SHA commit and/or update the Gemfile.lock? Notice that when I push updates to the super_beans branch I am not modifying the gem version.
Ideally, every time I run bundle it would check upstream git repo for a newer SHA revision of the branch.

This isn't how bundler works.
The point is to allow seamless versioning of dependencies.
(particularly so you know exactly what version of the code is deployed at any given time).
If want the latest version, you should just run.
bundle update magic_beans
This is exactly the same functionality as if you just say
gem "rails"
I'd suggest though, if you have a range of specific things you want to update
then add a custom binary (say an executable file named bundle_update)
#!/usr/bin/env bash
bundle install
bundle update magic_beans
Then just do a ./bundle_update when you want to update these things.

You can run bundle update to update all or specific gems to their latest available version, as stated in the docs.
Would that help?

After searching through the documents I finally found the magic way to do this:
bundle update magic_beans --source magic_beans
That is to update the magic_beans gem only, but not to touch other locked gems. The doc about this is: http://bundler.io/man/bundle-update.1.html

delete .gemlock is what worked for me :/

Related

Getting a gem from a repository with a specific version

I'm trying to get an old version from a repository using bundler.
For instance, in my Gemfile I have:
...
gem "custom-metrics", "~> 0.14.0", git: "https://gitlab.custom.co/gems/custom-metrics.git"
...
This is properly installing the version 0.14.0. Then I pushed a 0.14.1 version in the dependency repository, and it's still the same, as expected.
In the dependency, I'm setting the version in my .gemspec file as:
Gem::Specification.new do |spec|
spec.name = "custom-metrics"
spec.version = 0.14.0
...
end
If I now push a 0.15.0 version, it would stop working. In this case, I have to specify the ref: to the previous commit. So it seems it's not that bundler can't install the version, but that it only looks for versions in the last MINOR version (in this case, it would be in 0.15.*)
I couldn't find any doc that confirms my hypothesis.
> Am I missing something?
> Would it be possible to specify a version and be sure that bundler will always find it?
When loading a gem directly from a git repository, then Bundler loads exactly the version that it find in the defined branch, at the ref or tag.
When you define that a specific version of the gem should be used, then Bundler will check if the version of gem found in the repository matches that condition.
Therefore, you likely do not want to simply install the latest available version of a gem directly from a main branch from GitHub. But instead defining to install from a specific branch, tag, or ref might be easier to manage.
How to install gems from git repositories from the Bundler docs.

How to force bundler to re-fetch tag ref from GitHub

I have a gem that I manage, and have recently updated the ref for a tag (the SHA for the tag is now different on GitHub)
In my Gemfile, I have:
gem 'my_gem', tag: '0.25.0', git: 'http://github.com/Example/my_gem.git'
However, running $ bundle install only uses the old SHA for that tag. How can I force it to get the latest code?
I tried removing the path to my gem, like so:
$ rm -rf /usr/local/rvm/gems/ruby-2.4.5/bundler/gems/my_gem-*
... but bundler just downloads it again.
The only way I've been able to make this happen is by modifying the revision in my Gemfile.lock, but that only works on that one repo. I have multiple repos using this gem. It seems like Bundler has a database/cache mapping gem tags to revisions, but I can't find where that is.
PS: I know re-tagging things isn't best practice.

Gem from custom git repository is not available

I tried to modify an existing gem and forked the git repository.
I added some commits and pointed bundler to my GitHub repository.
bundler update does recognise the change and downloads my version of the gem.
Anyway when I try to launch the application which depends on that gem (testkitchen) my changes aren't available. And when I delete the official version of the gem my version is not found/used and the app fails.
The weird thing is that all the official gems are installed to /var/lib/gems and my version goes to ~/.bundler
gem environment also shows up the correct directories
- GEM PATHS:
- /var/lib/gems/2.2.0
- /home/ansible/.bundler/ruby/2.2.0
I'm not using rvm or similar. Am I doing something wrong?
As #matt pointed out I forgot to add bundle exec to my commands.

Ruby: How to Install a Plugin in a Git Repository

I'm not new to programming, but brand new to Ruby. Everything's working, but I'm still missing a key concept: how do you install a plugin and where/how do you include it in an app?
Example:
I'm trying to use the Facebooker2 plugin: https://github.com/mmangino/facebooker2. In the readme, step 1 is to "Install facebooker2 as a plugin in your rails app." I've run the command git clone https://github.com/mmangino/facebooker2.git to download a read only version of the repository.
Do I then bundle that up using Bundler, or do I need to create a gem file in some way? Do I simply
use gem to install it, or do I need to compile it into a gem?
Any help (terminal commands or otherwise) are extremely helpful.
I looked at the repo and it's set up as a gem. You can simply add
gem 'facebooker2'
to your Gemfile (in the root of your project) and run
bundle install
to download it and add it to your list of installed gems, both in development and in production.
Rails used to include the concept of plugins (added to your /vendor/plugins directory) but that's been dropped in favor of gems.
If you're source is source 'https://rubygems.org' but the gem you need is specific to github and not part of the rubygems.org library, then you can add the git method to your gemfile. You can also select a specific branch version. For example, here I have the gem cancan being pulled from the github repository on the 2.0 branch.
gem "cancan", :git => "git://github.com/ryanb/cancan.git", :branch => "2.0"

Bundler: How to check if Gems are up to date

Using Bundler, is there a way to check if there are newer gems available than the versions specified in the Gemfile? In other words, is there a way to check if my gems are up to date without actually updating?
List installed gems with newer versions available
$ bundle outdated [GEM] [--local] [--pre] [--source]
Options:
--local: Do not attempt to fetch gems remotely and use the gem cache instead
--pre: Check for newer pre-release gems
--source: Check against a specific source
Source: http://bundler.io/v1.3/bundle_outdated.html
If you mean just see if an update is necessary (w/o actually updating), you might have to specify in your Gemfile to use some repo source for comparison, like:
gem "rails", :git => "git://github.com/rails/rails.git"
or specify a specific version to compare against with :version => ...
Then run bundle check and it'll list all gems that are out of date.
EDIT - I guess it depends on what's meant by up to date.

Resources