使用 Maven 在 gitlab 中运行(Docker)测试容器 [英] Run (Docker) Test Container in gitlab with Maven
问题描述
我在 gitlab ci/cd 管道中工作.此管道使用 maven 和 docker 执行其所有命令(不包括部署).在这种情况下,我正在尝试运行集成测试(由 maven 启动),它使用测试容器(用于 mysql 数据库).这些测试在本地运行时运行良好.但是,当我尝试从 gitlab 运行它们时遇到了问题.我相当确定我的问题是我无法访问 docker(但是,在这种情况下,我需要我的图像同时具有 docker 和 maven).
I am working in a gitlab ci/cd pipeline. This pipeline executes all of its commands (excluding the deployments) with maven and docker. In this case, I am trying to run integration tests (that are kicked off by maven), which use a test container (for the mysql database). These tests work fine when running locally. However, I am running into issues when I try to run them from gitlab. I am fairly certain my issue is I don't have access to docker (however, in this case, I need my image to have both docker and maven).
这里是gitlab文件的适用部分:
Here is the applicable section of gitlab file:
#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 上的错误堆栈跟踪:
Here is the error stack trace on 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
在任何人建议之前,此测试在本地工作(并且已经在本地工作了很长时间).这是示例输出的一部分:
Before anyone suggests it, this testing works locally (and has worked locally for a long time). Here is part of the sample output:
[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-接口
How should i fix this? Should i create my own base image which has maven and docker installed? Can I easily modify my container to use apt get in my container? Any suggestions are welcome, but I would like to have both docker and maven, since they are in my technology stack and working--just a gitlab config issue. Here is the project location on gitlab: https://gitlab.com/connorbutch/reading-comprehension-api
虽然我当然欢迎对项目的其他部分发表评论,但应该通过电子邮件将这些评论发送给我,以保持页面整洁(不要放在这里)
推荐答案
对于任何想知道的人:您可以通过在 CI/CD 配置中添加一些变量来解决此问题.
For anyone who's wondering: You can fix this by adding a few variables to your CI/CD configuration.
我添加的是:
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"
这篇关于使用 Maven 在 gitlab 中运行(Docker)测试容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!