Skip to content

Commit 8dafa4b

Browse files
authored
chore: add test coverage in PRs as comment (#884)
* generate test coverage in CI pipeline * add pipeline which adds comments in PR with test coverage
1 parent f435d5d commit 8dafa4b

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

.github/workflows/ci.yaml

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
name: CI Workflow
22

3-
on: [pull_request, workflow_dispatch]
3+
on:
4+
pull_request:
5+
workflow_dispatch:
6+
push:
7+
branches:
8+
- main
49

510
env:
611
GO_VERSION: "1.24"
12+
CODE_COVERAGE_FILE_NAME: "coverage.out" # must be the same as in Makefile
13+
CODE_COVERAGE_ARTIFACT_NAME: "code-coverage"
714

815
jobs:
916
main:
@@ -33,8 +40,17 @@ jobs:
3340
- name: Test
3441
run: make test
3542

43+
44+
- name: Archive code coverage results
45+
uses: actions/upload-artifact@v4
46+
with:
47+
name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
48+
path: "stackit/${{ env.CODE_COVERAGE_FILE_NAME }}"
49+
50+
3651
config:
3752
name: Check GoReleaser config
53+
if: github.event_name == 'pull_request'
3854
runs-on: ubuntu-latest
3955
steps:
4056
- name: Checkout
@@ -44,3 +60,20 @@ jobs:
4460
uses: goreleaser/goreleaser-action@v6
4561
with:
4662
args: check
63+
64+
code_coverage:
65+
name: "Code coverage report"
66+
if: github.event_name == 'pull_request' # Do not run when workflow is triggered by push to main branch
67+
runs-on: ubuntu-latest
68+
needs: main
69+
permissions:
70+
contents: read
71+
actions: read # to download code coverage results from "main" job
72+
pull-requests: write # write permission needed to comment on PR
73+
steps:
74+
- name: Check new code coverage
75+
uses: fgrosse/go-coverage-report@v1.2.0
76+
with:
77+
coverage-artifact-name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
78+
coverage-file-name: ${{ env.CODE_COVERAGE_FILE_NAME }}
79+
root-package: 'github.com/stackitcloud/terraform-provider-stackit'

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fmt:
3535
# TEST
3636
test:
3737
@echo "Running tests for the terraform provider"
38-
@cd $(ROOT_DIR)/stackit && go test ./... -count=1 && cd $(ROOT_DIR)
38+
@cd $(ROOT_DIR)/stackit && go test ./... -count=1 -coverprofile=coverage.out && cd $(ROOT_DIR)
3939

4040
# Test coverage
4141
coverage:

0 commit comments

Comments
 (0)