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

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

问题描述

我正在尝试在 .gitlab-ci.yml 中使用缓存"(http://doc.gitlab.com/ce/ci/yaml/README.html#cache).我的 gitlab 版本是 8.2.1,我的 Runner 是:

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 只支持 per-job 缓存,8.3 将根据 @ayufan 在构建目录中缓存文件夹的可能性(#97)中的评论.

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).

然而,虽然我不能 100% 确定,但通过快速挖掘 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

结果是一个缓存压缩包被放置在路径 <namespace>/ 下,而不是两个缓存压缩包分别放置在路径 <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 月 4 日更新:

组"缓存已被 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 缓存:密钥gitlab-runner@d34f76f86a7c2fc4d399e9922175bcc614434016了解更多信息.

Checkout GitLab CI cache:key and gitlab-runner@d34f76f86a7c2fc4d399e9922175bcc614434016 for more information.

这篇关于gitlab 8.2.1,如何在 .gitlab-ci.yml 中使用缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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