test coverage #7
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 | |
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' | |
- name: Cache Go 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- | |
- name: Install dependencies | |
run: go mod download | |
# Run tests with coverage and generate the coverage.out | |
- 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 the changes to the current branch | |
- name: Push changes | |
run: | | |
git push origin HEAD:${{ github.ref }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |