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

时间:2016-05-12 16:03:59

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

我一直试图在我的ruby on rails应用程序上利用Gitlab Ci的缓存,但到目前为止还没有运气,我的应用程序正在使用共享的运行程序,也许那就是问题?

以下是我.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

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

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

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

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

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

希望这有帮助

答案 1 :(得分:1)

在发布此问题几周后,缓存实际上已开始正常运行,不需要更改我的配置。我认为这与推出的更新gitlab有关。