feat: Add coverage reporting and analysis workflows, enhance CI with additional tests for older Go versions

This commit is contained in:
ProtoTess
2025-12-01 00:44:34 +00:00
parent 4f3e2a6df0
commit 856f49c82d
12 changed files with 334 additions and 66 deletions
+148 -27
View File
@@ -2,17 +2,120 @@ name: CI
on:
push:
branches: [ master ]
branches: [ master, main, develop ]
pull_request:
branches: [ master ]
branches: [ master, main, develop ]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
test:
name: Test
# Quick validation - fail fast on obvious issues
validate:
name: Quick Validation
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
- name: Cache Go modules
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Download dependencies
run: go mod download && go mod verify
- name: Check formatting
run: |
if [ "$(gofmt -s -l . | grep -v vendor | wc -l)" -gt 0 ]; then
echo "Code formatting issues found:"
gofmt -s -d . | grep -v vendor
exit 1
fi
- name: Lint
uses: golangci/golangci-lint-action@v4
with:
version: latest
args: --timeout=5m --fix=false
# Test on primary Go version
test:
name: Test (Go 1.23)
runs-on: ubuntu-latest
needs: validate
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
- name: Cache Go modules
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-1.23-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-1.23-
- name: Download dependencies
run: go mod download
- name: Run tests with coverage
run: go test -v -race -covermode=atomic -coverprofile=coverage.out ./...
- name: Generate coverage report
run: go tool cover -html=coverage.out -o coverage.html
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.out
flags: unittests
name: codecov-umbrella
fail_ci_if_error: true
- name: Archive coverage
if: always()
uses: actions/upload-artifact@v4
with:
name: coverage-report
path: |
coverage.out
coverage.html
retention-days: 30
# Test on multiple Go versions (after primary test passes)
test-matrix:
name: Test (Go ${{ matrix.go-version }})
runs-on: ${{ matrix.os }}
needs: test
strategy:
fail-fast: true # Stop on first failure
matrix:
go-version: ['1.21', '1.22', '1.23']
os: [ubuntu-latest, macos-latest, windows-latest]
go-version: ['1.21', '1.22']
exclude:
- os: macos-latest
go-version: '1.21'
- os: windows-latest
go-version: '1.21'
steps:
- name: Checkout code
@@ -26,46 +129,53 @@ jobs:
- name: Cache Go modules
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
path: |
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
${{ runner.os }}-go-${{ matrix.go-version }}-
- name: Download dependencies
run: go mod download
- name: Run tests
run: go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
- name: Upload coverage
uses: codecov/codecov-action@v4
with:
file: ./coverage.txt
flags: unittests
name: codecov-umbrella
run: go test -v -race ./...
lint:
name: Lint
# Code quality - only run if tests pass
sonarcloud:
name: Code Quality (SonarCloud)
runs-on: ubuntu-latest
needs: test
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.23'
fetch-depth: 0
- name: Run golangci-lint
uses: golangci/golangci-lint-action@v8
- name: Download coverage from test job
uses: actions/download-artifact@v4
with:
version: v2.2
args: --timeout=5m ./cmd/onvif-cli ./cmd/onvif-quick ./cmd/onvif-server ./discovery/... ./internal/... .
name: coverage-report
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.projectKey=0x524a_go-onvif
-Dsonar.organization=0x524a
-Dsonar.go.coverage.reportPaths=coverage.out
# Build verification
build:
name: Build
runs-on: ubuntu-latest
needs: test
steps:
- name: Checkout code
@@ -76,7 +186,18 @@ jobs:
with:
go-version: '1.23'
- name: Build
- name: Cache Go modules
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-1.23-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-1.23-
- name: Download dependencies
run: go mod download
- name: Build main packages
run: go build -v ./...
- name: Build examples
+42
View File
@@ -0,0 +1,42 @@
name: Additional Coverage Reports
on:
workflow_run:
workflows: [CI]
types: [completed]
branches: [master, main]
jobs:
# Generate additional coverage analysis if CI passed
coverage-analysis:
name: Coverage Analysis
runs-on: ubuntu-latest
if: github.event.workflow_run.conclusion == 'success'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download artifacts
uses: actions/download-artifact@v4
with:
name: coverage-report
- name: Check coverage percentage
run: |
if [ -f coverage.out ]; then
coverage=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
echo "Coverage: $coverage%"
# Set threshold to 40%
if (( $(echo "$coverage < 40" | bc -l) )); then
echo "⚠️ Coverage below 40% threshold: $coverage%"
else
echo "✅ Coverage above threshold: $coverage%"
fi
fi
- name: Upload coverage badge
continue-on-error: true
run: |
# Optional: Update badges or notifications
echo "Coverage analysis complete"
+40
View File
@@ -0,0 +1,40 @@
name: Extra Tests
on:
workflow_dispatch: # Manual trigger only
schedule:
- cron: '0 2 * * *' # Daily at 2 AM UTC
jobs:
# Run tests on other Go versions as manual/scheduled job
test-older-versions:
name: Test on Go ${{ matrix.go-version }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: [ubuntu-latest]
go-version: ['1.20', '1.19']
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
- name: Cache Go modules
uses: actions/cache@v4
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go-version }}-
- name: Download dependencies
run: go mod download
- name: Run tests
run: go test -v -race ./...