gitlab 8.2.1,如何在.gitlab-ci.yml中使用缓存 [英] gitlab 8.2.1, How to use cache in .gitlab-ci.yml

查看:327
本文介绍了gitlab 8.2.1,如何在.gitlab-ci.yml中使用缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在.gitlab-ci.yml中使用缓存"(

I'm trying to use 'cache' in .gitlab-ci.yml (http://doc.gitlab.com/ce/ci/yaml/README.html#cache). My gitlab version is 8.2.1 and my Runner is:

$ docker exec -it gitlab-runner gitlab-runner -v 
gitlab-runner version 0.7.2 (998cf5d)

因此,根据文档,所有内容都是最新的,但是我无法使用缓存;-(.我的所有文件始终被删除.我在做错什么吗?

So according to the doc, everything is up to date, but I'm unable to use the cache ;-(. All my files are always deleted. Am I doing something wrong?

创建了一个缓存存档,但未传递给下一个作业.

A cache archive is created, but not passed to the next jobs.

$ cat .gitlab-ci.yml
    stages:
    - createcache
    - testcache

    createcache:
      type: createcache
      cache:
        untracked: true
        paths:
          - doc/
      script:
        - touch doc/cache.txt

    testcache:
      type: testcache
      cache:
        untracked: true
        paths:
          - doc/
      script:
        - find .
        - ls doc/cache.txt

作业"createcache"的输出

Running on runner-141d90d4-project-2-concurrent-0 via 849d416b5994...
Fetching changes...
HEAD is now at 2ffbadb MUST BE REVERTED
[...]
$ touch doc/cache.txt
[...]
Archiving cache...
INFO[0000] Creating archive cache.tgz ...              
INFO[0000] Done!                                        

Build succeeded.

作业"testcache"的输出

Running on runner-141d90d4-project-2-concurrent-0 via 849d416b5994...
Fetching changes...
Removing doc/cache.txt
[...]
$ ls doc/cache.txt
ls: cannot access doc/cache.txt: No such file or directory

ERROR: Build failed with: exit code 1

我的解决方法

我的解决方法是手动解压缩/cache目录中的内容...我很确定这不是使用缓存的正确方法...

My workaround

My workaround is to manually untar what's in the /cache directory ... I'm pretty sure that's not the correct way to use cache ...

$ cat .gitlab-ci.yml
    stages:
    - build
    - test
    - deploy

    image: ubuntu:latest

    before_script:
      - export CACHE_FILE=`echo ${CI_PROJECT_DIR}/createcache/${CI_BUILD_REF_NAME}/cache.tgz | sed -e "s|/builds|/cache|"`

    createcache:
      type: build
      cache:
        untracked: true
        paths:
          - doc/
      script:
        - find . | grep -v ".git"
        - mkdir -p doc
        - touch doc/cache.txt

    testcache:
      type: test
      script:
        - env
        - find . | grep -v ".git"
        - tar xvzf ${CACHE_FILE}
        - ls doc/cache.txt

推荐答案

8.2仅支持按作业缓存,而8.3将引入组"缓存,根据

8.2 only supports per-job cache, and 8.3 will introduce "group" cache that serves among jobs according to @ayufan's comment in Possibility to cache folders in build directory (#97).

但是,尽管我不能百分百确定,但是通过快速挖掘gitlab-ci-muti-runner的源代码,docker executor似乎无法使用缓存功能.由于将在每个作业中创建和销毁一个全新的容器,因此下一个构建中将不再存在cache.tgz存档.

勘误表:

上面的描述不正确,原因是在错误配置的环境中进行了测试.默认情况下,gitlab-ci-multi-runner创建一个专用的数据卷容器作为每个并发构建的缓存存储.缓存容器已安装到应用程序容器中的目录/cache,默认情况下,那些cache.tgz压缩文件位于/cache下.因此,缓存实际上可以在独立的内部版本之间重用.

The above description is incorrect due to testing in a misconfigured environment. By default, gitlab-ci-multi-runner creates a dedicated data volume container as a cache storage for each concurrent build. The cache container is mounted to directory /cache in the application container and those cache.tgz tarballs are placed under /cache by default. So caches are actually reusable among independent builds.

更新2015/12/11:

只需发现 gitlab- Runner @ 7dc9524f6ef0144b3797fc07c9035f38a8ad0512 ,可能尚未发布和记录.您可以使用

Just found out that "group" cache has already been implemented in gitlab-runner@7dc9524f6ef0144b3797fc07c9035f38a8ad0512, maybe not yet released and documented. You can enable it with

cache:
  paths:
    - doc/
  group: sharedcache

结果是将一个缓存tarball放置在路径<namespace>/<repo>/sharedcache/下,而不是将两个缓存tarball分别放置在路径<namespace>/<repo>/createcache/<namespace>/<repo>/testcache/下.

The result is one cache tarball being placed under path <namespace>/<repo>/sharedcache/ instead of two cache tarballs being placed separately under path <namespace>/<repo>/createcache/ and <namespace>/<repo>/testcache/.

更新2017/12/04:

组"缓存已由cache:key代替.使用key键可以在作业或git引用之间共享缓存.默认情况下,缓存在所有作业之间共享.因此,只需编写以下代码即可完成工作

"group" cache has been replaced by cache:key. Use the key key to make the cache share between jobs or git references. By default, a cache is shared between all jobs. So, simply write the following would do the job

cache:
  paths:
    - doc/

结帐 GitLab CI缓存:密钥 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆