如何在 GitLab Runner 执行的图像中准备 shell 环境?

How to prepare the shell environment in an image executed by GitLab Runner?

我在自托管的 GitLab 实例和 10 个 GitLab Runners 上 运行ning CI 作业。
对于这个最小的例子,需要两个跑步者:

  1. Admin-01
    安装了 Docker 的 shell 运行ner。 它可以执行例如docker build ... 创建新图像,然后将其推送到私有 Docker 注册表(也是自托管/GitLab 安装的一部分)
  2. Docker-01
    docker 运行ner,它执行之前构建的镜像。

在普通的裸机、虚拟机或 shell 运行ner 上,我会修改例如~/.profile 在执行 before_scriptscript 部分之前执行命令。在我的用例中,我需要设置新的环境变量并获取一些我想在图像中 运行 的工具提供的配置文件。是的,环境变量可以设置不同,但在执行 before_scriptscript 部分之前,无法自动获取 Bash 脚本的接缝。

手动获取 Bash 源文件时,它可以工作。我还注意到,我必须在 script 块中再次获取它。所以我假设 Bash 会话在 before_script 块到 script 块之间结束。当然,由图像用户手动在每个 .gitlab-ci.yml 文件中手动获取工具 Bash 配置脚本并不是一个好的解决方案。

myjobn:
  # ...
  before_script:
    - source /root/profile.additions
    - echo "PATH=${PATH}"
    # ...
  script:
    - source /root/profile.additions
    - echo "PATH=${PATH}"
    # ...

提到的修改例如shell 运行ners 在 GitLab Runner 执行的图像中不起作用。感觉容器里的Bash没有启动为登录shell.

最小示例图像构建如下:

  1. 从Docker Hub
  2. 获取debian:bullseye-slim
  3. 使用 Docker 文件中的 RUN 命令修改一些回显输出
    • /etc/profile
    • /root/.bashrc
    • /root/.profile
# ...

RUN echo "echo GREETINGS FROM /ROOT/PROFILE" >> /root/.profile \
 && echo "echo GREETINGS FROM /ETC/PROFILE" >> /etc/profile \
 && echo "echo GREETINGS FROM /ROOT/BASH_RC" >> /root/.bashrc

当作业开始时,没有 echo 打印消息,而猫显示时,echo 命令已在构建图像时放在正确的位置。

接下来我尝试修改

问题:

  1. 如何在 GitLab Runner 管理的 Docker 镜像中启动 Bash 作为登录 shell 以便它准备好配置脚本?
  2. 如何在 before_scriptscript 运行 之前修改容器中的环境。修改意味着环境变量和执行/采购配置脚本或修补的默认脚本,如 ~/.profile.
  3. GitLab Runner 如何使用 Docker 执行作业?
    GitLab在官方文档中没有记录...
    据我所知,它在 GitLab 指定的 Docker 图像和用户定义的图像之间跳转,并共享一些 directories/volumes 左右。

注:
是的,这种行为可以通过 docker run 中的一些 Docker 参数来实现,但正如我所写,GitLab Runner 正在管理容器。或者,如何配置 GitLab Runner 如何启动图像?据我所知,对于这种情况没有可用/记录的配置选项。

A shell runner with Docker installed. It can execute e.g. docker build ...

使用 docker-in-docker 或使用 kaniko。 https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

Shell executor 就像是“最后的手段”,你希望特别ci对服务器进行更改,或者你正在将你的应用程序“部署到”这个服务器。

How to start the Bash in the Docker image managed by GitLab Runner as login shell so it ready configuration scripts?

ENTRYPOING bash -l 添加到您的图像。或者从 gitlab-ci.yml 设置入口点。请参阅有关 ENTRYPOINT 的 docker 文档和有关 image: entrypoint: 的 gitlab-ci.yml 文档。

How to modify the environment in a container before before_script or script runs.

使用修改后的环境构建映像。请参阅有关 ENV 语句的 Dockerfile 文档。

或从 gitlab-ci.yml 文件设置环境。在 gitlab-ci.

中阅读有关 variables: 的文档

How to prepare the shell environment in an image executed by GitLab Runner?

不要。这个想法是环境是可重现的cible,因此,事先不应该有任何变化。在 gitlab-ci 文件中添加 variables: 并尽可能使用基本图像。

How does GitLab Runner execute a job with Docker?

This is not documented by GitLab in the official documentation ...

Gitlab 是开源的。

What I know so far, it jumps between Docker images specified by GitLab and user defined images and shares some directories/volumes or so.

是的,首先执行 gitlab-runner-helper - 它有 git 和 git-lfs,基本上克隆存储库并下载和上传工件。然后用image:指定的容器是运行,克隆的存储库被复制到其中,并在其中执行专门ci准备的shell脚本。