Skip to content

Commit cc6b65f

Browse files
committed
feat(ci): add code coverage reports on PRs
relates to STACKITTPR-162
1 parent 2f3adf4 commit cc6b65f

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

.github/workflows/ci.yaml

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ on: [pull_request, workflow_dispatch]
44

55
env:
66
GO_VERSION: "1.24"
7+
CODE_COVERAGE_FILE_NAME: "coverage.out" # must be the same as in Makefile
8+
CODE_COVERAGE_ARTIFACT_NAME: "code-coverage"
79

810
jobs:
911
main:
@@ -29,10 +31,33 @@ jobs:
2931

3032
- name: Lint
3133
run: make lint
32-
34+
35+
# note: test make task creates the coverage.out file
3336
- name: Test
3437
run: make test
3538

39+
- name: Upload code coverage results
40+
uses: actions/upload-artifact@v4
41+
with:
42+
name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
43+
path: ${{ env.CODE_COVERAGE_FILE_NAME }}
44+
45+
code_coverage:
46+
name: "Code coverage report"
47+
if: ${{ github.event_name == 'pull_request' }}
48+
runs-on: ubuntu-latest
49+
needs: [main]
50+
permissions:
51+
contents: read
52+
actions: read # to download code coverage results from "test" job
53+
pull-requests: write # write permission needed to comment on PR
54+
steps:
55+
- name: Add code coverage report comment to PR
56+
uses: fgrosse/go-coverage-report@v1.2.0
57+
with:
58+
coverage-artifact-name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
59+
coverage-file-name: ${{ env.CODE_COVERAGE_FILE_NAME }}
60+
3661
config:
3762
name: Check GoReleaser config
3863
runs-on: ubuntu-latest

CONTRIBUTION.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ These commands can be executed from the project root:
2525
- `make project-tools`: get the required dependencies
2626
- `make lint`: lint the code and examples
2727
- `make generate-docs`: generate terraform documentation
28-
- `make test`: run unit tests
29-
- `make coverage`: create unit test coverage report (output file: `stackit/coverage.html`)
28+
- `make test`: run unit tests and create unit test coverage report (output file: `stackit/coverage.html`)
3029
- `make test-acceptance-tf`: run acceptance tests
3130

3231
### Repository structure

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +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)
39-
40-
# Test coverage
41-
coverage:
42-
@echo ">> Creating test coverage report for the terraform provider"
43-
@cd $(ROOT_DIR)/stackit && (go test ./... -count=1 -coverprofile=coverage.out || true) && cd $(ROOT_DIR)
38+
@cd $(ROOT_DIR)/stackit && (go test ./... -count=1 -coverprofile=coverage.out) && cd $(ROOT_DIR)
4439
@cd $(ROOT_DIR)/stackit && go tool cover -html=coverage.out -o coverage.html && cd $(ROOT_DIR)
4540

4641
test-acceptance-tf:

0 commit comments

Comments
 (0)