在春季项目中,我使用jacoco插件来测量测试覆盖率。
我看到如下所示的html报告:
现在,我想以这样的百分比向github项目添加徽章:
有什么想法可以将jacoco与github动作结合起来吗?
答案 0 :(得分:5)
您可以使用GitHub动作通过GitHub Workflow生成徽章(不需要其他服务器)。您可以编写自己的工作/步骤或使用我刚发布的动作:https://github.com/marketplace/actions/badge-action。
首先,您需要分析coverage结果文件并提取值(示例中的81
)。在这里,我以parse-coverage-report
作为示例命令(您需要自己创建)。最后,将此值另存为GitHub工作流输出:
on: [push]
jobs:
coverage:
runs-on: ubuntu-latest
name: Generate test coverage badge
steps:
- name: Generate a coverage value
id: coverage
# Generates a GitHub Workflow output named `lines`
run: |
COVERAGE="$( parse-covergae-report )"
echo "##[set-output name=lines;]${COVERAGE}%"
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action@v1
with:
label: 'Test coverage'
status: ${{ steps.coverage.outputs.lines }}
color: 'blue,555,daf'
path: badge.svg
这会将徽章保存为文件badge.svg
。现在,您决定是否将此徽章上传到同一存储库,S3或任何您喜欢的文件。作为覆盖报告,我想您想将其上传到相同的仓库1)从中提取的同一分支或2)专用分支badges
:
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
- uses: actions/checkout@v1
with:
ref: ${{ steps.extract_branch.outputs.branch }}
- name: Commit badge
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add badge.svg
git commit -m "Add/Update badge"
- name: Push badge commit
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: ${{ steps.extract_branch.outputs.branch }}
extract_branch
步骤是从https://stackoverflow.com/a/58035262/2928168开始的。
badges
首先,使用(从StackOverflow中提取)创建并推送专用分支badges
:
git checkout master
# Use a fresh start
git checkout --orphan badges
# Unstage all the files in your working tree.
git rm --cached $(git ls-files)
# Create a dedicated README file, so it's clear what's going on here
echo '# Badges branch' > README.md
git add README.md
git commit -m 'Add dedicated README'
git push origin badges
- name: Extract branch name
shell: bash
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
id: extract_branch
- uses: actions/checkout@v1
with:
ref: 'badges'
- name: Commit badge
env:
BRANCH: ${{ steps.extract_branch.outputs.branch }}
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
mkdir -p "${BRANCH}"
mv badge.svg "${BRANCH}"
git add "${BRANCH}/badge.svg"
git commit -m "Add/Update badge"
- name: Push badge commit
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: badges
如果coverage报告是典型的三叶草coverage.xml
文件,则可以使用this action解析并输出coverage值。例如:
- name: Check test coverage
uses: johanvanhelden/gha-clover-test-coverage-check@v1
id: coverage
with:
percentage: "50"
filename: "coverage.xml"
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action@v1
id: badge
with:
label: 'Coverage'
status: ${{ steps.coverage.outputs.coverage }}
path: ./badges/test-coverage.svg
color: ${{ steps.coverage.outputs.coveragelines > 75 && 'green' || 'red' }}
您甚至可以使用渐变使徽章根据覆盖范围值更改其背景颜色:
# Use the output from the `coverage` step
- name: Generate the badge SVG image
uses: emibcn/badge-action@v1
id: badge
with:
label: 'Coverage'
status: ${{ steps.coverage.outputs.coverage }}
path: ./badges/test-coverage.svg
color: ${{
steps.coverage.outputs.coverage > 90 && 'green' ||
steps.coverage.outputs.coverage > 80 && 'yellow,green' ||
steps.coverage.outputs.coverage > 70 && 'yellow' ||
steps.coverage.outputs.coverage > 60 && 'orange,yellow' ||
steps.coverage.outputs.coverage > 50 && 'orange' ||
steps.coverage.outputs.coverage > 40 && 'red,orange' ||
steps.coverage.outputs.coverage > 30 && 'red,red,orange' ||
steps.coverage.outputs.coverage > 20 && 'red,red,red,orange' ||
'red' }}
答案 1 :(得分:3)
您可以使用codecov作为support every CI提供者。
您将需要两件事:
创建帐户并有权访问令牌后,将令牌作为secret存储在github操作中。称为bootstrap.min.xyz789.js
。
在您的工作流程中,创建一个看起来像这样的步骤,并根据需要configure:
CODECOV_TOKEN
在自述文件中,使用以下格式创建状态标志:
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
答案 2 :(得分:1)