|
| 1 | +# .github/workflows/coverage.yml |
| 2 | +# |
| 3 | +# Purpose: |
| 4 | +# - Run Go tests with coverage on every Pull Request (PR) |
| 5 | +# - Upload the coverage report to Coveralls |
| 6 | +# - Also run the same workflow once per month (useful as a periodic βcanaryβ) |
| 7 | +# |
| 8 | +# Notes: |
| 9 | +# - GitHub scheduled workflows run in UTC and only run from the default branch workflow file. |
| 10 | +# - If you add this file on a non-default branch, the schedule will not start until merged. :contentReference[oaicite:0]{index=0} |
| 11 | + |
| 12 | +name: Go Coverage (PR + Monthly) |
| 13 | + |
| 14 | +on: |
| 15 | + # Run for all PRs targeting any branch |
| 16 | + pull_request: |
| 17 | + |
| 18 | + # Run once a month: at 03:00 UTC on the 1st day of every month |
| 19 | + # Adjust to your preference; cron is always interpreted in UTC. :contentReference[oaicite:1]{index=1} |
| 20 | + schedule: |
| 21 | + - cron: "0 3 1 * *" |
| 22 | + |
| 23 | + # Optional: allow manual runs from the Actions tab (handy for testing changes) |
| 24 | + workflow_dispatch: |
| 25 | + |
| 26 | +# Minimal permissions. Coveralls can post PR comments; that requires pull-requests: write. |
| 27 | +# contents: read is sufficient for checkout. |
| 28 | +permissions: |
| 29 | + contents: read |
| 30 | + pull-requests: write |
| 31 | + |
| 32 | +# Concurrency prevents duplicated runs from stacking up when you push multiple commits quickly to a PR. |
| 33 | +concurrency: |
| 34 | + group: coverage-${{ github.event.pull_request.number || github.ref }} |
| 35 | + cancel-in-progress: true |
| 36 | + |
| 37 | +jobs: |
| 38 | + coverage: |
| 39 | + runs-on: ubuntu-latest |
| 40 | + |
| 41 | + steps: |
| 42 | + # 1) Check out repository code |
| 43 | + - name: Checkout |
| 44 | + uses: actions/checkout@v4 |
| 45 | + |
| 46 | + # 2) Install Go (recommended: read version from go.mod) |
| 47 | + # actions/setup-go exists in newer majors; using the major tag keeps you on supported updates. :contentReference[oaicite:2]{index=2} |
| 48 | + - name: Set up Go |
| 49 | + uses: actions/setup-go@v6 |
| 50 | + with: |
| 51 | + go-version-file: go.mod |
| 52 | + cache: true |
| 53 | + |
| 54 | + # 3) Run tests and generate a coverage profile (Go native format) |
| 55 | + # coverage.out is a common convention; it matches what you saw locally. |
| 56 | + - name: Run tests with coverage |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | + go test ./... -covermode=atomic -coverprofile=coverage.out |
| 60 | +
|
| 61 | + # 4) Print a human-readable summary into the workflow logs |
| 62 | + # This uses Goβs built-in cover tool. :contentReference[oaicite:3]{index=3} |
| 63 | + - name: Coverage summary (log) |
| 64 | + run: | |
| 65 | + set -euo pipefail |
| 66 | + go tool cover -func=coverage.out |
| 67 | +
|
| 68 | + # 5) Upload coverage.out as an artifact for later inspection/download |
| 69 | + # Useful for debugging and auditing. Artifact v4 is the current major. :contentReference[oaicite:4]{index=4} |
| 70 | + - name: Upload coverage artifact |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + with: |
| 73 | + name: coverage-out |
| 74 | + path: coverage.out |
| 75 | + if-no-files-found: error |
| 76 | + |
| 77 | + # 6) Send coverage to Coveralls |
| 78 | + # |
| 79 | + # coverallsapp/github-action supports multiple input styles; 'file' + 'format' is the modern pattern. |
| 80 | + # It can also add a PR comment on pull_request events. :contentReference[oaicite:5]{index=5} |
| 81 | + # |
| 82 | + # Authentication: |
| 83 | + # - For public repos with GitHub integration, GITHUB_TOKEN is often enough. |
| 84 | + # - If your Coveralls setup requires a repo token, set COVERALLS_REPO_TOKEN as a GitHub Actions secret |
| 85 | + # and pass it via env (see section B below). |
| 86 | + - name: Publish to Coveralls |
| 87 | + uses: coverallsapp/github-action@v2 |
| 88 | + with: |
| 89 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + file: coverage.out |
| 91 | + format: golang |
| 92 | + # If you need a repo token, uncomment the env block: |
| 93 | + env: |
| 94 | + COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} |
| 95 | +β |
0 commit comments