updated badge logic #20
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: CI/CD | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| branches: [main] | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Type check | |
| run: npm run typecheck | |
| - name: Lint | |
| run: npm run lint | |
| - name: Check formatting | |
| run: npm run format:check | |
| - name: Run tests with coverage | |
| run: npm run test:coverage | |
| - name: Upload coverage reports | |
| uses: codecov/codecov-action@v4 | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| with: | |
| file: ./coverage/lcov.info | |
| flags: unittests | |
| name: codecov-umbrella | |
| fail_ci_if_error: false | |
| build: | |
| runs-on: ubuntu-latest | |
| needs: test | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Build | |
| run: npm run build | |
| - name: Test build | |
| run: npm run test:build | |
| - name: Upload build artifacts | |
| uses: actions/upload-artifact@v4 | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| with: | |
| name: build-artifacts | |
| path: lib/ | |
| retention-days: 30 | |
| update-coverage-badge: | |
| runs-on: ubuntu-latest | |
| needs: test | |
| if: github.event_name == 'push' && github.ref == 'refs/heads/main' | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: '20.x' | |
| cache: 'npm' | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Run tests with coverage | |
| run: npm run test:coverage | |
| - name: Generate coverage badge | |
| run: | | |
| # Calculate coverage for only tested files (files with at least one covered line) | |
| if [ -f coverage/lcov.info ]; then | |
| total_lines=0 | |
| covered_lines=0 | |
| # For each file section in lcov.info | |
| awk '/^SF:/{if (file) {if (file_total>0 && file_covered>0) {total+=file_total; covered+=file_covered}}; file=1; file_total=0; file_covered=0} /^DA:/{file_total++; if ($0 ~ /,1$/) file_covered++} END{if (file && file_total>0 && file_covered>0) {total+=file_total; covered+=file_covered}; print total, covered}' coverage/lcov.info | { | |
| read total covered | |
| if [ "$total" -gt 0 ]; then | |
| PERCENTAGE=$(( 100 * covered / total )) | |
| else | |
| PERCENTAGE=0 | |
| fi | |
| # Determine color based on coverage | |
| if [ $PERCENTAGE -ge 80 ]; then | |
| COLOR=green | |
| elif [ $PERCENTAGE -ge 60 ]; then | |
| COLOR=yellow | |
| else | |
| COLOR=red | |
| fi | |
| # Create badge URL | |
| BADGE_URL="https://img.shields.io/badge/coverage-${PERCENTAGE}%25-${COLOR}" | |
| # Update README with new badge | |
| if [ -f README.md ]; then | |
| sed -i "s|https://img.shields.io/badge/coverage-[0-9]*%25-[a-z]*|${BADGE_URL}|g" README.md | |
| echo "Updated coverage badge to ${PERCENTAGE}% (tested files only)" | |
| else | |
| echo "Warning: README.md not found" | |
| fi | |
| } | |
| else | |
| echo "Warning: coverage/lcov.info not found" | |
| PERCENTAGE=0 | |
| fi | |
| - name: Commit and push changes | |
| run: | | |
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
| git config --local user.name "github-actions[bot]" | |
| # Pull latest changes first | |
| git pull origin main --rebase | |
| git add README.md | |
| if git diff --staged --quiet; then | |
| echo "No changes to commit" | |
| else | |
| git commit -m "Update coverage badge [skip ci]" | |
| git push origin main | |
| fi |