我一直在尝试将 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/