Refactor Go CI workflow for better structure and coverage #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Go CI / Test / Lint | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| pull_request: | |
| branches: [ "main" ] | |
| jobs: | |
| ci: | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| go-version: [1.20, 1.21] | |
| steps: | |
| # Checkout code | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| # Go setup | |
| - name: Setup Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: ${{ matrix.go-version }} | |
| # Cache modules and build cache | |
| - name: Cache Go build & modules | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.cache/go-build | |
| ~/go/pkg/mod | |
| key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
| restore-keys: | | |
| ${{ runner.os }}-go- | |
| # Ensure dependencies | |
| - name: Go mod tidy | |
| run: go mod tidy | |
| # Lint | |
| - name: Run golangci-lint | |
| uses: golangci/golangci-lint-action@v4 | |
| with: | |
| version: v1.59.0 | |
| args: run --timeout 5m | |
| # Build | |
| - name: Build code | |
| run: go build -v ./... | |
| # Test + coverage | |
| - name: Run tests with coverage | |
| run: | | |
| go test ./... -coverprofile=coverage.out -failfast -timeout=30s | |
| go tool cover -func=coverage.out | |
| # Upload coverage report (optional) | |
| - name: Upload coverage to Codecov | |
| if: github.event_name != 'pull_request' | |
| uses: codecov/codecov-action@v4 | |
| with: | |
| files: coverage.out |