11name : Go
22
3+ permissions :
4+ contents : write
5+
36on :
47 push :
58 branches : [main]
@@ -26,17 +29,83 @@ jobs:
2629 run : go build -v ./...
2730
2831 - name : Test
29- run : go test -bench=. -coverprofile=coverage.txt ./...
32+ run : go test -bench=. -coverprofile=coverage.out ./...
3033
3134 - name : Run Gosec Security Scanner
3235 uses : securego/gosec@master
3336 with :
3437 args : ./...
3538
36- - name : actions-goveralls
37- uses : shogo82148/actions-goveralls@v1
38- with :
39- path-to-profile : coverage.txt
40-
4139 - name : Go report card
4240 uses : creekorful/goreportcard-action@v1.0
41+
42+ # Generate code coverage badge and push it to the 'coverage' branch.
43+ # Reference it in your README.md like this:
44+ #
45+ # [](#)
46+ #
47+ # If you have a detailed HTML report of the coverage (here called report.html), replace the '#' with:
48+ #
49+ # https://htmlpreview.github.io/?https://github.com/USERNAME/REPO/blob/coverage/BRANCH/report.html
50+ #
51+ # You need to have given write permissions for the for the workflow:
52+ #
53+ # permissions:
54+ # contents: write
55+ #
56+ # To create the 'coverage' branch for the repository (replace 'main' with the branch you want to publish for):
57+ #
58+ # git checkout main
59+ # git checkout --orphan coverage && git rm --cached $(git ls-files) && echo '# Coverage branch' > README.md
60+ # git add README.md && git commit -m 'Add README.md' && git push origin coverage
61+ # git checkout --force main
62+ - name : Extract branch name
63+ run : echo "BRANCH=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
64+ id : extract_branch
65+ - uses : actions/checkout@v3
66+ with :
67+ ref : coverage
68+ path : coverage
69+ - name : Ensure directories for this branch exist
70+ env :
71+ BRANCH : ${{ steps.extract_branch.outputs.branch }}
72+ run : mkdir -p coverage/${BRANCH}
73+ - name : Extract code coverage
74+ id : coverage
75+ env :
76+ BRANCH : ${{ steps.extract_branch.outputs.branch }}
77+ # Change this to extract the coverage percentage (without the percent sign) for your language into COVERAGE.
78+ # Here we are using a 'coverage.out' file generated by 'go test -coverprofile=coverage.out ./...'
79+ # You may also generate a HTML report and place at 'coverage/${BRANCH}/report.html'.
80+ run : |
81+ echo "COVERAGE=$(go tool cover -func=coverage.out | tail -n 1 | tr -s '\t' | cut -f 3 | rev | cut -c2- | rev)" >> $GITHUB_OUTPUT
82+ go tool cover -html=coverage.out -o=coverage/${BRANCH}/report.html
83+ - name : Generate the badge SVG image
84+ uses : emibcn/badge-action@v2.0.3
85+ with :
86+ label : ' coverage'
87+ status : ${{ steps.coverage.outputs.coverage }}%
88+ path : coverage/${{ steps.extract_branch.outputs.branch }}/badge.svg
89+ color : ${{
90+ steps.coverage.outputs.coverage > 95 && 'green' ||
91+ steps.coverage.outputs.coverage > 90 && 'yellow,green,green' ||
92+ steps.coverage.outputs.coverage > 80 && 'yellow,green' ||
93+ steps.coverage.outputs.coverage > 70 && 'yellow' ||
94+ steps.coverage.outputs.coverage > 60 && 'orange,yellow' ||
95+ steps.coverage.outputs.coverage > 50 && 'orange' ||
96+ steps.coverage.outputs.coverage > 40 && 'red,orange' ||
97+ steps.coverage.outputs.coverage > 30 && 'red,red,orange' ||
98+ steps.coverage.outputs.coverage > 20 && 'red,red,red,orange' ||
99+ ' red' }}
100+ - name : Commit coverage files
101+ env :
102+ BRANCH : ${{ steps.extract_branch.outputs.branch }}
103+ run : |
104+ pushd coverage
105+ git config --local user.email "action@github.com"
106+ git config --local user.name "GitHub Action"
107+ test ! -f "${BRANCH}/badge.svg" || git add "${BRANCH}/badge.svg"
108+ test ! -f "${BRANCH}/report.html" || git add "${BRANCH}/report.html"
109+ test -z "$(git status --porcelain)" || git commit -m "update"
110+ git push
111+ popd
0 commit comments