代码之家  ›  专栏  ›  技术社区  ›  Santhosh

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

  •  1
  • Santhosh  · 技术社区  · 3 年前

    就像在docker中一样,我们可以通过进入一个容器,并拥有一个交互式shell

    docker-compose exec containername /bin/bash
    

    类似于 script 在gitlab CI/CD中,我们可以进入它。就像它提供了一个交互式外壳一样

    如:

    build:
      stage: build
      script:
        - pwd; ls -al
        HERE I WANT TO HAVE AN INTERACTIVE SHELL SO THAT I CAN CHECK FEW THINGS
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   Simon Schrottner    3 年前

    我认为我们需要在这里绕一小段路,解释一下GitLab CI中的工作原理。

    每个作业都是一个封装的docker容器。容器只执行您希望在 script 指令。默认情况下,共享运行程序上的作业使用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图像,您可以轻松地自行重新创建流程。