Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitLab CI - Cache not working

I'm currently using GitLab in combination with CI runners to run unit tests of my project, to speed up the process of bootstrapping the tests I'm using the built-in cache functionality, however this doesn't seem to work.

Each time someone commits to master, my runner does a git fetch and proceeds to remove all cached files, which means I have to stare at my screen for around 10 minutes to wait for a test to complete while the runner re-downloads all dependencies (NPM and PIP being the biggest time killers).

Output of the CI runner:

Fetching changes...

Removing bower_modules/jquery/  --+-- Shouldn't happen!
Removing bower_modules/tether/    |
Removing node_modules/            |
Removing vendor/                --'
HEAD is now at 7c513dd Update .gitlab-ci.yml

Currently my .gitlab-ci.yml

image: python:latest

services:
  - redis:latest
  - node:latest

cache:
  key: "$CI_BUILD_REF_NAME"
  untracked: true
  paths:
  - ~/.cache/pip/
  - vendor/
  - node_modules/
  - bower_components/


before_script:
  - python -V

  # Still gets executed even though node is listed as a service??
  - '(which nodejs && which npm) || (apt-get update -q && apt-get -o dir::cache::archives="vendor/apt/" install nodejs npm -yqq)'
  - npm install -g bower gulp

  # Following statements ignore cache!
  - pip install -r requirements.txt
  - npm install --only=dev
  - bower install --allow-root
  - gulp build

test:
  variables:
    DEBUG: "1"
  script:
  - python -m unittest myproject

I've tried reading the following articles for help however none of them seem to fix my problem:

  • http://docs.gitlab.com/ce/ci/yaml/README.html#cache
  • https://fleschenberg.net/gitlab-pip-cache/
  • https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/336
like image 688
Paradoxis Avatar asked Jun 28 '17 09:06

Paradoxis


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 is Gitlab CI cache stored?

By default, they are stored locally in the machine where the Runner is installed and depends on the type of the executor. Locally, stored under the gitlab-runner user's home directory: /home/gitlab-runner/cache/<user>/<project>/<cache-key>/cache.

How do I clear my CI cache?

In codeigniter, the cache is save in the folder with the path 'application/cache'. 1. Clearing all cache : You can clear the entire cache directory by calling $this->output->clear_all_cache();


1 Answers

Turns out that I was doing some things wrong:

  • Your script can't cache files outside of your project scope, creating a virtual environment instead and caching that allows you to cache your pip modules.
  • Most important of all: Your test must succeed in order for it to cache the files.

After using the following config I got a -3 minute time difference:

CI Passed results

Currently my configuration looks like follows and works for me.

# Official framework image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python
image: python:latest

# Pick zero or more services to be used on all builds.
# Only needed when using a docker container to run your tests in.
# Check out: http://docs.gitlab.com/ce/ci/docker/using_docker_images.html#what-is-service
services:
- mysql:latest
- redis:latest

cache:
    untracked: true
    key: "$CI_BUILD_REF_NAME"
    paths:
    - venv/
    - node_modules/
    - bower_components/


# This is a basic example for a gem or script which doesn't use
# services such as redis or postgres
before_script:

# Check python installation
- python -V

# Install NodeJS (Gulp & Bower)
# Default repository is outdated, this is the latest version
- 'curl -sL https://deb.nodesource.com/setup_8.x | bash -'
- apt-get install -y nodejs
- npm install -g bower gulp

# Install dependencie
- pip install -U pip setuptools
- pip install virtualenv


test:

    # Indicate to the framework that it's being unit tested
    variables:
        DEBUG: "1"

    # Test script
    script:

    # Set up virtual environment
    - virtualenv venv -ppython3
    - source venv/bin/activate
    - pip install coverage
    - pip install -r requirements.txt

    # Install NodeJS & Bower + Compile JS
    - npm install --only=dev
    - bower install --allow-root
    - gulp build

    # Run all unit tests
    - coverage run -m unittest project.tests
    - coverage report -m project/**/*.py

Which resulted in the following output:

Fetching changes...
Removing .coverage                              --+-- Don't worry about this
Removing bower_components/                        |
Removing node_modules/                            |
Removing venv/                                  --`
HEAD is now at 24e7618 Fix for issue #16
From https://git.example.com/repo
85f2f9b..42ba753  master     -> origin/master
Checking out 42ba7537 as master...
Skipping Git submodules setup
Checking cache for master...                    --+-- The files are back now :)
Successfully extracted cache                    --`

...

project/module/script.py                  157      9    94%   182, 231-244
---------------------------------------------------------------------------
TOTAL                                          1084    328    70%
Creating cache master...
Created cache
Uploading artifacts...
venv/: found 9859 matching files                   
node_modules/: found 7070 matching files           
bower_components/: found 982 matching files 
Trying to load /builds/repo.tmp/CI_SERVER_TLS_CA_FILE ... 
Dialing: tcp git.example.com:443 ...         
Uploading artifacts to coordinator... ok            id=127 responseStatus=201 Created token=XXXXXX
Job succeeded

For the coverage report, I used the following regular expression:

^TOTAL\s+(?:\d+\s+){2}(\d{1,3}%)$
like image 157
Paradoxis Avatar answered Sep 22 '22 08:09

Paradoxis