如何在github上创建具有测试覆盖率(jacoco)的徽章(动作)

在春季项目中,我使用jacoco插件来测量测试覆盖率。

我看到如下所示的html报告:

如何在github上创建具有测试覆盖率(jacoco)的徽章(动作)

现在,我想以这样的百分比向github项目添加徽章:

如何在github上创建具有测试覆盖率(jacoco)的徽章(动作)

有什么想法可以将jacoco与github动作结合起来吗?

lzq0809 回答:如何在github上创建具有测试覆盖率(jacoco)的徽章(动作)

您可以使用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

1)推送到它从中提取的同一分支

    - 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开始的。

2)推送到专用分支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' }}

更新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,orange' ||
          'red' }}
,

您可以使用codecov作为support every CI提供者。

您将需要两件事:

创建帐户并有权访问令牌后,将令牌作为secret存储在github操作中。称为bootstrap.min.xyz789.js

在您的工作流程中,创建一个看起来像这样的步骤,并根据需要configure

CODECOV_TOKEN

See example workflow

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

- name: Upload coverage to Codecov  
  uses: codecov/codecov-action@v1
    with:
      token: ${{ secrets.CODECOV_TOKEN }}

来源:Integrating Codecov with a GitHub project

,

作为持续集成构建的一部分,您需要将覆盖范围统计信息发布到Coveralls等服务。 CI服务器(例如CircleCITravisCI)已内置对Github和Coveralls的支持

您的项目是开源的吗? Coveralls,Travis和CircleCI都是免费的开放源代码。 github触发CI并将其发布以覆盖所有人后,您可以在您的readme.md中嵌入一个图像标签。

本文链接:https://www.f2er.com/2966773.html

大家都在问