GitLab CI - Cache Not Working

I am currently using GitLab in conjunction with CI runners to run my project's unit tests to speed up the test bootstrapping process. I am using the built-in caching feature however it doesn't seem to work.

Every time someone makes a commitment, my runner executes git fetch

and continues to delete all cached files , which means I have to stare at the screen for about 10 minutes to wait for the test to complete while the runner re-downloads all dependencies (NPM and PIP are the biggest time killers).

Runner CI Output:

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 tried to read the following articles for reference, however none of them seem to solve my problem:

+9


source to share


1 answer


It turns out I did the wrong thing:

  • Your script cannot cache files outside of your project scope, creating a virtual environment and caching instead, which allows you to cache your pip modules.
  • Most importantly, your test must pass in order for it to cache files.

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

CI Passed results

Currently my configuration looks like this 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 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 led to the following conclusion:

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 regex:

^TOTAL\s+(?:\d+\s+){2}(\d{1,3}%)$

      

+6


source







All Articles