我有一个托管在 GitLab 上的项目,该项目已经具有有效的 CI 配置。我想添加“最小代码覆盖率”的概念。
我想要的是强制一个正增量(合并请求的代码覆盖率必须大于目标分支的代码覆盖率,除非它已经是 100%)。
我会接受“最低覆盖率 80%”之类的规则,但我确信我可以做得更好。
除了关键字之外,我在文档中找不到任何内容coverage
抓取覆盖范围以显示在主页上。
我想要这样的东西.gitlab-ci.yml
:
check_coverage:
stage: test
rules:
- if: '$CI_OPEN_MERGE_REQUESTS && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH'
script:
- compare-coverage.sh $CI_CURRENT_COVERAGE $CI_TARGET_COVERAGE # something that fails if delta < 0
我怎样才能做到这一点?
我是这样做的:
# .gitlab-ci.yml
image: python:3.9
stages:
- tests
- min-coverage
variables:
MIN_COVERAGE: 80 # set the threshold at 80%
unit-test:
stage: tests
before_script:
- pip install pytest pytest-cov
script:
- coverage run -m pytest
- coverage json
- coverage report -m
artifacts:
paths:
- coverage.json
coverage: '/TOTAL.+ ([0-9]{1,3}%)/'
min-coverage-test:
stage: min-coverage
before_script:
- apt-get update && apt-get install jq -y
script:
- jsonStr="$(cat $CI_PROJECT_DIR/coverage.json)"
- coverage="$(echo $jsonStr | jq .totals.percent_covered)"
- if (( $(echo "$coverage < $MIN_COVERAGE" |bc -l) )) ; then echo "$coverage% of code coverage below threshold of $MIN_COVERAGE%" && exit 1 ; else exit 0 ; fi
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)