You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

将GitLab CI制品任务转换为Jenkins Post段等价实现求助

GitLab CI制品配置转Jenkins流水线等价实现

我是Jenkins新手,正在尝试把GitLab CI作业转换成Jenkins流水线。已经把GitLab CI的测试阶段转成了Jenkins的单元测试阶段,但不清楚怎么实现GitLab CI中artifacts部分的等价Jenkins Post段配置,包括制品路径保存、30天过期规则等,希望得到帮助。

原GitLab CI配置

stage: test
  script:
    - ./mvnw clean test -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE -Dspock.configuration=src/test/groovy/com/pet/api/test/runners/UnitTestsConfig.groovy
  artifacts:
    when: always
    paths:
      - target/surefire-reports
      - target/site/jacoco
    expire_in: 30 days
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml

已完成的Jenkins阶段配置

stage("Unit Tests") {
      steps {
        sh './mvnw clean test -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE -Dspock.configuration=src/test/groovy/com/pet/api/test/runners/UnitTestsConfig.groovy'
      }
      post {
        always {
          junit 'target/surefire-reports/TEST-*.xml'
        }
      }
    }

等价实现方案

要对应GitLab CI的artifacts配置,只需在Jenkins流水线的post块中添加archiveArtifacts步骤,同时保留已有的junit步骤即可:

  • 保存指定路径制品:用archiveArtifacts指定要归档的路径,对应GitLab的paths配置
  • 设置30天过期:通过archiveArtifacts的defaultExpirationDays参数设置自动清理时间,对应GitLab的expire_in
  • 始终执行归档:将步骤放在post的always块中,对应GitLab的when: always

完整Jenkins配置代码

stage("Unit Tests") {
  steps {
    sh './mvnw clean test -Dspring.profiles.active=$SPRING_PROFILES_ACTIVE -Dspock.configuration=src/test/groovy/com/pet/api/test/runners/UnitTestsConfig.groovy'
  }
  post {
    always {
      // 对应GitLab artifacts.reports.junit,解析并展示JUnit测试报告
      junit 'target/surefire-reports/TEST-*.xml'
      // 对应GitLab artifacts的路径、过期规则及执行时机
      archiveArtifacts(
        artifacts: 'target/surefire-reports/**,target/site/jacoco/**',
        defaultExpirationDays: 30,
        allowEmptyArchive: false // 可选,若制品为空时是否允许归档,按需调整
      )
    }
  }
}

补充说明

  • artifacts参数用逗号分隔多路径,支持通配符,和GitLab的路径匹配逻辑一致
  • defaultExpirationDays: 30会让归档的制品在30天后自动被Jenkins清理
  • allowEmptyArchive默认值为false,如果测试阶段未生成对应文件,流水线会失败;若允许空归档,可设置为true

内容的提问来源于stack exchange,提问作者Denis Kisina

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 16:25:28