gitlab CI/CD:如何进入容器进行测试,即获得交互式 shell

gitlab CI/CD: How to enter into a container for testing i.e getting an interactive shell

就像在docker中一样,我们可以进入一个容器并进行互动shell

docker-compose exec containername /bin/bash

类似gitlab中的scriptCI/CD可以进入吗。喜欢它提供了一个互动shell

例如:

build:
  stage: build
  script:
    - pwd; ls -al
    HERE I WANT TO HAVE AN INTERACTIVE SHELL SO THAT I CAN CHECK FEW THINGS

我认为我们需要在这里绕个小弯,解释一下工作在 GitLab 中是如何工作的 CI。

每个作业都是一个封装的 docker 容器。容器仅执行您希望在 script 指令中执行的内容。默认情况下,共享 运行ners 上的作业使用 ruby 容器映像。

如果您想检查图像中的可用内容,或者您​​想在本地尝试一下。您可以这样做 运行在本地使用此图像创建一个容器并将您的项目文件夹安装到其中。

docker run --rm -v "$(pwd):/build/project" -w "/build/project" -it <the job image> /bin/bash # or /bin/sh or whatever shell is available in the image.
# -v mounts the current directory int /build/project in your container
# -w changes the working directory to the mounting point
# /bin/bash starts the shell, it might be that there are others within the image

如果您想使用不同的 docker 图像,可以说因为您正在 运行 一些其他构建工具,您可以使用 image 指令指定它,例如:

build:
  image: maven:latest
  script:
    - echo "some output"

您确实拥有您的工作中可用的功能,这是由图像提供的。因为该作业将 运行 在该图像的容器内。

您甚至可以使用 https://github.com/firecow/gitlab-ci-local 等工具在本地进行验证。但最后这些只是 docker 图片,您可以轻松地自己重新创建流程。