From 5c2b53f4bcbed797e549c4a1938e3c5314e04ec1 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Fri, 31 Mar 2023 16:54:41 +0200 Subject: [PATCH] Add Codecov to GitHub actions --- .github/codecov.yml | 6 ++++++ .github/workflows/ci.yml | 30 +++++++++++++++++++++++++++--- .gitignore | 3 +++ Makefile | 25 +++++++++++++++++++++---- 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 .github/codecov.yml diff --git a/.github/codecov.yml b/.github/codecov.yml new file mode 100644 index 0000000..5634064 --- /dev/null +++ b/.github/codecov.yml @@ -0,0 +1,6 @@ +codecov: + notify: + after_n_builds: 2 + +ignore: + - "**/*_mock.go" diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a4e206a..55d23f7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -37,7 +37,7 @@ jobs: run: date +%s > ~/.cache/go-build/trim.txt continue-on-error: true - name: Build - run: make build + run: make build-cover - name: Upload Poseidon binary uses: actions/upload-artifact@v3 with: @@ -84,19 +84,26 @@ jobs: continue-on-error: true - name: Run tests run: make coverhtml + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v3 + if: ${{ success() || failure() }} + with: + token: ${{ secrets.CODECOV_TOKEN }} - name: Publish code coverage uses: paambaati/codeclimate-action@v3.2.0 + if: ${{ success() || failure() }} env: CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} with: prefix: github.com/openHPI/poseidon/ coverageLocations: | - ${{github.workspace}}/coverage_cleaned.cov:gocov + ${{github.workspace}}/coverage/coverage.cov:gocov - name: Upload coverage report uses: actions/upload-artifact@v3 + if: ${{ success() || failure() }} with: name: coverage - path: coverage_unit.html + path: coverage/coverage_unit.html dep-scan: runs-on: ubuntu-latest @@ -130,6 +137,7 @@ jobs: POSEIDON_AWS_ENDPOINT: ${{ secrets.POSEIDON_AWS_ENDPOINT }} POSEIDON_AWS_FUNCTIONS: "" POSEIDON_NOMAD_DISABLEFORCEPULL: true + GOCOVERDIR: coverage steps: - name: Checkout repository uses: actions/checkout@v3 @@ -204,6 +212,7 @@ jobs: sudo ./nomad agent -dev -log-level=WARN -config e2e-config.hcl & until curl -s --fail http://localhost:4646/v1/agent/health ; do sleep 1; done chmod +x ./poseidon + mkdir -p ${GOCOVERDIR} ./poseidon & until curl -s --fail http://localhost:7200/api/v1/health ; do sleep 1; done make e2e-test @@ -211,3 +220,18 @@ jobs: run: | killall poseidon make e2e-test-recovery + if: ${{ success() || failure() }} + - name: Convert coverage reports + run: make convert-run-coverage + if: ${{ success() || failure() }} + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v3 + if: ${{ success() || failure() }} + with: + token: ${{ secrets.CODECOV_TOKEN }} + - name: Upload coverage report + uses: actions/upload-artifact@v3 + if: ${{ success() || failure() }} + with: + name: coverage + path: coverage/coverage_run.html diff --git a/.gitignore b/.gitignore index 97c84f1..943451d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,9 @@ tests/e2e/configuration.yaml # trivy artifacts .trivy +# coverage reports +/coverage + # IDE files /.idea *.iml diff --git a/Makefile b/Makefile index d05082a..5758997 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ PROJECT_NAME = poseidon REPOSITORY_OWNER = openHPI PKG = github.com/$(REPOSITORY_OWNER)/$(PROJECT_NAME)/cmd/$(PROJECT_NAME) UNIT_TESTS = $(shell go list ./... | grep -v /e2e | grep -v /recovery) +GOCOVERDIR=coverage # Define the PGO file to be used for the build PGO_FILE = ./cmd/$(PROJECT_NAME)/default.pgo @@ -57,6 +58,10 @@ else @go build -o $(PROJECT_NAME) -v $(PKG) endif +.PHONY: build-cover +build-cover: deps ## Build the binary and with coverage support for e2e-tests + @go build -cover -o $(PROJECT_NAME) -v $(PKG) + .PHONY: clean clean: ## Remove previous build @rm -f poseidon @@ -97,18 +102,30 @@ race: deps ## Run data race detector .PHONY: coverage coverage: deps ## Generate code coverage report - @go test $(UNIT_TESTS) -v -coverprofile coverage.cov + @mkdir -p $(GOCOVERDIR) + @go test $(UNIT_TESTS) -v -coverprofile $(GOCOVERDIR)/coverage_output.cov -covermode atomic # exclude mock files from coverage - @cat coverage.cov | grep -v _mock.go > coverage_cleaned.cov || true - @go tool cover -func=coverage_cleaned.cov + @cat $(GOCOVERDIR)/coverage_output.cov | grep -v _mock.go > $(GOCOVERDIR)/coverage.cov || true + @rm $(GOCOVERDIR)/coverage_output.cov + @go tool cover -func=$(GOCOVERDIR)/coverage.cov .PHONY: coverhtml coverhtml: coverage ## Generate HTML coverage report - @go tool cover -html=coverage_cleaned.cov -o coverage_unit.html + @go tool cover -html=$(GOCOVERDIR)/coverage.cov -o $(GOCOVERDIR)/coverage_unit.html deploy/dockerfiles: ## Clone Dockerfiles repository @git clone git@github.com:$(REPOSITORY_OWNER)/dockerfiles.git deploy/dockerfiles +.PHONY: run-with-coverage +run-with-coverage: build-cover ## Run binary and capture code coverage (during e2e tests) + @mkdir -p $(GOCOVERDIR) + @GOCOVERDIR=$(GOCOVERDIR) ./$(PROJECT_NAME) + +.PHONY: convert-run-coverage +convert-run-coverage: ## Convert coverage data (created by `run-with-coverage`) to legacy text format + @go tool covdata textfmt -i $(GOCOVERDIR) -o $(GOCOVERDIR)/coverage_run.cov + @go tool cover -html=$(GOCOVERDIR)/coverage_run.cov -o $(GOCOVERDIR)/coverage_run.html + .PHONY: e2e-test-docker-image e2e-test-docker-image: deploy/dockerfiles ## Build Docker image that is used in e2e tests @docker build -t $(E2E_TEST_BASE_IMAGE) deploy/dockerfiles/$(E2E_TEST_BASE_CONTAINER)