我似乎无法在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
和测试。现在,我已经离开了这一步,以测试部署。 .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
我怀疑我没有正确设置或使用跑步者,但我在文档中找不到任何超出非常简单的情况的内容,而且我不太了解该工具以便知道它们如何组合在一起引擎盖下。
答案 0 :(得分:5)
缓存不是为了在构建阶段之间传递文件而设计的。
来自doc
cache:定义后续应该缓存的文件列表 运行
我认为您所需要的实际上正在进行中:WIP: Download build artifacts from previous stages and restore them in context of the build (Technology Preview)
答案 1 :(得分:1)
答案 2 :(得分: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,则此目录需要包含在其卷参数中。
答案 3 :(得分:0)
缓存有点奇怪,但基本上是:
缓存下的 <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
这将允许您在每个构建作业之间缓存文件,同时允许您在内部使用相同的缓存。
不要忘记在每个构建步骤中添加工件所需的文件。