-
-
Notifications
You must be signed in to change notification settings - Fork 0
284 lines (255 loc) · 10.6 KB
/
Copy pathprod.yml
File metadata and controls
284 lines (255 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# Push to `prod` -> tests run -> version is bumped automatically -> tag is
# pushed -> publish.yml ships to pub.dev.
#
# Bump rules:
# - If the pubspec.yaml version on prod is *not* yet tagged on the remote,
# the workflow assumes you bumped it by hand and uses it as-is.
# - Otherwise the latest commit subject decides the bump:
# contains "breaking" or "BREAKING CHANGE" -> major (X+1.0.0)
# starts with "feat" / "feat:" / "feat(...)" -> minor (X.Y+1.0)
# anything else -> patch (X.Y.Z+1)
#
# The bump is committed back to `prod` with a `ci:` message. Self-commits
# (by github-actions[bot]) are filtered out at the top so the workflow does
# not re-trigger itself.
#
# An early `preflight` job compares HEAD's tree to the most recent release
# tag's tree. If nothing tracked changed, the rest of the workflow is
# skipped — so a no-op push to prod doesn't burn through a version number.
name: Prod — Test, Bump & Tag
on:
push:
branches:
- prod
permissions:
contents: write # Needed to push the bump commit and the tag
concurrency:
group: prod-release
cancel-in-progress: false
jobs:
preflight:
name: Detect changes since last release
runs-on: ubuntu-latest
# Same self-commit filter as the release job — no point even running
# preflight when the bot is responsible for the push.
if: github.event.head_commit.author.email != 'github-actions[bot]@users.noreply.github.com'
outputs:
changed: ${{ steps.diff.outputs.changed }}
steps:
- name: Checkout code
uses: actions/checkout@v5
with:
fetch-depth: 0 # Need history + tags for the diff
ref: prod
# Default GITHUB_TOKEN is fine here — preflight is read-only.
- name: Compare HEAD against last release tag
id: diff
run: |
set -euo pipefail
LAST_TAG=$(git describe --tags --abbrev=0 --match='v[0-9]*' 2>/dev/null || true)
if [[ -z "$LAST_TAG" ]]; then
echo "No previous release tag found — first release. Proceeding."
echo "changed=true" >> "$GITHUB_OUTPUT"
exit 0
fi
if git diff --quiet "$LAST_TAG" HEAD -- ; then
echo "::notice::No tracked changes since $LAST_TAG — skipping release."
echo "changed=false" >> "$GITHUB_OUTPUT"
else
echo "Tracked changes detected since $LAST_TAG — proceeding."
echo "changed=true" >> "$GITHUB_OUTPUT"
fi
release:
name: Test, bump, tag
needs: preflight
if: needs.preflight.outputs.changed == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5
with:
fetch-depth: 0 # Full history + tags for accurate bump decisions
ref: prod
# PAT (not GITHUB_TOKEN) so the tag push at the end of this job
# is attributed to a real account and triggers publish.yml.
# GitHub blocks workflow_run cascades from GITHUB_TOKEN.
token: ${{ secrets.RELEASE_PAT }}
- name: Set up Flutter
uses: subosito/flutter-action@v2
with:
channel: stable
- name: Install dependencies
run: flutter pub get
- name: Verify formatter
# Scope to this package's own primary source. Avoids walking into
# sub-projects (example/, smoke_test/) that carry their own pubspec
# but aren't `pub get`-ed in CI — formatting them with no package
# config triggers a language-version fallback and spurious diffs.
run: |
DIRS=""
for d in lib test bin; do
[ -d "$d" ] && DIRS="$DIRS $d"
done
if [ -z "$DIRS" ]; then
echo "No primary source directories to format."
exit 0
fi
dart format --output=none --set-exit-if-changed $DIRS
- name: Static analysis
run: flutter analyze
- name: Run tests
run: flutter test
- name: Decide next version
id: bump
env:
COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
CURRENT=$(grep -E '^version:' pubspec.yaml | sed -E 's/^version:[[:space:]]*//; s/[[:space:]]+$//')
if [[ -z "$CURRENT" ]]; then
echo "::error::Could not parse version from pubspec.yaml"
exit 1
fi
echo "Current pubspec version: $CURRENT"
# If the current version is *not* yet a tag on the remote, assume
# the maintainer already bumped it. Use as-is.
if git ls-remote --tags origin | grep -q "refs/tags/v${CURRENT}$"; then
echo "Tag v${CURRENT} already exists -> auto-bump required."
NEEDS_BUMP=true
else
echo "Tag v${CURRENT} does not exist -> using pubspec version as-is."
NEEDS_BUMP=false
fi
if [[ "$NEEDS_BUMP" == "false" ]]; then
NEXT="$CURRENT"
else
# Strip optional +build suffix so we can do arithmetic on the
# numeric core; re-attach it (if any) at the end.
CORE="${CURRENT%%+*}"
BUILD=""
if [[ "$CURRENT" == *"+"* ]]; then
BUILD="+${CURRENT#*+}"
fi
IFS='.' read -r MAJOR MINOR PATCH <<< "$CORE"
FIRST_LINE=$(printf '%s\n' "$COMMIT_MSG" | head -n 1)
LOWER_FIRST=$(printf '%s' "$FIRST_LINE" | tr '[:upper:]' '[:lower:]')
if printf '%s' "$COMMIT_MSG" | grep -qiE 'breaking change|^breaking:|^[a-z]+(\([^)]*\))?!:'; then
MAJOR=$((MAJOR + 1)); MINOR=0; PATCH=0
BUMP_KIND="major"
elif [[ "$LOWER_FIRST" =~ ^feat([\(:[:space:]]|$) ]]; then
MINOR=$((MINOR + 1)); PATCH=0
BUMP_KIND="minor"
else
PATCH=$((PATCH + 1))
BUMP_KIND="patch"
fi
NEXT="${MAJOR}.${MINOR}.${PATCH}${BUILD}"
echo "Bump kind: $BUMP_KIND -> $CURRENT -> $NEXT"
fi
{
echo "CURRENT=$CURRENT"
echo "NEXT=$NEXT"
echo "NEEDS_BUMP=$NEEDS_BUMP"
} >> "$GITHUB_OUTPUT"
- name: Apply version bump
if: steps.bump.outputs.NEEDS_BUMP == 'true'
env:
CURRENT: ${{ steps.bump.outputs.CURRENT }}
NEXT: ${{ steps.bump.outputs.NEXT }}
run: |
# Use a literal-string sed: replaces the exact `version: $CURRENT`
# line with `version: $NEXT`. Anchored to start-of-line so a
# `version:` appearing elsewhere (a doc comment, etc.) is ignored.
sed -i.bak -E "s/^version:[[:space:]]+.*$/version: ${NEXT}/" pubspec.yaml
rm pubspec.yaml.bak
echo "pubspec.yaml updated:"
grep -E '^version:' pubspec.yaml
- name: Update CHANGELOG.md if entry missing
env:
NEXT: ${{ steps.bump.outputs.NEXT }}
COMMIT_MSG: ${{ github.event.head_commit.message }}
run: |
set -euo pipefail
# Skip if the changelog already mentions this version anywhere.
# pana matches both `## 1.2.3` and `## [1.2.3]`, so just grep for
# the bare version string after a `## ` heading.
if grep -qE "^## \[?v?${NEXT//./\\.}\]?(\b|[[:space:]]|$)" CHANGELOG.md 2>/dev/null; then
echo "CHANGELOG.md already mentions v${NEXT} — leaving it alone."
exit 0
fi
# Build a bullet list from the commit message: the subject becomes
# the headline bullet; body lines starting with "- " or "* " are
# included as their own bullets.
SUBJECT=$(printf '%s\n' "$COMMIT_MSG" | head -n 1 | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
BODY_BULLETS=$(printf '%s\n' "$COMMIT_MSG" | awk 'NR>1 && /^[[:space:]]*[-*][[:space:]]/ { sub(/^[[:space:]]*[-*][[:space:]]/, ""); print "- " $0 }')
{
echo "## [${NEXT}]"
echo ""
if [[ -n "$SUBJECT" ]]; then
echo "- ${SUBJECT}"
else
echo "- Release ${NEXT}"
fi
if [[ -n "$BODY_BULLETS" ]]; then
echo "$BODY_BULLETS"
fi
echo ""
} > /tmp/changelog-entry.md
if [[ ! -f CHANGELOG.md ]]; then
{ echo "# Changelog"; echo ""; cat /tmp/changelog-entry.md; } > CHANGELOG.md
else
# Insert the new section directly after the first `# ` heading.
awk -v entry_file=/tmp/changelog-entry.md '
BEGIN { inserted = 0 }
{
print
if (!inserted && $0 ~ /^# /) {
print ""
while ((getline line < entry_file) > 0) print line
close(entry_file)
inserted = 1
}
}
' CHANGELOG.md > CHANGELOG.md.new
mv CHANGELOG.md.new CHANGELOG.md
fi
echo "CHANGELOG.md updated."
- name: Commit bump back to prod
id: commit
env:
NEXT: ${{ steps.bump.outputs.NEXT }}
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add pubspec.yaml CHANGELOG.md
if git diff --staged --quiet; then
echo "No bump-related changes to commit."
echo "COMMITTED=false" >> "$GITHUB_OUTPUT"
else
git commit -m "ci: release v${NEXT}"
git push origin HEAD:prod
echo "COMMITTED=true" >> "$GITHUB_OUTPUT"
fi
- name: Create & push tag
env:
NEXT: ${{ steps.bump.outputs.NEXT }}
run: |
set -euo pipefail
TAG="v${NEXT}"
# Re-check the remote in case a parallel push beat us to it.
if git ls-remote --tags origin | grep -q "refs/tags/${TAG}$"; then
echo "Tag ${TAG} already on origin; skipping."
exit 0
fi
# Drop any stale local tag without a remote counterpart (e.g. a
# re-run of a workflow that had previously created the tag but
# whose remote was later deleted by hand). Without this the
# `git tag -a` below would fail with "tag already exists".
if git rev-parse --verify --quiet "refs/tags/${TAG}" >/dev/null; then
echo "Local tag ${TAG} present without remote counterpart; removing."
git tag -d "${TAG}"
fi
git tag -a "${TAG}" -m "Release ${TAG}"
git push origin "${TAG}"
echo "Pushed tag ${TAG}. publish.yml will take it from here."