update syntax #14
Workflow file for this run
This file contains 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
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go | |
name: Update Tests Coverage | |
on: | |
push: | |
# push: | |
# branches: [ "main" ] | |
# pull_request: | |
# branches: [ "main" ] | |
jobs: | |
build: | |
name: Go test coverage | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '1.22' | |
- name: Install dependencies | |
run: go get . | |
- name: Build | |
run: go build -v ./... | |
- name: Run tests and generate coverage badge | |
id: badge | |
run: | | |
go test -coverprofile=coverage.out ./... | |
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') | |
echo "Coverage: $COVERAGE" | |
COLOR="gray" | |
if (( $(echo "$COVERAGE < 50" | bc -l) )); then | |
COLOR="red" | |
elif (( $(echo "$COVERAGE >= 50 && $COVERAGE < 80" | bc -l) )); then | |
COLOR="yellow" | |
elif (( $(echo "$COVERAGE >= 80 && $COVERAGE < 90" | bc -l) )); then | |
COLOR="green" | |
elif (( $(echo "$COVERAGE >= 90" | bc -l) )); then | |
COLOR="brightgreen" | |
fi | |
BADGE_MARKDOWN="![Coverage Badge](https://img.shields.io/badge/Coverage-${COVERAGE}%25-${COLOR}.svg)" | |
echo "Current README.md content:" | |
cat README.md # Debug output | |
sed -i "s|!\[Coverage Badge\](.*)|$BADGE_MARKDOWN|g" README.md | |
echo "Updated README.md content:" | |
cat README.md # Debug output | |
- name: Commit coverage badge | |
continue-on-error: true | |
run: | | |
git config --local user.email "[email protected]" | |
git config --local user.name "GitHub Action" | |
# Add the README.md if changes exist | |
git add README.md | |
# Only commit if there are changes | |
if ! git diff-index --quiet HEAD --; then | |
git commit -m "Update coverage badge to $COVERAGE%" | |
git push | |
else | |
echo "No changes to commit." | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |