Go のカバレッジ計測結果を GitHub Pages にデプロイする

github.com

coveralls や codecov を使わずにローカルでもカバレッジを見たい、ついでにそれをそのまま GitHub Pages をつかって見れるようにしたいと思ったのでやってみたログ。

Go 標準機能のカバレッジレポートをデプロイする

Go の標準機能の範疇でやる設定は以下のよう

site ディレクトリに HTML を生成して、それをそのまま GitHub Pages に持っていく

# Makefile
.PHONY: coverage
coverage:
  go test -cover ./... -coverprofile=coverage.out
  go tool cover -html=coverage.out -o site/gocoverage.html
# .github/workflows/pages.yml
name: GitHub Pages

on:
  push

jobs:
  deploy:
    runs-on: ubuntu-20.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: 1.18

      - name: coverage
        run: make coverage

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./site

実際に生成されるページはこんな感じ

https://goropikari.github.io/lcov_pages_sample/gocoverage.html

LCOV を使う

Go 標準のカバレッジツールで生成される HTML は見やすくないので LCOV を使うようにする場合は以下のよう

# Makefile
SHELL = /bin/bash

ROOT_DIR = $(shell pwd)
GOBIN = $(ROOT_DIR)/bin
export PATH := $(ROOT_DIR)/bin:$(PATH)

.PHONY: tools
tools:
  GOBIN=$(GOBIN) go install github.com/jandelgado/gcov2lcov@v1.0.5

.PHONY: coverage
coverage:
  # lcov
  go test -cover ./... -coverprofile=coverage.out
  bin/gcov2lcov -infile=coverage.out -outfile=coverage.lcov
  genhtml coverage.lcov -o site
  # go coverage
  go tool cover -html=coverage.out -o site/gocoverage.html
name: GitHub Pages

on:
  push

jobs:
  deploy:
    runs-on: ubuntu-20.04
    concurrency:
      group: ${{ github.workflow }}-${{ github.ref }}
    steps:
      - uses: actions/checkout@v3

      - name: Set up Go
        uses: actions/setup-go@v3
        with:
          go-version: 1.18

      - name: lcov
        run: sudo apt-get update && sudo apt-get install -y lcov

      - name: tools
        run: make tools

      - name: coverage
        run: |
          percent=$(make coverage | grep lines | sed -r 's/[^0-9]*(.*\.[0-9]*)%.*/\1/' | sed -e 's/%/%25/')
          int=${percent%.*}
          if [ $int -gt 90 ]; then
            curl -o site/coverage.svg https://img.shields.io/badge/coverage-${percent}%25-green
          elif [ $int -gt 75 ]; then
            curl -o site/coverage.svg https://img.shields.io/badge/coverage-${percent}%25-yellow
          else
            curl -o site/coverage.svg https://img.shields.io/badge/coverage-${percent}%25-red
          fi

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./site

実際に生成されるページはこんな感じ

https://goropikari.github.io/lcov_pages_sample/index.html

README に貼り付ける用のこんな badge coverage も欲しかったので shields.io から画像をダウンロードして、それを GitHub Pages 用の branch に一緒にデプロイしてます。

↓のように raw content のリンクを README に貼り付けておけばとりあえずそれっぽくなります。

[![coverage](https://raw.githubusercontent.com/goropikari/lcov_pages_sample/gh-pages/coverage.svg)](https://goropikari.github.io/lcov_pages_sample/)