Skip to content

Commit 3767875

Browse files
committed
Add auto release on ver change
1 parent 22708ca commit 3767875

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: Create Release on Version Change
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
paths:
8+
- "build.gradle.kts"
9+
10+
permissions:
11+
contents: write
12+
13+
jobs:
14+
create-release:
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- name: Checkout repository
19+
uses: actions/checkout@v4
20+
with:
21+
# Needed so we can inspect previous commits and tags
22+
fetch-depth: 0
23+
24+
- name: Extract current version from build.gradle.kts
25+
id: current_version
26+
run: |
27+
VERSION_LINE=$(grep '^version' build.gradle.kts | head -n 1 || true)
28+
if [ -z "$VERSION_LINE" ]; then
29+
echo "Could not find version line in build.gradle.kts"
30+
exit 1
31+
fi
32+
VERSION=$(echo "$VERSION_LINE" | sed -E 's/^[^"]*"([^"]+)".*/\1/')
33+
echo "Current version: $VERSION"
34+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
35+
36+
- name: Extract previous version from build.gradle.kts
37+
id: previous_version
38+
run: |
39+
# If there is no previous commit on this branch, treat it as a version change
40+
if ! git rev-parse HEAD^ >/dev/null 2>&1; then
41+
echo "No previous commit on this branch, treating as version change."
42+
echo "version=" >> "$GITHUB_OUTPUT"
43+
exit 0
44+
fi
45+
46+
VERSION_LINE=$(git show HEAD^:build.gradle.kts | grep '^version' | head -n 1 || true)
47+
if [ -z "$VERSION_LINE" ]; then
48+
echo "Could not find version line in previous build.gradle.kts; treating as version change."
49+
echo "version=" >> "$GITHUB_OUTPUT"
50+
exit 0
51+
fi
52+
53+
VERSION=$(echo "$VERSION_LINE" | sed -E 's/^[^"]*"([^"]+)".*/\1/')
54+
echo "Previous version: $VERSION"
55+
echo "version=$VERSION" >> "$GITHUB_OUTPUT"
56+
57+
- name: Determine if version changed
58+
id: version_changed
59+
run: |
60+
CURR="${{ steps.current_version.outputs.version }}"
61+
PREV="${{ steps.previous_version.outputs.version }}"
62+
echo "Current version: $CURR"
63+
echo "Previous version: $PREV"
64+
65+
if [ -n "$PREV" ] && [ "$CURR" = "$PREV" ]; then
66+
echo "Version did not change."
67+
echo "changed=false" >> "$GITHUB_OUTPUT"
68+
else
69+
echo "Version changed."
70+
echo "changed=true" >> "$GITHUB_OUTPUT"
71+
fi
72+
73+
- name: Stop if version did not change
74+
if: steps.version_changed.outputs.changed == 'false'
75+
run: |
76+
echo "Version did not change; skipping release."
77+
exit 0
78+
79+
- name: Determine previous tag
80+
id: previous_tag
81+
if: steps.version_changed.outputs.changed == 'true'
82+
run: |
83+
if git describe --tags --abbrev=0 >/dev/null 2>&1; then
84+
TAG=$(git describe --tags --abbrev=0)
85+
echo "Previous tag: $TAG"
86+
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
87+
else
88+
echo "No previous tag found."
89+
echo "tag=" >> "$GITHUB_OUTPUT"
90+
fi
91+
92+
- name: Generate release notes
93+
id: notes
94+
if: steps.version_changed.outputs.changed == 'true'
95+
run: |
96+
PREV_TAG="${{ steps.previous_tag.outputs.tag }}"
97+
98+
if [ -n "$PREV_TAG" ]; then
99+
RANGE="$PREV_TAG..HEAD"
100+
else
101+
FIRST_COMMIT=$(git rev-list --max-parents=0 HEAD)
102+
RANGE="$FIRST_COMMIT..HEAD"
103+
fi
104+
105+
echo "Using commit range: $RANGE"
106+
BODY=$(git log --pretty=format:'- %s (%h)' "$RANGE")
107+
108+
{
109+
echo "body<<EOF"
110+
echo "Changes in version ${{ steps.current_version.outputs.version }}"
111+
echo
112+
echo "$BODY"
113+
echo "EOF"
114+
} >> "$GITHUB_OUTPUT"
115+
116+
- name: Create GitHub Release
117+
if: steps.version_changed.outputs.changed == 'true'
118+
uses: softprops/action-gh-release@v2
119+
with:
120+
tag_name: v${{ steps.current_version.outputs.version }}
121+
name: v${{ steps.current_version.outputs.version }}
122+
body: ${{ steps.notes.outputs.body }}
123+
env:
124+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
125+
126+

0 commit comments

Comments
 (0)