如何在GitLab CI中用Matrix聚合并行任务报告并统一发Slack?
解决方案:聚合并行测试报告并单次发送Slack通知
核心思路是将测试执行与报告聚合、通知分离:保留并行测试任务生成独立的结构化测试结果(JSON格式,便于聚合),新增一个专门的任务等待所有测试完成后,统一聚合结果生成单份报告,再发送一次Slack通知。
修改后的.gitlab-ci.yml配置
# 定义阶段执行顺序 stages: - test - post_test test: stage: test tags: - saas-linux-large-amd64 parallel: matrix: - VAR: "@WebSuite1" - VAR: "@WebSuite2" script: # 执行测试时同时生成JSON格式报告(用于聚合)和单套件HTML报告(可选保留) - mvn clean test -Dcucumber.filter.tags="$VAR" -Dcucumber.plugin="json:target/cucumber-$VAR.json,html:target/cucumber-$VAR.html" artifacts: when: always paths: - target/cucumber-$VAR.json - target/cucumber-$VAR.html expire_in: 1 day aggregate_and_notify: stage: post_test tags: - saas-linux-large-amd64 # 依赖所有test阶段的并行任务,必须等待全部完成才执行 needs: [test] script: # 使用cucumber-reporting插件聚合所有JSON报告,生成统一HTML报告 - | mvn net.masterthought:cucumber-reporting:generate \ -Dcucumber.reporting.jsonReportDirectory=./target \ -Dcucumber.reporting.outputDirectory=./target/aggregated-report \ -Dcucumber.reporting.projectName="Web Acceptance Tests" # 发送聚合报告的Slack通知 - | curl -X POST -H 'Content-type: application/json' --data '{ "text": "Selenium Aggregated Test Report: https://shaguf.gitlab.io/-/bites/acceptance-tests/-/jobs/'$CI_JOB_ID'/artifacts/target/aggregated-report/cucumber-html-reports/overview-features.html" }' $SLACK_WEBHOOK_URL artifacts: when: always paths: - target/aggregated-report/ expire_in: 3 days
补充配置说明
pom.xml依赖添加
需要在项目pom.xml中加入cucumber-reporting插件(用于聚合报告),示例配置:<build> <plugins> <plugin> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>5.7.0</version> <!-- 可替换为最新稳定版 --> </plugin> </plugins> </build>关键细节
- 每个并行测试任务生成带唯一标识的JSON报告(用
$VAR区分),避免文件名冲突 aggregate_and_notify任务通过needs: [test]自动拉取所有并行测试的产物- 聚合后的报告存储在该任务的产物中,Slack通知链接指向这份统一报告
- 移除了原
test任务中的after_script,彻底避免重复发送通知
- 每个并行测试任务生成带唯一标识的JSON报告(用
内容的提问来源于stack exchange,提问作者Mohamed Ahmed
相关产品推荐
相关产品推荐

