gitlab docker executor-在before_script之后缓存图像 [英] Gitlab docker executor - cache image after before_script
问题描述
在 gitlab-ci
中, .gitlab-ci.yml
文件,以在运行任何实际脚本之前执行命令,称为 before_script
。 .gitlab-ci.yml
示例说明在此处安装辅助程序。但是,我注意到,使用docker executor时,这些更改未缓存在Docker中。我天真地假设运行这些命令后,docker将缓存该图像,因此对于下一次运行或测试,docker将仅加载 before_script
之后生成的缓存图像。
In gitlab-ci
there's an option in the .gitlab-ci.yml
file to execute commands before any of the actual script runs, called before_script
. .gitlab-ci.yml
examples illustrate installing ancillary programs here. However, what I've noticed is that these changes are not cached in Docker when using a docker executor. I had naively assumed that after running these commands, docker would cache the image, so for the next run or test, docker would just load the cached image produced after before_script
. This would drastically speed up builds.
例如,我的 .gitlab-ci.yml
看起来有点像:
As an example, my .gitlab-ci.yml
looks a little like:
image: ubuntu
before_script:
- apt-get update -qq && apt-get install -yqq make ...
build:
script:
- cd project && make
一种可能的解决方案是转到运行器机器并创建可以构建我的软件的docker映像无需任何其他安装,然后在yaml文件的图片
部分中进行引用。这样做的缺点是,每当我要添加依赖项时,都需要登录到运行器计算机并更新映像,然后构建才能成功。如果我只需要将依赖项添加到 apt-get install
的末尾,并让docker / gitlab-ci处理适当的缓存,那就更好了。
A possible solution is to go to the runner machine and create a docker image that can build my software without any other installation and then reference it in the image
section of the yaml file. The downside of this is that whenever I want to add a dependency, I need to log in to the runner machine and update the image before builds will succeed. It would be much nicer if I just had to add the dependency to to the end of apt-get install
and have docker / gitlab-ci handle the appropriate caching.
在 .gitlab-ci.yml
缓存命令>,我尝试将其设置为 untracked:true
,我认为这会缓存不是我的项目副产品的所有内容,但似乎没有任何效果。
There is also a cache
command in .gitlab-ci.yml
, which I tried setting to untracked: true
, which I thought would cache everything that wasn't a byproduct of my project, but it didn't seem to have any effect.
有没有办法实现我想要的行为?
Is there any way to get the behavior I desire?
推荐答案
您可以添加一个阶段以首先构建图像。如果图像没有任何变化,则该阶段将非常短,不到1秒。
You can add a stage to build the image in first place. If the image doesn't have any change, the stage will be very short, under 1 second.
您可以在以下阶段使用该图像,从而加快整体速度
You can use that image on the following stages, speeding up the whole process.
这是 .gitlab-ci.yml
的示例:
stages:
- build_test_image
- test
build_test:
stage: build_test_image
script:
- docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
- docker build -t $CI_REGISTRY_IMAGE:test -f dockerfiles/test/Dockerfile .
- docker push $CI_REGISTRY_IMAGE:test
tags:
- docker_build
test_syntax:
image: $CI_REGISTRY_IMAGE:test
stage: test
script:
- pip install flake8
- flake8 --ignore=E501,E265 app/
查看标签 docker_build
。该标签用于在具有该标签的跑步者上强制执行舞台。该运行程序的执行程序是 shell
,它仅用于构建Docker映像。因此,跑步者所在的主机应已安装Docker Engine。我发现此解决方案比docker和其他解决方案。
Look at the tag docker_build
. That tag is used to force the execution of the stage on the runner which has that tag. The executor for that runner is shell
, and it's used only to build Docker images. So, the host where the runner lives should have installed Docker Engine. I found this solution suits better my needs than docker in docker and another solutions.
此外,我正在使用私有注册表,这就是为什么我使用 $ CI_REGISTRY *
变量,但是您可以使用DockerHub而不需要指定注册表。但是问题是要在DockerHub上进行身份验证。
Also, I'm using a private registry, that's why I'm using $CI_REGISTRY*
variables, but you can use DockerHub without need to specify the registry. The problem would be to authenticate on DockerHub, though.
这篇关于gitlab docker executor-在before_script之后缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!