简单介绍一下如何使用 GitLab-CI 来自动创建 Docker 镜像, 并上传到仓库.
想法
用 GitLab 来管理 Dockerfile 是一件很显然的事情了. 在每次更改 Dockerfile 之后, 都要手动 build 然后 push 到 registry, 有点烦. 正好这两天自己开了个 registry. 那就采用一种自动的方法来帮助我们做这种机械重复的工作.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
首先我想到的是使用 webhook 的响应 结合docker api 来调用 docker build 和 push. 我试验了一下觉得比较麻烦.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
后来想到可以使用了 GitLab CI, 并且自 GitLab 8.x 开始已经集成了 GitLab CI Server. 所以也不用额外部署 CI Server 了. 我们要做的工作就是部署一下 GitLab CI runner. 然后在 Dockerfile 的项目里配置一下 .gitlab-ci.yml
告诉 GitLab CI runner 如何做就行了.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
安装 Docker
在你想用来 build image 的机器上, 显然需要先装好 Docker. 参见 Docker官方文档.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
安装 GitLab CI Runner
在用来 build 的机器上我们需要安装 GitLab CI Runner. 官方项目里面提供了很多安装说明. 可以直接找你对应的需要. 这里我就说一下我怎么直接在 Ubuntu 14.04 LTS 上安装的.(对应的官方文档)文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash sudo apt-get install gitlab-ci-multi-runner
然后注册 runner:文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
sudo gitlab-ci-multi-runner register
当中会让你填写一些信息. 例如你的 gitlab-ci coordinator 的地址和注册这个 runner 的 token, 这两个在你 GitLab 中可以找到. 具体的内容我忘记截图了. 关于 executor 的话, 我这里使用的是 shell, 因为我将 runner 直接运行在物理机的系统上, 想其能够直接使用 docker.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
注册好后, 我们可以看 runner 的配置文件, 类似如下:文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
# /etc/gitlab-runner/config.toml concurrent = 1 [[runners]] url = "http://gitlab.com/ci" token = "******************" name = "image builder" executor = "shell" [runners.ssh] [runners.docker] image = "" privileged = false [runners.parallels] base_name = ""
关于这些参数可以在官方文档中找到. 注册好的 runner 在 GitLab 中如下图所示.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
实际显示情况可能和上图有不同, 因为我已经将这个 runner 设置为 specific runner 了. 关于 runner 的说明可以参考 官方文档 configuring runner.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
使用 GitLab runner 来 build docker image 的相关说明, 可以参考 官方文档 using docker build. 主要需要注意的是, 为了要让 runner 可以调用 docker 命令, 需要把 gitlab-runner 这个用户加入 docker 所在组.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
sudo usermod -aG docker gitlab-runner
配置项目
可以参考官方文档 Configuring project. 我这里直接贴出我用的配置了.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
stages: - build_image - push_image before_script: - docker info build_image: stage: build_image script: - docker build -t myregistry/aplusplus/ubuntu:14.04 . push_image: stage: push_image script: - docker push myregistry/aplusplus/ubuntu:14.04
简单地说, stages 定义了你要做几步(stage) 以及他们之间的顺序. 默认每个 stage 都是在之前所有 stage 成功执行后才会执行. 每个 stage 可以包含多个任务(job), 例如上面的 build_image 和 push_image. 这里只定义了一个. 当每个 stage 有多个 jobs 时, 每个 jobs 会并行执行.文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
当你每次修改项目并 push 到 gitlab 后, runner 就会开始执行你配置的任务了. 如下图文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/
文章源自运维生存时间-https://www.ttlsa.com/docker/use-gitlab-ci-to-automatically-create-docker-images/

评论