简体   繁体   English

捆绑器:获取部分git repo?

[英]bundler: fetch partial git repo?

I have a Rails project that contains a my_gem folder at root: 我有一个Rails项目,其根目录包含my_gem文件夹:

my_repo
├── app
├── config
├── Gemfile
├── Gemfile.lock
├── db
├── lib
├── log
├── my_gem <-- the gem folder
├── public
├── Rakefile
├── README.md
├── script
├── test
├── tmp
└── vendor

I use this gem in another project by adding these lines to it's Gemfile: 我通过在Gemfile中添加以下行,在另一个项目中使用了这个gem:

git 'git@gitlab.mydomain.com:qosenergy/my_repo.git', :branch => 'master' do
  gem 'my_gem'
end

It works perfectly except that all the repo is fetched, that represents hundreds of megabytes (vs less than 1Mo for the only my_gem folder) and this is problematic when I build docker images from the repo that uses the gem: because more than 60% of the image size is filled by the full rails app contained in my_repo . 它可以正常工作,除了获取所有存储库(代表数百兆字节(相对于唯一的my_gem文件夹小于1Mo)),并且当我从使用gem的存储库构建docker映像时,这是有问题的:因为超过60%图像大小由my_repo包含的全轨应用程序my_repo

How could I keep the advantage of this behavior but keep only the my_gem folder instead of the complete repo ? 我如何才能保留这种行为的优势,而仅保留my_gem文件夹而不是完整的存储库?

According to the bundler documentation it seems that you can fetch one or more gems inside a given repository. 根据捆绑程序文档 ,您似乎可以在给定存储库中获取一个或多个gem。

Eg The Rails repo contains several gems: actioncable, actionmailer, actionpack ... 例如, Rails回购包含几个宝石:actioncable,actionmailer,actionpack ...

Specify that a git repository containing multiple .gemspec files should be treated as a gem source 指定包含多个.gemspec文件的git存储库应被视为gem源

 git 'https://github.com/rails/rails.git' do gem 'railties' gem 'action_pack' gem 'active_model' end 

If your my_gem contained in the project has a given .gemspec , you should be able to install it without downloading the full application where it is contained. 如果您my_gem包含的项目有一个给定的.gemspec ,你应该能够不用下载它包含完整的应用程序进行安装。 If you have done it this way, you might consider other options. 如果以这种方式完成此操作,则可以考虑其他选择。

Git sparse checkout Git稀疏签出

Git provides the feature of sparse checkout for cloning only a given path inside the repo. Git提供了稀疏签出的功能,用于仅克隆存储库中的给定路径。 You can read this blog for a few use cases: 您可以阅读此博客的一些用例:

$ mkdir pcl-examples
$ cd pcl-examples                               #make a directory we want to copy folders to
$ git init                                      #initialize the empty local repo
$ git remote add origin -f https://github.com/PointCloudLibrary/pcl.git     #add the remote origin
$ git config core.sparsecheckout true           #very crucial. this is where we tell git we are checking out specifics
$ echo "examples/*" >> .git/info/sparse-checkout" #recursively checkout examples folder
$ git pull --depth=2 origin master          #go only 2 depths down the examples directory

This answer is probably making this concept even clearer but I did not figure out how to take advantage of it inside a Gemfile . 这个答案可能使这个概念更加清晰,但是我没有弄清楚如何在Gemfile利用它。

Other debugging 其他调试

Since you are building a Docker image, you can refer to this checklist about rails errors. 由于您正在构建Docker映像,因此可以参考此清单以了解Rails错误。 However it seems that you are not experiencing any error, but only an image size issue. 但是,看来您没有遇到任何错误,而只是图像大小问题。

A couple of clean solution 几个干净的解决方案

I guess the cleanest solution to your issue would be to extract the gem code inside a new repository and install it as you were trying to do. 我想最干净的解决方案是将gem代码提取到新存储库中,并按照您的尝试进行安装。 I would personally go for this solution. 我个人会寻求这种解决方案。

If you really don't want to take it out, you could give a try to submodules inside your original project. 如果您真的不想删除它,可以尝试一下原始项目中的子模块 This approach can be very tricky and even Github is not really encouraging it. 这种方法可能非常棘手,甚至Github也不是很鼓励。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM