Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cache for a rails project in Gitlab CI?

I've been trying to leverage Gitlab Ci's cache for my ruby on rails application but so far haven't had any luck, my application is using shared runners, maybe that's the issue?

Here are the contents of my .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

When my tests run I do see the runner checking for cached content, but it never restores it:

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

And when the build is about to finish I do see it trying to create the cache:

Creating cache rspec/master...
vendor/bundle: found 8917 matching files

Any ideas?

like image 330
Manuel Martinez Avatar asked May 12 '16 16:05

Manuel Martinez


People also ask

How does GitLab CI cache work?

all tiers. A cache is one or more files a job downloads and saves. Subsequent jobs that use the same cache don't have to download the files again, so they execute more quickly.

Where does GitLab runner store artifacts?

The artifacts are stored by default in /home/git/gitlab/shared/artifacts . Save the file and restart GitLab for the changes to take effect.


2 Answers

Gitlab Runner creates cache in git working directory by default. As you mentioned, your cache is created correctly, but it is stored in current git working directory of gitlab-runner. Whenever next build is run, gitlab-runner cleans the working directory (probably using git clean -dfx), which removes cache directory in git working directory.

You need to specify a separate cache directory for gitlab-runner. You can specify manually in /etc/gitlab-runner/config.toml file in the [[runners]] section using cache_dir setting key. Gitlab CI advanced configuration

You can also specify cache directory during gitlab runner registration using the --cache-dir option as in

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

Hope this helps

like image 134
meow Avatar answered Oct 16 '22 11:10

meow


The cache actually started working properly a few weeks after I posted this question, no changes to my configuration were required. I believe it had to do with an update gitlab rolled out.

like image 24
Manuel Martinez Avatar answered Oct 16 '22 11:10

Manuel Martinez