ruby-on-rails - 如何在 Gitlab CI 中为 rails 项目使用缓存?

标签 ruby-on-rails bundler gitlab gitlab-ci

我一直在尝试将 Gitlab Ci 的缓存用于我的 ruby​​ on rails 应用程序,但到目前为止还没有任何运气,我的应用程序正在使用共享运行程序,也许这就是问题所在?

以下是我的.gitlab-ci.yml的内容

services:
  - postgres:latest

rspec:
  stage: test
  script:
    - apt-get update -qy
    - apt-get install -y nodejs
    - gem install bundler
    - bundle check --path vendor/bundle || bundle install --path vendor/bundle --jobs $(nproc)
    - cp config/database.gitlab-ci.yml config/database.yml
    - RAILS_ENV=test bundle exec rake db:create db:schema:load
    - bundle exec rspec
  cache:
    paths:
      - vendor/bundle
  tags:
    - ruby
    - postgres

当我的测试运行时,我确实看到运行器检查缓存的内容,但它永远不会恢复它:
gitlab-ci-multi-runner 1.1.3 (a470667)
Using Docker executor with image ruby:2.1 ...
Pulling docker image postgres:latest ...
Starting service postgres:latest ...
Waiting for services to be up and running...
Pulling docker image ruby:2.1 ...

Running on runner-8a2f473d-project-1129003-concurrent-0 via runner-8a2f473d-machine-1462982763-a9a70bd7-digital-ocean-4gb...
Cloning repository...
Cloning into '/builds/foo/bar'...
Checking out 30ea1b5f as master...
Note: checking out '30ea1b5f036808f7e27bfa32e939c1f591343ba6'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 30ea1b5... Fix width of tables contained in table-scroll divs
Checking cache for rspec/master...

$ apt-get update -qy

当构建即将完成时,我确实看到它正在尝试创建缓存:
Creating cache rspec/master...
vendor/bundle: found 8917 matching files

有任何想法吗?

最佳答案

Gitlab Runner 默认在 git 工作目录中创建缓存。正如您所提到的,您的缓存已正确创建,但它存储在 gitlab-runner 的当前 git 工作目录中。每当下一次构建运行时,gitlab-runner 都会清理工作目录(可能使用 git clean -dfx),这会删​​除 git 工作目录中的缓存目录。

您需要为 gitlab-runner 指定一个单独的缓存目录。您可以在 [[runners]] 中的/etc/gitlab-runner/config.toml 文件中手动指定部分使用 cache_dir设置键。 Gitlab CI advanced configuration

您还可以在 gitlab runner 注册期间使用 --cache-dir 选项指定缓存目录,如

gitlab-runner register --name blabblah --cache-dir /var/opt/gitlab/gitlab-runner-cache

希望这可以帮助

关于ruby-on-rails - 如何在 Gitlab CI 中为 rails 项目使用缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37192133/

相关文章:

ruby-on-rails - 如何在一天结束时将 boolean 值重置为 "default: false"?

ruby-on-rails - 在 rails 中向 link_to 添加方法和 html 选项?

git - 从现有的本地分支推送到新的在线存储库

ruby-on-rails - Carrierwave如何获取文件扩展名

ruby-on-rails - Ruby - Oci8 - 行类型调用函数不起作用

rubygems - 为什么运行 "Could not find rubygems-bundler"会出现错误 'bundle package'

ruby - 在 Heroku 上部署,如何指定用于 bundler 的 ruby​​ 版本?

ruby - 为什么需要 "require ' bundler/setup'”?

gitlab - 如何在GitLab Wiki中创建目录?

docker - 如何在GitLab CI作业中运行没有外壳的容器