Skip to content

Commit 1f1053e

Browse files
committed
chore: add pre-commit hooks, semantic releases and rollback workflow
Configure lefthook for git hooks: commit-msg validates Conventional Commits, pre-commit runs format and analyze, pre-push runs tests. Add release-please for automatic version bumps, changelog generation and GitHub releases. Add rollback workflow for quick recovery and PR preview workflow for testing changes before merge.
1 parent 6995cdd commit 1f1053e

9 files changed

Lines changed: 217 additions & 6 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
workflow_call:
99

1010
concurrency:
11-
group: ci-${{ github.ref }}
11+
group: ci-${{ github.workflow }}-${{ github.ref }}
1212
cancel-in-progress: true
1313

1414
jobs:

.github/workflows/preview.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: PR Preview
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
7+
permissions:
8+
contents: read
9+
pull-requests: write
10+
11+
jobs:
12+
preview:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v6
16+
17+
- uses: subosito/flutter-action@v2
18+
with:
19+
channel: stable
20+
cache: true
21+
22+
- name: Install dependencies
23+
run: flutter pub get
24+
25+
- name: Build web
26+
run: flutter build web --release --pwa-strategy=offline-first --base-href "/"
27+
28+
- name: Upload preview artifact
29+
uses: actions/upload-artifact@v4
30+
id: artifact
31+
with:
32+
name: preview-pr-${{ github.event.number }}
33+
path: build/web
34+
retention-days: 7
35+
36+
- name: Comment PR with preview link
37+
uses: actions/github-script@v7
38+
with:
39+
script: |
40+
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
41+
const body = [
42+
'### 🔍 Preview build ready',
43+
'',
44+
`Download the preview artifact and open \`index.html\` locally, or check the [workflow run](${runUrl}) to download it.`,
45+
'',
46+
`Built from \`${context.sha.substring(0, 7)}\` · artifact expires in 7 days.`,
47+
].join('\n');
48+
49+
const { data: comments } = await github.rest.issues.listComments({
50+
owner: context.repo.owner,
51+
repo: context.repo.repo,
52+
issue_number: context.issue.number,
53+
});
54+
const existing = comments.find(c => c.body.includes('Preview build ready'));
55+
if (existing) {
56+
await github.rest.issues.updateComment({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
comment_id: existing.id,
60+
body,
61+
});
62+
} else {
63+
await github.rest.issues.createComment({
64+
owner: context.repo.owner,
65+
repo: context.repo.repo,
66+
issue_number: context.issue.number,
67+
body,
68+
});
69+
}

.github/workflows/release.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
permissions:
8+
contents: write
9+
pull-requests: write
10+
11+
jobs:
12+
ci:
13+
uses: ./.github/workflows/ci.yml
14+
15+
release:
16+
needs: ci
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: googleapis/release-please-action@v4
20+
with:
21+
config-file: release-please-config.json
22+
manifest-file: .release-please-manifest.json

.github/workflows/rollback.yml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: Rollback
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
ref:
7+
description: "Git ref to rollback to (tag, commit SHA or branch)"
8+
required: true
9+
type: string
10+
11+
permissions:
12+
contents: read
13+
pages: write
14+
id-token: write
15+
16+
concurrency:
17+
group: pages
18+
cancel-in-progress: false
19+
20+
jobs:
21+
rollback:
22+
runs-on: ubuntu-latest
23+
steps:
24+
- uses: actions/checkout@v6
25+
with:
26+
ref: ${{ inputs.ref }}
27+
28+
- uses: subosito/flutter-action@v2
29+
with:
30+
channel: stable
31+
cache: true
32+
33+
- name: Install dependencies
34+
run: flutter pub get
35+
36+
- name: Build web
37+
run: flutter build web --release --pwa-strategy=offline-first --base-href "/${{ github.event.repository.name }}/"
38+
39+
- name: Upload artifact
40+
uses: actions/upload-pages-artifact@v4
41+
with:
42+
path: build/web
43+
44+
deploy:
45+
needs: rollback
46+
runs-on: ubuntu-latest
47+
environment:
48+
name: github-pages
49+
url: ${{ steps.deployment.outputs.page_url }}
50+
steps:
51+
- name: Deploy to GitHub Pages
52+
id: deployment
53+
uses: actions/deploy-pages@v4

.release-please-manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
".": "1.0.0"
3+
}

Makefile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
.PHONY: help format format-check analyze pubget run-web build-web clean test
1+
.PHONY: help format format-check analyze pubget run-web build-web clean test setup coverage
22

33
help:
44
@echo "Available targets:"
5+
@echo " make setup - Install deps and git hooks (lefthook)"
56
@echo " make format - Run official Dart formatter"
67
@echo " make format-check - Check formatting (CI)"
78
@echo " make analyze - Run flutter analyze"
89
@echo " make test - Run flutter test"
10+
@echo " make coverage - Run tests and show coverage"
911
@echo " make pubget - Run flutter pub get"
1012
@echo " make run-web - Run flutter run"
1113
@echo " make build-web - Build web with offline-first PWA"
1214
@echo " make clean - Run flutter clean"
1315

16+
setup:
17+
flutter pub get
18+
@command -v lefthook >/dev/null 2>&1 && lefthook install || echo "Install lefthook: https://github.com/evilmartians/lefthook#install"
19+
1420
format:
1521
dart format .
1622

@@ -29,6 +35,11 @@ pubget:
2935
run-web:
3036
flutter run -d web-server --web-port=8080 --web-hostname=0.0.0.0
3137

38+
coverage:
39+
flutter test --coverage
40+
@awk 'BEGIN{s=0}/^SF:.*lib\/l10n\//{s=1}/^end_of_record/{if(s){s=0;next}}!s' coverage/lcov.info > coverage/lcov_filtered.info
41+
@awk '/^LF:/{t+=substr($$0,4)}/^LH:/{h+=substr($$0,4)}END{printf "Coverage: %.1f%% (excluding generated l10n)\n",h/t*100}' coverage/lcov_filtered.info
42+
3243
build-web:
3344
flutter build web --release --pwa-strategy=offline-first
3445

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,41 @@ Pictograms are universally easy to identify, making communication simple and int
4747
### Prerequisites
4848

4949
- Flutter SDK >= 3.0.0
50+
- [lefthook](https://github.com/evilmartians/lefthook#install) (optional, for git hooks)
5051

5152
### Installation
5253

5354
```bash
54-
flutter pub get
55-
flutter gen-l10n
55+
make setup
5656
```
5757

58+
This installs dependencies and configures pre-commit hooks (format + analyze) and pre-push hooks (tests).
59+
5860
### Running
5961

6062
```bash
61-
flutter run
63+
make run-web
6264
```
6365

6466
### Testing
6567

6668
```bash
67-
flutter test
69+
make test # run all tests
70+
make coverage # run tests and report coverage
6871
```
6972

73+
### Releasing
74+
75+
Releases are managed automatically by [release-please](https://github.com/googleapis/release-please). Push to `main` using [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`, `fix:`, `chore:`, etc.) and a release PR will be created with the version bump and changelog.
76+
77+
### Rollback
78+
79+
If a deploy breaks in production, rollback to any previous version from the **Actions** tab:
80+
81+
1. Go to **Actions → Rollback → Run workflow**
82+
2. Enter a git ref (tag like `v1.0.0`, commit SHA, or branch)
83+
3. The previous version will be rebuilt and deployed to GitHub Pages
84+
7085
## Project Structure
7186

7287
```

lefthook.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
commit-msg:
2+
commands:
3+
conventional-commit:
4+
run: |
5+
msg=$(head -1 {1})
6+
if ! echo "$msg" | grep -qE '^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?!?: .+'; then
7+
echo "ERROR: Commit message must follow Conventional Commits"
8+
echo " Format: <type>[optional scope]: <description>"
9+
echo " Types: feat fix docs style refactor perf test build ci chore revert"
10+
echo " Got: $msg"
11+
exit 1
12+
fi
13+
14+
pre-commit:
15+
parallel: false
16+
commands:
17+
format:
18+
run: dart format --set-exit-if-changed .
19+
analyze:
20+
run: flutter analyze
21+
22+
pre-push:
23+
parallel: false
24+
commands:
25+
test:
26+
run: flutter test

release-please-config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"packages": {
3+
".": {
4+
"release-type": "dart",
5+
"bump-minor-pre-major": true,
6+
"bump-patch-for-minor-pre-major": true,
7+
"extra-files": [
8+
"pubspec.yaml"
9+
]
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)