Skip to content

Commit 6fbd874

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

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

.github/workflows/ci.yaml

Lines changed: 31 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,38 @@ 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: Move code coverage report
40+
if: ${{ github.event_name == 'pull_request' }}
41+
run: mv stackit/${{env.CODE_COVERAGE_FILE_NAME}} .
42+
43+
- name: Upload code coverage results
44+
if: ${{ github.event_name == 'pull_request' }}
45+
uses: actions/upload-artifact@v4
46+
with:
47+
name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
48+
path: ${{ env.CODE_COVERAGE_FILE_NAME }}
49+
50+
code_coverage:
51+
name: "Code coverage report"
52+
if: ${{ github.event_name == 'pull_request' }}
53+
runs-on: ubuntu-latest
54+
needs: [main]
55+
permissions:
56+
contents: read
57+
actions: read # to download code coverage results from "test" job
58+
pull-requests: write # write permission needed to comment on PR
59+
steps:
60+
- name: Add code coverage report comment to PR
61+
uses: fgrosse/go-coverage-report@v1.2.0
62+
with:
63+
coverage-artifact-name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }}
64+
coverage-file-name: ${{ env.CODE_COVERAGE_FILE_NAME }}
65+
3666
config:
3767
name: Check GoReleaser config
3868
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)