首页 文章

Gitlab CI运行器配置与docker上的缓存

提问于
浏览
9

我似乎无法在gitlab CI中的作业之间进行缓存或工件 . 我怀疑这与我的配置有关,但我不确定是什么 . 我正在使用以下docker-compose配置在docker中运行gitlab和gitlab-ci-multirunner . 为简洁起见,我省略了数据库配置和一些环境变量:

version: '2'

services:
  gitlab:
    image: sameersbn/gitlab:8.5.1
    links:
      - redis:redisio
      - postgresql:postgresql
    ports:
      - "10080:80"
      - "10022:22"
    environment:
      ...
    volumes:
      - gitlab_data:/home/git/data

  gitlab-ci-runner:
    restart: always
    image: gitlab/gitlab-runner
    volumes:
      - gitlab_runner_config_data:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker.sock
      - /etc/nginx/ssl/gitlab.crt:/etc/gitlab-runner/certs/ca.crt
      - /etc/ssh:/ssh
    links:
      - gitlab:gitlab

  redis:
    ...
  postgresql:
    ...


volumes:
  postgresql_data:
  redis_data:
  gitlab_data:
  gitlab_runner_config_data:

跑步者配置( config.toml )是:

concurrent = 1

[[runners]]
  name = "docker"
  url = <public gitlab url>/ci 
  token = <gitlab token>
  tls-ca-file = "/etc/gitlab-runner/certs/ca.crt"
  executor = "docker"
  [runners.docker]
    image = "docker-bash"
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]

引用的 docker-bash 图像只是安装了bash的官方docker:1.10图像 .

我的构建过程包括3个步骤:

  • 运行 npm install 和官方node:5图像中的测试 . 现在,我已经离开了这一步,以测试部署 .

  • 构建包含代码的docker镜像

  • 使用ansible,通过客户构建的ansible docker镜像将构建的映像部署到 生产环境 服务器 .

.gitlab-ci.yml 文件如下所示:

variables:
  FULL_IMAGE_TAG: deploy-$CI_BUILD_REF_NAME:$CI_BUILD_ID-$CI_BUILD_REF
  IMAGE_FILE: deploy-$CI_BUILD_REF_NAME.tar.gz

cache:
  paths:
    - $IMAGE_FILE

build:
  stage: build
  script:
    - docker build -t $FULL_IMAGE_TAG .
    - docker save $FULL_IMAGE_TAG | gzip -cf - > $IMAGE_FILE
  artifacts:
    paths:
      - $IMAGE_FILE

deploy:
  stage: deploy
  image: ansible-ssh
  script:
    - ls
    - ansible-playbook -e image_file=$IMAGE_FILE -e branch=$CI_BUILD_REF_NAME -e full_image_name=$FULL_IMAGE_TAG deploy-playbook.yml
  only:
    - develop
    - master

如您所见,压缩的docker镜像在缓存和工件部分中都被引用,但在部署步骤中实际上不可用,其中ansible应该将其复制到远程机器 . 我试过包含一个 ls 命令,所以检查文件夹内容,文件显然不存在,但它是绝对构建的,我可以从gitlab UI下载它 . 以下是部署作业的日志:

gitlab-ci-multi-runner 1.0.4 (014aa8c)
Using Docker executor with image ansible-ssh ...
Pulling docker image ansible-ssh ...
WARNING: Cannot pull the latest version of image ansible-ssh : Error: image library/ansible-ssh not found
WARNING: Locally found image will be used instead.

Running on runner-59d43cf3-project-8-concurrent-0 via 381c2ea97744...
Fetching changes...
Removing artifacts.zip
Removing deploy-develop.tar.gz
HEAD is now at 6009bd0 test
Checking out 6009bd0f as develop...
HEAD is now at 6009bd0... test

$ ls
Dockerfile
deploy-playbook.yml
server
$ ansible-playbook -e image_file=$IMAGE_FILE -e branch=$CI_BUILD_REF_NAME -e full_image_name=$FULL_IMAGE_TAG deploy-playbook.yml
Using /etc/ansible/ansible.cfg as config file
1 plays in deploy-playbook.yml

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [deploy-host]

TASK [copy docker image] *******************************************************
task path: /builds/test/test/deploy-playbook.yml:44
fatal: [deploy-host]: FAILED! => {"changed": false, "failed": true, "msg": "could not find src=/builds/test/test/deploy-develop.tar.gz"}

NO MORE HOSTS LEFT *************************************************************
    to retry, use: --limit @deploy-playbook.retry

PLAY RECAP *********************************************************************
deploy-host            : ok=1    changed=0    unreachable=0    failed=1   


ERROR: Build failed with: exit code 1

我怀疑我没有正确设置或使用跑步者,但我在文档中找不到任何超出非常简单的情况的东西,我不知道该工具是否足以知道它们如何在引擎盖下组合在一起 .

4 回答

  • 0

    你是否在你身上启用了工件 gitlab.rb

    gitlab_rails['artifacts_enabled'] = false
    

    Build Artifacts documentation中所述?

  • 5

    缓存有点奇怪,但基本上:

    高速缓存下的 <dir path> 在构建作业之间可用,而 <dir path> 构件将允许您在同一作业中使用它 .

    所以:

    cache:
      untracked: true
      key: "$CI_BUILD_REF_NAME"
      paths:
        - cache-dir/
    
    setup:
      stage: setup
      [snip]
      artifacts:
        paths:
         - cache-dir/ #notice that the path above is the same
    

    这将允许您在每个构建作业之间缓存文件,同时允许您在同一作业中使用相同的缓存 .

    不要忘记在每个构建步骤中添加工件所需的文件 .

  • 1

    缓存不是为了在构建阶段之间传递文件而设计的 .

    来自doc

    cache:定义后续运行之间应缓存的文件列表

    我认为你所需要的实际上正在进行中:WIP: Download build artifacts from previous stages and restore them in context of the build (Technology Preview)

  • 1

    首先,更新gitlab和gitlab运行器,尤其是1.0.4运行器是安静的实验 .

    其次在缓存定义中,你应该添加一个键见https://docs.gitlab.com/ce/ci/yaml/README.html#cache-key

    cache:
      key: "$CI_BUILD_REF_NAME"
      paths:
      - ..
    

    https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/configuration/advanced-configuration.md#the-runners-section,您应该修改 config.toml 并添加缓存目录

    的cache_dir:

    构建缓存的目录将存储在所选执行程序(本地,Docker,SSH)的上下文中 . 如果使用docker executor,则此目录需要包含在其volumes参数中 .

相关问题