Deploy a Ruby gem local without using git or inter

2019-08-08 20:50发布

Is there a way to distribute a Ruby gem without access to the internet, just bundling the gem's that are installed on the dev's pc ?

This is for deploying inside our organisation, access to internet is heavily restricted. All the pc's run Windows7 with Ruby1.9.3 and Bundler 1.11.2

I have little experience with git or bundler. Rails is not used but some other gems like activerecord and logger are.

What I tried: I created a folder with my rb. scripts and a .gemspec file with the required gem's, then I executed bundle gem name_of_the_gem_i_want_to_make but got an error Errno::ENOENT: No such file or directory - git config user.name. In the past I created a git account and did some experimenting bu I'd prefer not to use it.

In the past I experimented with jRuby and Warble to produce JAR's but in this case I'd like to stick with MRI Ruby.

I have internet connection on my dev pc but some of the pc's or servers I want to deploy the scripts - and more important the gem's they depend on - don't. Ruby is allready installed everywhere though.

I'd like a simple way to gather all the needed files from my dev pc and transfer these to the target pc's. I could zip my Ruby folder and extract it elsewhere but then I have a lot of files I don't need.

Can you give me the commands I need to use or give me a site that explains how to do this ? I read a lot about bundler online but have nowhere found this way of working. Can't imagine other devs haven't the same issues and a common solution is present.

EDIT:

I managed to create a gem by executing gem build mygem.gemspec but it has only my own scripts in it, no required gem's.

EDIT: followed the suggestion of Mihai, removed the previous attempts.

Made a subfolder vendor/gems and copied the 2 gemfiles there.

Now I can do a bundle package and a bundle install --no-deployment with the following Gemfile.

gem 'active_record', '4.1.1', :path => 'vendor/gems'
gem 'logger', '1.2.8', :path => 'vendor/gems'

Question now: how to deploy this onto another pc ? Copy everything in the folder and on the target pc do a bundle install --local ? Or is there a way to bundle everything in a gem so that on the target pc I can do gem install mygem --local ? A gem build mygem doesn't include anything from the bundle.

2条回答
Fickle 薄情
2楼-- · 2019-08-08 21:12

You can do this in two ways.

a) Cache (package) the gems in your project's vendor/cache folder

bundle package

This will generate/update Gemfile.lock and copy all the gems locally inside your project's vendor/cache folder. Then you can copy your project files to your server and run

bundle install --local # translates to pick my gems from vendor/cache

b) Use a custom path, usually located outside your project

bundle install supports local paths

gem install --local path_to_my_gem/my_gem.gem

The --local flag is optional but it's useful in this case as it skips the lookup in the usual remote repositories.

Subsequently you can specify the path in the Gemfile as well

gem "my_gem", path: "path_to_my_gem/my_gem.gem"
查看更多
劫难
3楼-- · 2019-08-08 21:18

Mihai's solution combined with the local gem folder and the --no-deployment option works to some point but there are a few problems: - on the target pc you need to do a gem install bundler first using a local copied gem, then a bundle install --local of the package you made and copied earlier - for the gems ruby-oci8 and activerecord-oracle_enhanced-adapter my solution for the other gems didn't work for some reason (something with the mingw32 platform)

Since the work about the creating and afterward installing of the package and the problems still resting I decided -for the moment- to resort to the good old batch approach and made following batch script.

This is windows only -obviously- but I asume this can easily be adapter for other OS.

The gems are installed from the local copies, no internet connection necessary, the dependencies of activerecord also, no need to specify them but the gems need to be present.

The devkit is necessary for the i18n and json gems, both dependensies of activerecord.

Here a list of the gemfiles I needed, it depends on the versions you use on your dev pc.

activemodel-4.1.1.gem
activerecord-4.1.1.gem
activerecord-oracle_enhanced-adapter-1.5.6.gem
activesupport-4.1.1.gem
arel-5.0.1.20140414130214.gem
builder-3.1.4.gem
bundler-1.11.2.gem
i18n-0.7.0.gem
json-1.8.2.gem
logger-1.2.8.gem
minitest-5.3.4.gem
ruby-oci8-2.1.2-x86-mingw32.gem
thread_safe-0.3.4.gem
tzinfo-1.2.2.gem

You can find them under C:\Rubyxxx\lib\ruby\gems\1.9.1\cache

Here the batch

@ECHO OFF
rem in case started as administrator in folder \windows\system32
pushd "%~dp0"

rem check if ruby is installed, if not, start installation
if not exist c:\ruby193 (
  echo Install Ruby 1.9.3 to c:\ruby193
  start /w \\myshare\ruby\rubyinstaller-1.9.3-p551.exe
)

rem check if devkit is installed, if not, copy folder local
if not exist c:\devkit (
  echo Copying devkit..
  md c:\devkit
  xcopy /s \\myshare\devkit c:\devkit
)

echo Adding the DevKit to PATH
set path=c:\devkit\bin;c:\devkit\mingw\bin;%PATH%
rem gems is a folder containing all the gemfiles needed
cd gems
call gem install logger-1.2.8.gem --no-rdoc --no-ri --local
call gem install activerecord-4.1.1.gem --no-rdoc --no-ri --local
call gem install ruby-oci8-2.1.2-x86-mingw32.gem --no-rdoc --no-ri --local
call gem install activerecord-oracle_enhanced-adapter-1.5.6.gem --no-rdoc --no-ri --local
cd ..

I hope someone posts a solution for the bundler approach (or better) though.

查看更多
登录 后发表回答