Skip to content

Commit 4c41737

Browse files
author
Dima Koss
committed
add pipeline to generage the coverage badge attempt 2
1 parent dc71715 commit 4c41737

1 file changed

Lines changed: 45 additions & 27 deletions

File tree

.github/workflows/coverage-badge.yml

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,81 @@ name: Coverage badge
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [ master ]
66
pull_request:
77

8+
env:
9+
COVERAGE_FILE: coverage.out
10+
BADGE_FILE: coverage.svg
11+
812
jobs:
913
test-and-badge:
14+
if: github.actor != 'github-actions[bot]'
1015
runs-on: ubuntu-latest
16+
1117
steps:
1218
- name: Checkout
1319
uses: actions/checkout@v4
1420
with:
1521
fetch-depth: 0
1622

23+
- name: Skip if commit message requests it
24+
run: |
25+
if git log -1 --pretty=%B | grep -q '$$skip badge$$'; then
26+
echo "Found [skip badge] in commit message — exiting"
27+
exit 0
28+
fi
29+
1730
- name: Set up Go
1831
uses: actions/setup-go@v4
1932
with:
2033
go-version: '1.21'
2134

2235
- name: Install gopherbadger
2336
run: |
24-
go install github.com/mitterst/gopherbadger@latest
25-
echo "$GOPATH/bin"
37+
GO_BIN="$(go env GOPATH)/bin"
38+
mkdir -p "$GO_BIN"
39+
GOBIN="$GO_BIN" go install github.com/mitterst/gopherbadger@latest
40+
echo "GOBIN=$GO_BIN"
2641
env:
2742
GO111MODULE: on
2843

29-
- name: Run tests with coverage (all packages)
44+
- name: Run tests and produce coverage
3045
run: |
31-
# create a combined coverage file for all packages
32-
rm -f coverage.out
33-
go test ./... -covermode=count -coverprofile=coverage.tmp || true
34-
# merge per-package profiles if present (this merges multiple coverage.tmp files produced by packages)
35-
if [ -f coverage.tmp ]; then
36-
mv coverage.tmp coverage.out
37-
fi
38-
# normalize: ensure coverage.out exists for gopherbadger
39-
if [ ! -f coverage.out ]; then
40-
echo "mode: count" > coverage.out
46+
rm -f $COVERAGE_FILE
47+
# run tests for all packages and produce a single profile
48+
# use a temp file and merge if multiple packages produce files
49+
set -euo pipefail
50+
# Try a simple go test for all packages; if it fails for some packages, still produce profile
51+
go test ./... -covermode=count -coverprofile=profile.tmp || true
52+
if [ -f profile.tmp ]; then
53+
# ensure header only once
54+
awk 'NR==1{print; next} /^mode: /{next} {print}' profile.tmp > $COVERAGE_FILE
55+
rm -f profile.tmp
56+
else
57+
echo "mode: count" > $COVERAGE_FILE
4158
fi
4259
4360
- name: Generate coverage badge (SVG)
4461
run: |
45-
# Produce badge.svg and update README.md if desired
46-
gopherbadger -coverprofile=coverage.out -svg=coverage.svg -md="README.md"
62+
GO_BIN="$(go env GOPATH)/bin"
63+
PATH="$GO_BIN:$PATH" gopherbadger -coverprofile=$COVERAGE_FILE -svg=$BADGE_FILE -md="README.md"
4764
env:
48-
PATH: ${{ env.PATH }}:${{ github.workspace }}/go/bin
65+
PATH: ${{ runner.temp }}
4966

5067
- name: Commit and push coverage badge
5168
if: ${{ github.ref == 'refs/heads/main' }}
69+
env:
70+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5271
run: |
5372
git config user.name "github-actions[bot]"
54-
git config user.email "github-actions[bot]@users.noreply.github.com"
55-
git add coverage.svg README.md || true
56-
# only commit when there are changes
57-
if ! git diff --staged --quiet; then
58-
git commit -m "chore: update coverage badge"
59-
git push
60-
else
61-
echo "No changes to commit"
73+
git.config_email="github-actions[bot]@users.noreply.github.com"
74+
git config user.email "$git.config_email"
75+
git add $BADGE_FILE README.md || true
76+
if git diff --staged --quiet; then
77+
echo "No badge changes to commit"
78+
exit 0
6279
fi
63-
env:
64-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
80+
git commit -m "chore: update coverage badge [skip badge]"
81+
git push origin HEAD:main
82+

0 commit comments

Comments
 (0)