Gitlab Coverage徽章永远不知道 [英] Gitlab coverage badge always unknow

查看:148
本文介绍了Gitlab Coverage徽章永远不知道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

我正在研究Laravel软件包项目,并建立了一个管道以对合并请求进行测试(针对php 7.3、7.4和8.0),并将其发布到主分支上的软件包注册表中.

我正在尝试设置代码覆盖范围的徽章,但由于它始终会误涂 unknown 百分比,因此我无法使其正常工作.

我做了什么

管道

 阶段:-依赖-棉绒- 测试-发布#依存关系工作下载依赖关系:阶段:依赖#工作的东西#Linting职位phpcs:阶段:棉绒#工作的东西#测试作业模板.testing_template:&测试阶段:测试脚本:-php -dxdebug.mode = coverage vendor/bin/phpunit --coverage-text --colors = never只要:-merge_requests#测试工作的PHP 8:图片:mileschou/xdebug:8.0<< ;: *测试的PHP 7.4:图片:mileschou/xdebug:7.4<< ;: *测试的PHP 7.3:图片:mileschou/xdebug:7.3<< ;: *测试#发布到程序包注册表发布:只要:- 掌握-标签#工作的东西 

所以我的管道如下:

  • 在功能分支上
    • 下载依赖项
    • 皮棉代码
    • 针对php 7.3、7.4和8.0进行测试
  • 在主分支和标签上
    • 将软件包发布到软件包注册表中

因此,在合并请求中执行的部分如下

此外,我可以在管道作业部分看到每个测试作业的代码覆盖范围

(由于代码库相同,所以我不确定为什么测试会导致不同的代码覆盖率,但是显然与php版本以及代码解析中进行的特定优化有关.)

徽章

由于这是一个Laravel软件包项目,因此我正在用PhpUnit对其进行测试,因此我使用了给定的正则表达式进行测试覆盖率解析设置(取自Gitlab .

我坚信Gitlab找不到所需的 php 7.4 作业,因为它不是在 master 分支上执行,而是在功能分支上执行的,所以我尝试了将%{default_branch} 占位符替换为%{commit_sha} 以引用特定的提交,但这不起作用,因为(我想)它引用了合并提交(仅触发发布作业),而不触发整个管道的

.

我被困住了

我不知道如何获得 master 分支的代码覆盖率.

我不想在主分支上再次执行测试,因为我已经在合并功能分支之前对其进行了测试.

我该怎么办?我想念什么吗?

解决方案

我在不同的堆栈( Java,Gradle和Jacoco )中遇到了相同的问题.我已经解决了这个问题,我将分享我所做的事情.希望我的解决方案能给您一些帮助.

首先,在您的项目中,转到设置> CI/CD ,并展开常规管道部分.根据您使用的工具,用适当的正则表达式填充测试覆盖率分析文本框.您可以在此页面上找到这些正则表达式的示例列表:测试覆盖率解析示例

Gitlab期望测试运行器工具将基于CI作业日志中某处的给定正则表达式打印测试覆盖率百分比.如果测试运行器引擎未将其打印到控制台,则可以手动进行.例如:

 测试:阶段:测试脚本:-gradle测试jacocoTestReport --info#将测试覆盖率打印到控制台-cat build/reports/jacoco/test/html/index.html |grep -o'总计[^%] *%'工件:路径:-构建/报告/jacoco/测试/* 

确保日志中包含测试范围后,徽章将可以正常工作.

管道和覆盖率徽章示例

The problem

I'm working on a Laravel package project and setting up a pipeline to test it (against php 7.3, 7.4 and 8.0) on merge request and publishing it to my package registry on main branch.

I'm trying to set up a badge for code coverage, but i can't make it work correctly since it is always dipslayng unknown percentage.

What i've done

The pipeline

stages:
  - dependancies
  - linting
  - test
  - publish

# Dependancies job
download dependancies:
  stage: dependancies
  # job stuff

# Linting jobs
phpcs:
  stage: linting
  # job stuff

# test jobs templates
.testing_template: &testing
  stage: test
  script:
    - php -dxdebug.mode=coverage vendor/bin/phpunit --coverage-text --colors=never
  only:
    - merge_requests

# Testing jobs
php 8:
  image: mileschou/xdebug:8.0
  <<: *testing

php 7.4:
  image: mileschou/xdebug:7.4
  <<: *testing

php 7.3:
  image: mileschou/xdebug:7.3
  <<: *testing


#  Publish to package registry
publish:
  only:
    - master
    - tags
  # job stuff

So my pipeline works as follow:

  • on the feature branch
    • download dependancies
    • lint code
    • test against php 7.3, 7.4 and 8.0
  • on the main branch and on tags
    • publish the package to the packages registry

So the part executed on the merge request is as follow

Moreover, i can see code coverage for each test job in the pipeline job section

(I'm not sure why the tests results in different code coverage percentages since the codebase is the same, but is obviously related to the php version and probably specific optimizations done in code parsing.)

The badge

Since this is a Laravel package project i'm testing it with PhpUnit, so i used the given regex for the test coverage parsing setting (taken from the Gitlab docs).

^\s*Lines:\s*\d+.\d+\%

I want a badge that show the code coverage status on the master branch, not on the feature branch, so i've created a coverage badge with the badge image url as follow (i added a reference to a specific test job, i.e. php 7.4)

https://gitlab.com/%{project_path}/badges/%{default_branch}/coverage.svg?job=php%207.4

but it keeps being displayed as

.

I've tought that Gitlab can't find the desired php 7.4 job since it is not executed on the master branch but on a feature branch, so i tried replacing the %{default_branch} placeholder with the %{commit_sha} to reference a specific commit, but this doesn't work neither since (i suppose) it reference the merge commit (that triggered just the publish job) and not the one that triggered the entire pipeline.

I'm stuck

I cannot figure out how to get the code coverage percentage for the master branch.

I don't want to execute testing again on the master branche since i've already tested the feature branch before merging it.

How can i do? Am I missing something?

解决方案

I had the same issue in a different stack (Java, Gradle, and Jacoco). I've already solved the issue, and I will share what I've done. I hope my solution gives you some idea.

First, In your project, go to Settings > CI/CD and expand the General pipelines section. Fill the Test coverage parsing textbox with an appropriate regular expression based on the tool you are using. You can find the sample list of these regular expressions on this page: Test coverage parsing samples

Gitlab expects that the test runner tool will print the test coverage percent based on the given regular expression somewhere on the CI job's log. If the test runner engine doesn't print it to the console, you can do it manually. For instance:

test:
  stage: test
  script:
    - gradle test jacocoTestReport --info
    # Print test coverage to console
    - cat build/reports/jacoco/test/html/index.html | grep -o 'Total[^%]*%'
  artifacts:
    paths:
      - build/reports/jacoco/test/*

After making sure that the test coverage exists in the log, the badge will work perfectly.

pipeline and coverage badge sample

这篇关于Gitlab Coverage徽章永远不知道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆