【问题标题】:Make the build failure when code coverage is less than x%当代码覆盖率小于 x% 时使构建失败
【发布时间】:2021-05-20 14:35:54
【问题描述】:

我正在使用 github 操作,在我的测试中,当我的代码覆盖率低于 80% 时,我需要使 myt 构建失败。我在 github 市场上查找了一些 github 操作,但没有找到任何东西。我可以做吗 ?如果有帮助,我将链接我的工作流文件

---
name: lint build and test
on:
  push:
    branches: [master]
  pull_request:
    branches: [master]

jobs:
  build-and-test:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v2

      - name: Set up Go
        uses: actions/setup-go@v2
        with:
          go-version: 1.15
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}  

      - name: Super-Linter
        uses: github/super-linter@v3.14.0
        env:
          VALIDATE_GO: false
          VALIDATE_JSCPD: false
          VALIDATE_ALL_CODEBASE: true
          DEFAULT_BRANCH: master          
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  

      - name: golangci-lint
        uses: golangci/golangci-lint-action@v2
        with:
          version: v1.29

      - name: Build
        run: go build -o apiDateTime -v ./...    

      - name: Test 
        run: go test ./... -coverprofile cover.out -covermode atomic

      - name: Coverage result
        run: go tool cover -func cover.out

【问题讨论】:

    标签: go devops github-actions


    【解决方案1】:

    我会将go test 的调用替换为调用shell 脚本,如here 所述。

    shell 脚本看起来像这样

    !#/bin/sh
    
    set -e -u
    
    go test ./... -coverprofile cover.out -covermode atomic
    
    perc=`go too cover -func=cover.out | tail -n 1 | sed -Ee 's!^[^[:digit:]]+([[:digit:]]+(\.[[:digit:]]+)?)%$!\1!'`
    res=`echo "$perc >= 80.0" | bc`
    test "$res" -eq 1 && exit 0
    echo "Insufficient coverage: $perc" >&2
    exit 1
    

    地点:

    1. 涉及sed 的咒语提取覆盖百分比(请参阅here)。
    2. 下一行要求计算器将百分比与您配置的阈值进行比较。
    3. 如果测试通过,则下一行使脚本成功退出。
    4. 如果不满足覆盖要求,脚本的其余部分就会崩溃。

    此脚本需要 bc tool 安装在 ubuntu-latest 包中(我不知道)。
    如果不是,则可以使用映像中可用的任何语言(例如 Perl 或 Python)编写整个脚本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多