gradle 如何在github上创建一个测试覆盖率(jacoco)的徽章(actions)

vmdwslir  于 2023-04-30  发布在  Git
关注(0)|答案(3)|浏览(237)

在spring项目中,我使用jacoco插件来测量测试覆盖率。
我看到HTML报告如下:

现在我想用这个百分比为github项目添加一个徽章,如下所示:

我如何将jacoco与github操作结合起来?

taor4pac

taor4pac1#

您可以使用codecov,因为它们支持每个CI提供程序。
你需要两样东西:

创建帐户并访问令牌后,将令牌存储为github action中的secret。称之为CODECOV_TOKEN
在您的工作流中,创建一个类似于以下内容的步骤,并根据需要进行配置:

- name: Upload coverage to Codecov  
  uses: codecov/codecov-action@v1
    with:
      token: ${{ secrets.CODECOV_TOKEN }}
  • 请参阅示例工作流程 *

在您的自述文件中,使用以下格式创建状态标记:

[![codecov](https://codecov.io/gh/<your-name>/<project-name>/branch/master/graph/badge.svg)](https://codecov.io/gh/<your-name>/<project-name>)

来源:Integrating Codecov with a GitHub project

ffdz8vbo

ffdz8vbo2#

作为持续集成构建的一部分,您需要将覆盖统计数据发布到诸如Coveralls之类的服务。CircleCITravisCI等CI服务器内置了对Github和Coveralls的支持
你的项目是开源的吗?Coveralls、Travis和CircleCI都是免费的开源软件。一旦你在github上触发了CI并发布到工作服上,你就可以在你的www.example中嵌入一个图像标签 www.example.com

m1m5dgzv

m1m5dgzv3#

您可以使用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-coverage-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或任何您喜欢的存储库。由于这是一个覆盖率报告,我想你会喜欢上传到same的repo 1)它被提取的相同分支或2)专用分支badges

1)推送到解压缩分支

- name: Extract branch name
      shell: bash
      run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      id: extract_branch
    - name: Create badges dir if necessary
      run: mkdir -p .github/badges
    - 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: .github/badges/badge.svg
    - name: Commit badge
      run: |
        git config --local user.email "action@github.com"
        git config --local user.name "GitHub Action"
        git add .github/badges/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

2)推流到专用分支badges

首先,创建并推送专用分支badges(从StackOverflow中提取):

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
        path: badges
    - name: Create badges dir if necessary
      env:
        BRANCH: ${{ steps.extract_branch.outputs.branch }}
      run: mkdir -p badges/${BRANCH}
    - 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: badges/${{ steps.extract_branch.outputs.branch }}/badge.svg
    - name: Commit badge
      env:
        BRANCH: ${{ steps.extract_branch.outputs.branch }}
      run: |
        pushd badges
            git config --local user.email "action@github.com"
            git config --local user.name "GitHub Action"
            git add "${BRANCH}/badge.svg"
            git commit -m "Add/Update badge"
        popd
    - name: Push badge commit
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: badges
        directory: badges

更新

如果你的覆盖率报告是一个典型的三叶草coverage.xml文件,你可以使用this action来解析和输出覆盖率值。例如:

- 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' }}

更新2

您可以根据覆盖率值更改徽章的背景颜色,甚至可以使用渐变:

# 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' }}

更新3

更新了2个工作流:

  • 同一分支:将徽章保存到.github/badges/
  • 专用分支:使用工作流中的子目录来管理标记,以便工作流环境可用于进一步的步骤(例如,保存一些缓存)。

更新4:工作示例

您可以在一些存储库工作流中看到工作示例(通过编辑答案或评论来添加您的答案):

相关问题