【问题标题】:Maven build as GitLab artifact is being ignored by following jobs以下作业忽略了作为 GitLab 工件的 Maven 构建
【发布时间】:2020-11-26 22:01:43
【问题描述】:

我有一个管道,其中前 2 个阶段是一个用于 build 和一个用于 单元测试,在 Maven 项目中。

这两个阶段可以用以下命令来概括:

  • [构建]mvn -s ci/settings.xml test-compile
  • [unit_tests]mvn -s ci/settings.xml verify

在本地机器上运行它们时,第一个打印:

[INFO] Changes detected - recompiling the module!
[INFO] Compiling 133 source files to <mydir>

而第二个,鉴于项目已经建成,打印:

[INFO] Nothing to compile - all classes are up to date

这也是 GitLab 上的预期行为。

然而,在 GitLab 上发生的事情是,单元测试 阶段打印的内容与构建阶段完全相同,这意味着它没有正确使用我在之前导出的工件阶段。

这是构建工作:

build:
  stage: build
  image: maven:3.6-jdk-11
  script:
    - 'mvn -s ci/settings.xml test-compile'
  except:
    - tags
  artifacts:
    paths:
      - target/

此作业以以下日志结束:

Uploading artifacts...
target/: found 226 matching files and directories  
Uploading artifacts as "archive" to coordinator... ok  id=1964 responseStatus=201 Created token=qwYzjEeM

表示target文件夹已正确上传。

这是单元测试的工作:

junit:
  stage: unit_tests
  script:    
    - 'mvn -s ci/settings.xml verify'
  artifacts:
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml

此作业从以下日志开始:

Downloading artifacts for build (1964)...
Downloading artifacts from coordinator... ok        id=1964 responseStatus=200 OK token=qwYzjEeM

表示 target 文件夹已正确接收(我还添加了 ls -la target 以查看文件是否存在并且它们看起来是否正确)。

鉴于工件似乎已正确上传/下载,为什么单元测试作业要重建整个项目?

【问题讨论】:

  • 我不知道为什么这个gitlab目录移动失败,但是你为什么要这样做呢?为什么不直接运行verify
  • 这是我继承的一个管道,我无法更改它的步骤,因为很多项目都依赖于这个模板。此外,在 JUnit 之后还有更多的 maven 步骤,它们会遇到与这个完全相同的问题。

标签: maven gitlab gitlab-ci


【解决方案1】:

所以,最终问题是由 GitLab 恢复工件的方式和 git clone 的工作方式引起的。

基本上 GitLab 将保留工件的最后修改时间戳,而 git clone 不会。

这会导致以下情况:

  1. git clone被执行,现在src目录下的所有文件都有当前时间戳
  2. GitLab 提取工件存档并保留时间戳,因此现在target 目录中的文件具有它们在之前 CI 作业中的时间戳
  3. 这意味着target 目录中的文件看起来比src 目录中的文件旧,这会触发 Maven 中的完全重新编译

该问题已报告给 GitLab,可以通过here 进行跟踪。

一种临时解决方法是手动创建工件存档并在其他作业开始后手动提取它。

这是一个用作变通方法的示例管道(-DD 选项是在提取时不保留时间戳的选项,这就是使该变通方法实际起作用的原因):

.prepare-build-artifact: &prepare-build-artifact |
  apt update && apt install -y zip
  zip -r -q target.zip target/

.extract-build-artifact: &extract-build-artifact |
  unzip -DD -q target.zip

build:
  stage: build
  image: maven:3.6-jdk-11
  script:
    - 'mvn test-compile'
    - *prepare-build-artifact
  except:
    - tags
  artifacts:
    paths:
      - target.zip

junit:
  stage: unit_tests
  script:
    - *extract-build-artifact
    - 'mvn verify'
  artifacts:
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml

【讨论】:

    猜你喜欢
    • 2022-01-07
    • 2019-11-05
    • 2020-09-09
    • 2021-07-02
    • 2016-06-14
    • 1970-01-01
    • 2014-01-25
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多