Skip to content

Commit 8e89c95

Browse files
committed
feat: ✨ upload badges to github pages
1 parent 2a0887f commit 8e89c95

File tree

7 files changed

+176
-52
lines changed

7 files changed

+176
-52
lines changed

ACTION_README.md

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A GitHub Action that automatically creates beautiful coverage reports in pull re
1010
- 🔄 **Smart Updates**: Updates existing comments instead of creating duplicates
1111
- 🎨 **Visual Indicators**: Uses emojis to quickly identify coverage status
1212
- 🏷️ **Coverage Badges**: Generates shields.io compatible badges for GitHub Pages
13+
- 🚀 **Automatic GitHub Pages Upload**: Automatically uploads badges to GitHub Pages
1314
-**Lightweight**: Fast execution with minimal dependencies
1415

1516
## Usage
@@ -56,6 +57,7 @@ on:
5657

5758
permissions:
5859
pull-requests: write
60+
contents: write # Required for GitHub Pages upload
5961

6062
jobs:
6163
coverage:
@@ -82,18 +84,24 @@ jobs:
8284
show-files: "true"
8385
coverage-threshold: "90"
8486
make-badges: "true"
87+
upload-badges-to-pages: "true"
88+
pages-branch: "gh-pages"
89+
pages-badges-dir: "badges"
8590
```
8691
8792
## Inputs
8893
89-
| Input | Description | Required | Default |
90-
| -------------------- | --------------------------------------------------------- | -------- | -------------------------------- |
91-
| `coverage-file` | Path to the coverage summary JSON file | No | `coverage/coverage-summary.json` |
92-
| `token` | GitHub token for creating comments | No | `${{ github.token }}` |
93-
| `title` | Title for the coverage report comment | No | `📊 Coverage Report` |
94-
| `show-files` | Whether to show individual file coverage details | No | `true` |
95-
| `coverage-threshold` | Minimum coverage percentage to consider as good (0-100) | No | `80` |
96-
| `make-badges` | Whether to generate coverage badges in a badges directory | No | `true` |
94+
| Input | Description | Required | Default |
95+
| ------------------------ | --------------------------------------------------------- | -------- | -------------------------------- |
96+
| `coverage-file` | Path to the coverage summary JSON file | No | `coverage/coverage-summary.json` |
97+
| `token` | GitHub token for creating comments | No | `${{ github.token }}` |
98+
| `title` | Title for the coverage report comment | No | `📊 Coverage Report` |
99+
| `show-files` | Whether to show individual file coverage details | No | `true` |
100+
| `coverage-threshold` | Minimum coverage percentage to consider as good (0-100) | No | `80` |
101+
| `make-badges` | Whether to generate coverage badges in a badges directory | No | `true` |
102+
| `upload-badges-to-pages` | Whether to automatically upload badges to GitHub Pages | No | `true` |
103+
| `pages-branch` | Branch to upload badges to for GitHub Pages | No | `gh-pages` |
104+
| `pages-badges-dir` | Directory within the pages branch to store badges | No | `badges` |
97105

98106
## Coverage File Format
99107

@@ -208,26 +216,44 @@ When `make-badges` is enabled (default: `true`), the action creates a `badges` d
208216

209217
### Using Badges with GitHub Pages
210218

211-
1. Enable GitHub Pages in your repository settings
212-
2. Set the source to "Deploy from a branch" and select your main branch
213-
3. The badges will be available at:
219+
The action automatically uploads badges to your GitHub Pages branch when `upload-badges-to-pages` is enabled (default: `true`).
220+
221+
1. **Automatic Setup**: The action will:
222+
223+
- Create the `gh-pages` branch if it doesn't exist
224+
- Upload badges to the specified directory
225+
- Commit and push changes automatically
226+
227+
2. **Enable GitHub Pages** in your repository settings:
228+
229+
- Go to Settings → Pages
230+
- Set source to "Deploy from a branch"
231+
- Select your `gh-pages` branch (or the branch specified in `pages-branch`)
232+
- Set folder to `/ (root)` or `/badges` depending on your preference
233+
234+
3. **Badge URLs** will be available at:
214235

215236
```
216237
https://yourusername.github.io/yourrepo/badges/coverage.json
238+
https://yourusername.github.io/yourrepo/badges/statements.json
239+
https://yourusername.github.io/yourrepo/badges/branches.json
240+
https://yourusername.github.io/yourrepo/badges/functions.json
241+
https://yourusername.github.io/yourrepo/badges/lines.json
217242
```
218243

219-
4. Use in your README.md:
244+
4. **Use in your README.md**:
220245
```markdown
221246
![Coverage](https://yourusername.github.io/yourrepo/badges/coverage.json)
247+
![Statements](https://yourusername.github.io/yourrepo/badges/statements.json)
248+
![Branches](https://yourusername.github.io/yourrepo/badges/branches.json)
222249
```
223250

224-
### Example Badge JSON
251+
### Required Permissions
225252

226-
```json
227-
{
228-
"schemaVersion": 1,
229-
"label": "coverage",
230-
"message": "89.8%",
231-
"color": "green"
232-
}
253+
For automatic GitHub Pages upload, your workflow needs:
254+
255+
```yaml
256+
permissions:
257+
pull-requests: write
258+
contents: write # Required for uploading to GitHub Pages
233259
```

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ A plugin for [Vitest](https://vitest.dev/) that generates a structured JSON cove
1111
- ✅ Compatible with Vitest 3.0+
1212
- 🚀 **NEW**: GitHub Action for automatic PR coverage reporting
1313
- 🏷️ **NEW**: Automatic coverage badge generation for GitHub Pages
14+
- 🚀 **NEW**: Automatic upload of badges to GitHub Pages
1415

1516
## Installation
1617

@@ -20,7 +21,7 @@ npm install --save-dev vitest-v8-json-coverage-summary
2021

2122
## GitHub Action
2223

23-
This package also includes a GitHub Action that automatically creates beautiful coverage reports in pull requests.
24+
This package also includes a GitHub Action that automatically creates beautiful coverage reports in pull requests and uploads coverage badges to GitHub Pages.
2425

2526
### Quick Start
2627

@@ -32,6 +33,7 @@ on:
3233

3334
permissions:
3435
pull-requests: write
36+
contents: write # Required for GitHub Pages upload
3537

3638
jobs:
3739
coverage:
@@ -44,8 +46,17 @@ jobs:
4446
- run: npm ci
4547
- run: npm test
4648
- uses: glideapps/vitest-v8-json-coverage-summary@v1
49+
with:
50+
make-badges: "true"
51+
upload-badges-to-pages: "true"
4752
```
4853
54+
The action will automatically:
55+
56+
- Create coverage reports in pull request comments
57+
- Generate coverage badges
58+
- Upload badges to the `gh-pages` branch for GitHub Pages
59+
4960
For detailed documentation, see [ACTION_README.md](ACTION_README.md).
5061

5162
## Usage

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ inputs:
3030
description: "Whether to generate coverage badges in a badges directory"
3131
required: false
3232
default: "true"
33+
upload-badges-to-pages:
34+
description: "Whether to automatically upload badges to GitHub Pages branch"
35+
required: false
36+
default: "true"
37+
pages-branch:
38+
description: "Branch to upload badges to for GitHub Pages"
39+
required: false
40+
default: "gh-pages"
41+
pages-badges-dir:
42+
description: "Directory within the pages branch to store badges"
43+
required: false
44+
default: "badges"
3345

3446
runs:
3547
using: "node20"

example-workflow-with-badges.yml

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
name: Coverage Report with Badges
1+
name: Coverage Report with Automatic Badge Upload
22

33
on:
44
pull_request:
55
branches: [main, develop]
66

77
permissions:
8-
contents: read
98
pull-requests: write
9+
contents: write # Required for GitHub Pages upload
1010

1111
jobs:
1212
test-and-coverage:
@@ -27,38 +27,14 @@ jobs:
2727
- name: Run tests with coverage
2828
run: npm test
2929

30-
- name: Report Coverage
30+
- name: Report Coverage and Upload Badges
3131
uses: glideapps/vitest-v8-json-coverage-summary@v1
3232
with:
3333
coverage-file: "coverage/coverage-summary.json"
3434
title: "🧪 Test Coverage Report"
3535
show-files: "true"
3636
coverage-threshold: "80"
3737
make-badges: "true"
38-
39-
- name: Commit badges (if on main branch)
40-
if: github.ref == 'refs/heads/main'
41-
run: |
42-
git config --local user.email "[email protected]"
43-
git config --local user.name "GitHub Action"
44-
git add badges/
45-
git diff --quiet && git diff --staged --quiet || git commit -m "Update coverage badges"
46-
git push
47-
48-
# Optional: Deploy badges to GitHub Pages
49-
deploy-badges:
50-
if: github.ref == 'refs/heads/main'
51-
needs: test-and-coverage
52-
runs-on: ubuntu-latest
53-
permissions:
54-
contents: write
55-
steps:
56-
- name: Checkout code
57-
uses: actions/checkout@v4
58-
59-
- name: Deploy to GitHub Pages
60-
uses: peaceiris/actions-gh-pages@v3
61-
with:
62-
github_token: ${{ secrets.GITHUB_TOKEN }}
63-
publish_dir: ./badges
64-
destination_dir: badges
38+
upload-badges-to-pages: "true"
39+
pages-branch: "gh-pages"
40+
pages-badges-dir: "badges"

example-workflow.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
branches: [main, develop]
66

77
permissions:
8-
contents: read
98
pull-requests: write
9+
contents: write # Required for GitHub Pages upload
1010

1111
jobs:
1212
test-and-coverage:
@@ -35,3 +35,4 @@ jobs:
3535
show-files: "true"
3636
coverage-threshold: "80"
3737
make-badges: "true"
38+
upload-badges-to-pages: "true"

github-action/action.js

Lines changed: 98 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
File renamed without changes.

0 commit comments

Comments
 (0)