简体   繁体   English

使用 Docker Image 用于 MAVEN 项目的 Gitlab CI

[英]Gitlab CI for MAVEN project with Docker Image

I have project on java, where i have my tests.我有关于 Java 的项目,在那里我有我的测试。 Now i have .gitlab-ci.yml :现在我有.gitlab-ci.yml

image: maven:latest

stages:
  - build

build:
  stage: build
  script:
    - mvn test-compile compile
  tags:
    - mytag
  only:
    refs:
      - dev

Each time, when i am making commit in my repo, i am waiting a lot of time, when it will download all depencies.每次,当我在我的 repo 中提交时,我都在等待很多时间,当它下载所有依赖项时。 In docker we can use volume option.在 docker 中,我们可以使用 volume 选项。 The question: Can i download and compile locally this project for creating of .m2 directory, and can i use this directory in my .gitlab-ci.yml .问题:我可以在本地下载和编译这个项目来创建.m2目录,我可以在我的.gitlab-ci.yml使用这个目录。 If yes, can you help me how, because i have not found in internet examples according to it.如果是,你能帮我怎么做,因为我还没有根据它在互联网示例中找到。

I made changes in my /etc/gitlab-runner/config.toml :我在/etc/gitlab-runner/config.toml进行了更改:

  [runners.docker]
    tls_verify = false
    image = "maven:latest"
    privileged = true
    disable_entrypoint_overwrite = false
    oom_kill_disable = false
    disable_cache = false
    volumes = ["/cache","/M2/.m2:/root/.m2"]
    shm_size = 0

/M2/ is a dir, with gitlab-runner owner. /M2/是一个目录,拥有 gitlab-runner 所有者。 But it does not help, how we can see:但这无济于事,我们如何才能看到:

Downloaded from central: https://repo.maven.apache.org/maven2/org/hamcrest/hamcrest-parent/1.3/hamcrest-parent-1.3.pom (2.0 kB at 20 kB/s)
Downloading from atlas: https://dl.bintray.com/qameta/maven/org/seleniumhq/selenium/selenium-java/3.8.1/selenium-java-3.8.1.pom
Downloading from nio: http://clojars.org/repo/org/seleniumhq/selenium/selenium-java/3.8.1/selenium-java-3.8.1.pom
Downloading from central: https://repo.maven.apache.org/maven2/org/seleniumhq/selenium/selenium-java/3.8.1/selenium-java-3.8.1.pom
Progress (1): 2.2/5.9 kB
Progress (1): 5.0/5.9 kB
Progress (1): 5.9 kB    

steps to take: (host machine - the machine gitlab - runner installd on and the runner signed)采取的步骤:(主机 - 机器 gitlab - 运行器已安装且运行器已签名)

  1. clone your repository on the host machine在主机上克隆您的存储库
  2. compile it with maven用maven编译
  3. check that there is cached data folder under /root/.m2 on the host machine检查主机上的 /root/.m2 下是否有缓存的数据文件夹
  4. fix your config.toml add this line修复你的 config.toml 添加这一行

volumes = ["/cache","~/.m2:/root/.m2"]卷 = ["/cache","~/.m2:/root/.m2"]

  1. then add to .gitlab-ci.yml然后添加到 .gitlab-ci.yml
 cache: paths: - /root/.m2/

variables: MAVEN_OPTS: "-Dmaven.repo.local=.m2"

the .gilatb.yml should look like this .gilatb.yml 应该是这样的

https://stackoverflow.com/a/40024602/4267015 https://stackoverflow.com/a/40024602/4267015

  • I build a "base docker image".我构建了一个“基础 docker 镜像”。 Copy pom.xml to docker image and then run maven verify that image.将 pom.xml 复制到 docker 镜像,然后运行 ​​maven 验证该镜像。 This is dockerFile:这是 dockerFile:
 FROM maven:3.6.1-jdk-8-alpine COPY pom.xml . RUN mvn verify clean --fail-never
  • Open Terminal, then go to the project directory and run docker build:打开终端,然后进入项目目录并运行 docker build:

docker build -t xxx/projectName:base . docker build -t xxx/projectName:base 。

  • Now "base docker image" has cached data.现在“基本泊坞窗图像”已缓存数据。

  • And then You can use "base docker image" in gitlab-ci.yml:然后你可以在 gitlab-ci.yml 中使用“基础 docker 镜像”:

 image: xxx/projectName:base stages: - build build: stage: build script: - mvn test-compile compile tags: - mytag only: refs: - dev

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM