GitLab CI - 缓存未生效

17
我目前正在使用GitLab与CI运行程序来运行我的项目的单元测试,为了加快测试启动过程,我使用了内置的缓存功能,但是这似乎不起作用。
每当有人提交到主分支时,我的运行程序会进行git fetch,然后继续删除所有缓存文件,这意味着我必须盯着屏幕等待大约10分钟才能完成测试,同时运行程序重新下载所有依赖项(NPM和PIP是最耗时间的)。
CI运行程序的输出:
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

目前我的.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

我尝试阅读以下文章以获取帮助,但似乎没有一个可以解决我的问题:


3
你好。首先,你不能缓存 ~/.cache/pip/,缓存只允许用于项目文件。 其次,如果你指定了 untracked: true,那么 `paths:
  • vendor/
  • node_modules/
  • bower_components/` 是无用的,因为这些文件夹实际上是未跟踪的。 第三点:是的,我有同样的问题。有任何新消息吗?
- Etienne Gautier
@etienne,原来你需要在项目构件中指定目录,只有当测试成功后才会缓存。我马上会在我的问题中添加一个答案 :) - Paradoxis
1个回答

13

原来我做了一些错误的事情:

  • 你的脚本不能缓存项目范围之外的文件,创建一个虚拟环境并缓存它可以允许你缓存pip模块。
  • 最重要的是:你的测试必须成功才能缓存文件。

使用以下配置后,我得到了-3分钟的时间差异:

CI Passed results

目前我的配置如下,并且对我有效。

# 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

这导致了以下输出:

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

关于覆盖率报告,我使用了以下正则表达式:

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

11
我认为构件与缓存无关。如果这里的构件功能有效,我认为您的缓存无法正常工作。让我解释一下,构件是从运行程序上传到Gitlab的,我不想要这种行为,因为我正在使用一个依赖很多的Symfony项目,它在上传和下载时需要花费约20分钟的时间。因此,使用构件无法解决问题:缓存无效。 - Etienne Gautier
对我来说,问题在于没有意识到缓存只会在成功运行时发生。感谢您的评论。 - kevinlinxc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接