test coverage #10
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
name: Go CI | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
permissions: | |
contents: write # Allow GitHub Actions to push changes | |
jobs: | |
test: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- name: Set up Go | |
uses: actions/setup-go@v4 | |
with: | |
go-version: '1.22' | |
# Cache Go modules with error handling for extraction conflicts | |
- name: Cache Go modules | |
uses: actions/cache@v3 | |
continue-on-error: true # Do not fail the workflow if caching fails | |
with: | |
path: | | |
~/.cache/go-build | |
~/go/pkg/mod | |
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
restore-keys: | | |
${{ runner.os }}-go- | |
- name: Install dependencies | |
run: go mod download | |
# Run tests with coverage and generate the coverage.out file | |
- name: Run tests with coverage | |
run: go test ./... -coverprofile=coverage.out | |
# Calculate coverage percentage and store it in a JSON file | |
- name: Generate coverage badge | |
run: | | |
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') | |
cat <<EOF > badge.json | |
{ | |
"schemaVersion": 1, | |
"label": "coverage", | |
"message": "$COVERAGE%", | |
"color": "brightgreen" | |
} | |
EOF | |
# Configure Git for committing the badge.json | |
- name: Configure Git | |
run: | | |
git config --global user.name 'GitHub Action' | |
git config --global user.email '[email protected]' | |
# Commit badge.json to the repository | |
- name: Commit badge | |
run: | | |
git add badge.json | |
git commit -m "Update coverage badge" | |
# Push changes to the triggering branch (whether it's a PR branch or main) | |
- name: Push changes | |
run: | | |
git push origin HEAD:${{ github.ref }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |