如何在CI脚本作业之前的推送时重建docker镜像

时间:2016-05-12 18:34:33

标签: docker continuous-integration gitlab gitlab-ci gitlab-ci-runner

我想在GitLab CI脚本中生成Dockerfile并构建它。然后在构建作业中使用此新生成的图像。我怎样才能做到这一点?试图使用全局的before_script,但它已经在默认容器中启动。我需要用任何容器来做这件事。

1 个答案:

答案 0 :(得分:2)

before_script在每个工作之前运行,所以这不是你想要的。但是你可以有第一份工作来完成图像构建,并利用每个job可以使用不同Docker镜像的事实。 manual涵盖了图像的构建。

选项A(嗯...好的)

有2个跑步者,一个有shell执行者(标记为 shell ),另一个有Docker执行者(标记为 docker )。然后,您将拥有第一个阶段,其中包含专门用于构建泊坞窗图像的作业。它将使用 shell runner。

image_build:
  stage: image_build
  script:
    - # create dockerfile
    - # run docker build
    - # push image to a registry
  tags:
    - shell

然后第二个作业将使用具有docker executor的跑步者运行并使用此创建的图像:

job_1:
  stage: test
  image: [image you created]
  script:
    - # your tasks
  tags:
    - docker

问题在于,跑步者需要成为 docker 组的一部分,该组具有安全隐患

选项B(更好)

第二个选项会做同样的事情但只有一个跑步者使用Docker执行器。 Docker镜像将构建在一个正在运行的容器中( gitlab / dind:latest image)=“docker in docker”解决方案。

stages:
  - image_build
  - test

image_build:
  stage: image_build
  image: gitlab/dind:latest
  script:
    - # create dockerfile
    - # run docker build
    - # push image to a registry

job_1:
  stage: test
  image: [image you created]
  script:
    - # your tasks
相关问题