Skip to content

Commit ea71d9c

Browse files
committed
Add release scripts
1 parent 7387331 commit ea71d9c

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Gradle Release
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
release:
8+
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: set up JDK 17
14+
uses: actions/setup-java@v3
15+
with:
16+
java-version: '17'
17+
distribution: 'temurin'
18+
cache: gradle
19+
20+
- run: git config user.name 'tuancoltech'
21+
- run: git config user.email 'tuancoltech@gmail.com'
22+
23+
- run: ./gradlew utils:releaseClean
24+
- run: ./gradlew utils:releasePrepare -PbumpType=patch
25+
- run: ./gradlew utils:releasePerform
26+
env:
27+
GITHUB_TOKEN: ${{ github.token }}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ buildscript {
1717
classpath libs.kotlin.gradle.plugin
1818
classpath libs.hilt.gradle.plugin
1919
classpath libs.hilt.gradle.plugin
20+
classpath libs.grgit
2021
// NOTE: Do not place your application dependencies here; they belong
2122
// in the individual module build.gradle files
2223
}

gradle/libs.versions.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ appcompat = "1.7.0"
99
material = "1.12.0"
1010
hilt = "2.55"
1111
kotlinAndroidPlugin = "1.9.24"
12+
grgit = "5.0.0"
1213

1314
[libraries]
1415
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -24,5 +25,6 @@ hilt-gradle-plugin = { group = "com.google.dagger", name = "hilt-android-gradle-
2425
kotlin-gradle-plugin = { group = "org.jetbrains.kotlin", name = "kotlin-gradle-plugin", version.ref = "kotlinAndroidPlugin" }
2526
android-gradle-plugin = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" }
2627
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
28+
grgit = { group = "org.ajoberstar.grgit", name = "grgit-gradle", version.ref = "grgit" }
2729

2830
[plugins]

utils/build.gradle

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ plugins {
44
id 'kotlin-kapt'
55
id 'dagger.hilt.android.plugin'
66
id 'maven-publish'
7+
id 'org.ajoberstar.grgit'
78
}
89

910
android {
@@ -74,4 +75,135 @@ publishing {
7475
credentials(PasswordCredentials)
7576
}
7677
}
78+
}
79+
80+
tasks.register('ensureCleanRepo') {
81+
doLast {
82+
if (!grgit.repository.jgit.status().call().clean) {
83+
throw new GradleException('Git status is not clean, please stash your changes!')
84+
}
85+
}
86+
}
87+
88+
tasks.register('releaseClean') {
89+
dependsOn ensureCleanRepo
90+
doLast {
91+
def clean = true
92+
def applicationId = android.defaultConfig.applicationId
93+
94+
String headCommitMessage = grgit.head().shortMessage
95+
while (headCommitMessage.contains("[gradle-release-task]")) {
96+
clean = false
97+
println "Found git commit: $headCommitMessage"
98+
if (headCommitMessage.indexOf("$applicationId-") > -1) {
99+
def tagName = headCommitMessage.split("$applicationId-")[1]
100+
println "Removing the git tag: $tagName"
101+
try {
102+
grgit.tag.remove {
103+
names = [tagName]
104+
}
105+
} catch (Exception e) {
106+
println "Error while removing git tag:\n $e"
107+
}
108+
}
109+
println "Resetting the git commit permanently!"
110+
grgit.reset(commit: "HEAD~1", mode: "hard")
111+
headCommitMessage = grgit.head().shortMessage
112+
113+
}
114+
if (clean) {
115+
println "Repository is already clean"
116+
}
117+
println "Done!"
118+
}
119+
}
120+
121+
// Task parameters:
122+
// bumpVersion -> if available will specify new versionName directly and ignores the `bumpType` parameter.
123+
// bumpType[major|minor|patch] -> will specify how the version bumping occurs.
124+
tasks.register('releasePrepare') {
125+
dependsOn ensureCleanRepo
126+
doLast {
127+
def applicationId = android.defaultConfig.applicationId
128+
def versionName = android.defaultConfig.versionName
129+
130+
if (versionName.indexOf("-") > -1) {
131+
versionName = versionName.split("-")[0]
132+
}
133+
134+
// Prepare the release commit with the specific tag.
135+
String buildText = buildFile.getText()
136+
buildText = buildText.replaceFirst(/versionName(\s+.*)/, "versionName '$versionName'")
137+
buildFile.setText(buildText) //replace the build file's text
138+
grgit.add(patterns: ['utils/build.gradle'])
139+
try {
140+
grgit.commit(message: "[gradle-release-task] prepare release $applicationId-$versionName")
141+
} catch (Exception e) {
142+
throw new GradleException("Failed to commit, error:\n $e")
143+
}
144+
try {
145+
grgit.tag.add {
146+
name = versionName
147+
message = "Release of $versionName"
148+
}
149+
} catch (Exception e) {
150+
throw new GradleException("Failed to tag the repo, error:\n $e")
151+
}
152+
153+
// Set new version name from input parameters.
154+
def newVersionName
155+
if (project.properties.containsKey("bumpVersion")) {
156+
newVersionName = project.properties["bumpVersion"]
157+
println "Bumping the version directly (bumpVersion=$newVersionName)"
158+
} else if (project.properties.containsKey("bumpType")) {
159+
def (major, minor, patch) = versionName.tokenize('.')
160+
switch (bumpType) {
161+
case "major":
162+
major = major.toInteger() + 1
163+
minor = 0
164+
patch = 0
165+
break
166+
case "minor":
167+
minor = minor.toInteger() + 1
168+
break
169+
case "patch":
170+
patch = patch.toInteger() + 1
171+
break
172+
}
173+
newVersionName = "$major.$minor.$patch"
174+
} else {
175+
throw new GradleException('Either bumpType or bumpVersion parameters should be provided')
176+
}
177+
178+
// Prepare for next development iteration.
179+
def versionCode = android.defaultConfig.versionCode
180+
def newVersionCode = versionCode + 1
181+
println "Bumping versionName from $versionName to $newVersionName"
182+
println "Bumping versionCode from $versionCode to $newVersionCode"
183+
buildText = buildFile.getText()
184+
buildText = buildText.replaceFirst(/versionName(\s+.*)/, "versionName '$newVersionName-SNAPSHOT'")
185+
buildText = buildText.replaceFirst(/versionCode(\s+.*)/, "versionCode $newVersionCode")
186+
buildFile.setText(buildText) //replace the build file's text
187+
grgit.add(patterns: ['utils/build.gradle'])
188+
grgit.commit(message: "[gradle-release-task] prepare for next development iteration")
189+
println "Done!"
190+
}
191+
}
192+
193+
tasks.register('releasePerform') {
194+
dependsOn ensureCleanRepo
195+
doLast {
196+
boolean force = false
197+
if (project.properties.containsKey("force")) {
198+
force = project.properties["force"]
199+
}
200+
println "Pushing the newest commits to the remote repository (force: $force)"
201+
try {
202+
grgit.push(force: force, tags: true)
203+
} catch (Exception e) {
204+
throw new GradleException("Failed to push to the repo,\n" +
205+
" you can try using -Pforce=true parameter to force the push, error: \n$e")
206+
}
207+
println "Done!"
208+
}
77209
}

0 commit comments

Comments
 (0)