1

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

4

1 回答 1

2

before_script在每项工作之前运行,因此这不是您想要的。但是你可以有第一份工作来构建镜像,并利用每个工作可以使用不同的 Docker 镜像这一事实。手册中介绍了映像的构建。

选项A(嗯...有点好)

有 2 个运行器,一个带有 shell 执行器(标记为shell),一个带有 Docker 执行器(标记为docker)。然后,您将有一个专门用于构建 docker 映像的工作的第一阶段。它将使用外壳运行器。

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映像)=“ 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
于 2016-05-13T08:56:33.393 回答