Skip to content

Commit b06447b

Browse files
committed
Publish coverage badge to GitHub Pages from CI
1 parent 0b8a580 commit b06447b

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

.github/workflows/ci.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ on:
33
pull_request:
44
push:
55
branches: [master]
6+
7+
permissions:
8+
contents: read
9+
pages: write
10+
id-token: write
11+
612
jobs:
713
test:
814
name: Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }} / AA ${{ matrix.activeadmin }}
@@ -24,3 +30,59 @@ jobs:
2430
bundler-cache: true
2531
- name: Run tests
2632
run: bundle exec rspec spec
33+
34+
- name: Generate badge.json
35+
if: matrix.ruby == '4.0' && matrix.rails == '~> 8.1.0' && matrix.activeadmin == '~> 3.5'
36+
run: |
37+
LAST_RUN="coverage/.last_run.json"
38+
if [ ! -f "$LAST_RUN" ]; then
39+
mkdir -p badge
40+
echo '{"schemaVersion":1,"label":"coverage","message":"unknown","color":"lightgrey"}' > badge/badge.json
41+
exit 0
42+
fi
43+
PERCENT=$(ruby -rjson -e "puts JSON.parse(File.read('$LAST_RUN')).dig('result','line').round(1)")
44+
PERCENT_NUM=$(ruby -rjson -e "puts JSON.parse(File.read('$LAST_RUN')).dig('result','line')")
45+
if ruby -e "exit(($PERCENT_NUM >= 90) ? 0 : 1)"; then COLOR="brightgreen"
46+
elif ruby -e "exit(($PERCENT_NUM >= 75) ? 0 : 1)"; then COLOR="green"
47+
elif ruby -e "exit(($PERCENT_NUM >= 60) ? 0 : 1)"; then COLOR="yellow"
48+
else COLOR="red"; fi
49+
mkdir -p badge
50+
echo "{\"schemaVersion\":1,\"label\":\"coverage\",\"message\":\"${PERCENT}%\",\"color\":\"${COLOR}\"}" > badge/badge.json
51+
52+
- name: Upload badge artifact
53+
if: matrix.ruby == '4.0' && matrix.rails == '~> 8.1.0' && matrix.activeadmin == '~> 3.5'
54+
uses: actions/upload-artifact@v4
55+
with:
56+
name: coverage-badge
57+
path: badge
58+
59+
deploy-coverage:
60+
needs: test
61+
if: github.ref == 'refs/heads/master' && github.event_name == 'push'
62+
runs-on: ubuntu-latest
63+
environment:
64+
name: github-pages
65+
url: ${{ steps.deployment.outputs.page_url }}
66+
67+
steps:
68+
- uses: actions/checkout@v4
69+
with:
70+
ref: gh-pages
71+
72+
- name: Download coverage badge
73+
uses: actions/download-artifact@v4
74+
with:
75+
name: coverage-badge
76+
path: .
77+
78+
- name: Setup Pages
79+
uses: actions/configure-pages@v5
80+
81+
- name: Upload Pages artifact
82+
uses: actions/upload-pages-artifact@v3
83+
with:
84+
path: .
85+
86+
- name: Deploy to GitHub Pages
87+
id: deployment
88+
uses: actions/deploy-pages@v4

0 commit comments

Comments
 (0)