使用 Maven 在 gitlab 中运行(Docker)测试容器
Posted
技术标签:
【中文标题】使用 Maven 在 gitlab 中运行(Docker)测试容器【英文标题】:Run (Docker) Test Container in gitlab with Maven 【发布时间】:2020-03-29 22:57:42 【问题描述】:我在 gitlab ci/cd 管道中工作。此管道使用 maven 和 docker 执行其所有命令(不包括部署)。在这种情况下,我正在尝试运行集成测试(由 maven 启动),它使用测试容器(用于 mysql 数据库)。这些测试在本地运行时运行良好。但是,当我尝试从 gitlab 运行它们时遇到了问题。我相当确定我的问题是我无法访问 docker(但是,在这种情况下,我需要我的图像同时具有 docker 和 maven)。
这里是gitlab文件的适用部分:
#This phase is only run when merging (to master)
merge_tests:
image: maven:latest
stage: mvn_build_and_test
#TODO can remove services if this does not work without docker image as base image
services:
- docker:stable-dind
#set variables for use later when running maven in script section
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
script:
#ONLY RUN WHEN MERGE REQUEST IS TO MASTER BRANCH
#if ["$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" == "master"]; then
#check docker is installed and running
#- docker --version #this fails if uncommented
- mvn $MAVEN_CLI_OPTS clean install -Pintegration-test-profile
#; fi
#only:
#- merge_requests
这是 gitlab 上的错误堆栈跟踪:
org.testcontainers.containers.ContainerLaunchException: Container startup failed
1958 Caused by: org.testcontainers.containers.ContainerFetchException: Can't get Docker image: RemoteDockerImage(imageNameFuture=java.util.concurrent.CompletableFuture@21baa903[Completed normally], imagePullPolicy=DefaultPullPolicy(), dockerClient=LazyDockerClient.INSTANCE)
1959 Caused by: java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
在任何人建议之前,此测试在本地工作(并且已经在本地工作了很长时间)。这是示例输出的一部分:
[INFO] --- maven-failsafe-plugin:2.22.1:integration-test (integration-tests) @ reading-comprehension-api ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.connor.retrieving.GetByIsbnIT
-----content ommitted for brevity----------------------------
Tests run: 3, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 10.451 s - in com.connor.adding.AddAssessmentIT
--content ommitted, but test containers start
[INFO] --- maven-failsafe-plugin:2.22.1:verify (integration-tests) @ reading-comprehension-api ---
[INFO]
[INFO] --- maven-cucumber-reporting:2.8.0:generate (addCucumberReport) @ reading-comprehension-api ---
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
[INFO] About to generate Cucumber report.
[INFO]
[INFO] --- maven-cucumber-reporting:2.8.0:generate (getCucumberReport) @ reading-comprehension-api ---
[INFO] About to generate Cucumber report.
[INFO]
[INFO] --- maven-cucumber-reporting:2.8.0:generate (updateCucumberReport) @ reading-comprehension-api ---
[INFO] About to generate Cucumber report.
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ reading-comprehension-api ---
[INFO] Installing /home/connor/Desktop/code/reading-comprehension-api/target/reading-comprehension-api-0.0.1-SNAPSHOT.jar to /home/connor/.m2/repository/com/connor/reading-comprehension-api/0.0.1-SNAPSHOT/reading-comprehension-api-0.0.1-SNAPSHOT.jar
[INFO] Installing /home/connor/Desktop/code/reading-comprehension-api/pom.xml to /home/connor/.m2/repository/com/connor/reading-comprehension-api/0.0.1-SNAPSHOT/reading-comprehension-api-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 38.282 s
[INFO] Finished at: 2019-12-04T22:31:23-05:00
我应该如何解决这个问题?我应该创建自己的安装了 maven 和 docker 的基本映像吗?我可以轻松修改我的容器以在我的容器中使用 apt get 吗?欢迎提出任何建议,但我希望同时拥有 docker 和 maven,因为它们在我的技术堆栈中并且正在工作——只是一个 gitlab 配置问题。 这里是gitlab上的项目位置:https://gitlab.com/connorbutch/reading-comprehension-api
虽然我当然欢迎 cmets 参与项目的其他部分,但应通过电子邮件将其定向给我,以保持此页面干净(不要放在这里)
【问题讨论】:
没关系,我可以通过添加一些变量来解决我自己的问题:) 【参考方案1】:对于任何想知道的人:您可以通过在 CI/CD 配置中添加一些变量来解决此问题。
我添加的是:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
#these were added to try and get test containers to work -- if it doesnt, can remove below two
DOCKER_HOST: "tcp://docker:2375"
DOCKER_DRIVER: "overlay2"
【讨论】:
我试过了,这确实有效,但它破坏了 docker 推送到 SSL 注册表。错误是 -> 无法从非 TTY 设备执行交互式登录。您知道如何让 SSL 上的 testcontainers 和 docker login 都能正常工作吗?【参考方案2】:对于正在运行 kubernetes 运行器的任何人,必须将运行器配置为运行 dind 服务的特权:
runners:
## Run all containers with the privileged flag enabled
## This will allow the docker:stable-dind image to run if you need to run Docker
## commands. Please read the docs before turning this on:
## ref: https://docs.gitlab.com/runner/executors/kubernetes.html#using-docker-dind
##
privileged: true
https://docs.gitlab.com/runner/install/kubernetes.html#running-privileged-containers-for-the-runners
values.yaml 中的示例
【讨论】:
以上是关于使用 Maven 在 gitlab 中运行(Docker)测试容器的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Gitlab CI 构建 Java Maven 项目?
如何为 GitLab CI 运行器启用 Maven 工件缓存?