Skip to content

Commit 76772c3

Browse files
authored
Merge pull request #125 from elimu-ai/124-create-git-tag-on-push-to-main
chore: create git tag on push to `main`
2 parents cf5a504 + 48c8c9f commit 76772c3

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: Gradle Release
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
7+
jobs:
8+
create_git_tag:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v4
12+
- uses: actions/setup-java@v4
13+
with:
14+
distribution: temurin
15+
java-version: 17
16+
- uses: gradle/gradle-build-action@v3
17+
18+
- name: Git Config
19+
run: |
20+
git config user.name 'Nya Ξlimu'
21+
git config user.email 'info@elimu.ai'
22+
23+
- name: Remove -SNAPSHOT from versionName
24+
run: ./gradlew removeSnapshot
25+
26+
- name: Set the new versionName as an environment variable
27+
id: get_version_name
28+
run: |
29+
./gradlew getVersionName --quiet --console=plain
30+
VERSION_NAME="$(./gradlew getVersionName --quiet --console=plain)"
31+
echo "::set-output name=version_name::$VERSION_NAME"
32+
33+
- name: Git Commit
34+
run: |
35+
git add app/build.gradle
36+
echo "version_name: ${{ steps.get_version_name.outputs.version_name }}"
37+
git commit -m "chore: prepare release ${{ steps.get_version_name.outputs.version_name }}"
38+
39+
- name: Git Tag
40+
run: |
41+
echo "version_name: ${{ steps.get_version_name.outputs.version_name }}"
42+
git tag ${{ steps.get_version_name.outputs.version_name }}
43+
44+
- name: Git Push Tag
45+
run: |
46+
echo "version_name: ${{ steps.get_version_name.outputs.version_name }}"
47+
git push origin ${{ steps.get_version_name.outputs.version_name }}
48+
49+
- name: Bump version
50+
run: ./gradlew bumpVersion
51+
52+
- name: Add -SNAPSHOT to versionName
53+
run: ./gradlew addSnapshot
54+
55+
- name: Git Commit
56+
run: |
57+
git add app/build.gradle
58+
git commit -m "chore: prepare for next development iteration"
59+
60+
- name: Git Push
61+
run: |
62+
git push

app/build.gradle

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import java.util.regex.Matcher
2+
import java.util.regex.Pattern
3+
14
apply plugin: 'com.android.application'
25
apply plugin: 'com.mxalbert.gradle.jacoco-android'
36
apply plugin: 'dagger.hilt.android.plugin'
@@ -89,3 +92,68 @@ publishing {
8992
tasks.named("publishApkPublicationToMavenLocal") {
9093
mustRunAfter(":app:assembleRelease")
9194
}
95+
96+
task removeSnapshot {
97+
doLast {
98+
println("removeSnapshot")
99+
100+
def file = file("build.gradle")
101+
def originalFileContent = file.getText()
102+
103+
Pattern pattern = Pattern.compile("versionName \"\\d+\\.\\d+\\.\\d+-SNAPSHOT\"")
104+
Matcher matcher = pattern.matcher(originalFileContent)
105+
matcher.find()
106+
println("match: ${matcher.group()}")
107+
108+
def newVersionName = matcher.group().replace("-SNAPSHOT", "")
109+
println("newVersionName: ${newVersionName}")
110+
111+
def newFileContent = originalFileContent.replaceFirst("versionName \"\\d+\\.\\d+\\.\\d+-SNAPSHOT\"", newVersionName)
112+
file.write(newFileContent)
113+
}
114+
}
115+
task getVersionName {
116+
doLast {
117+
println android.defaultConfig.versionName
118+
}
119+
}
120+
task bumpVersion {
121+
doLast {
122+
println("bumpVersion")
123+
124+
def currentVersionCode = android.defaultConfig.versionCode
125+
println("currentVersionCode: ${currentVersionCode}")
126+
127+
def newVersionCode = currentVersionCode + 1
128+
println("newVersionCode: ${newVersionCode}")
129+
130+
def newVersionName = newVersionCode.toString().substring(0, 1).toInteger() + "." + newVersionCode.toString().substring(1, 4).toInteger() + "." + newVersionCode.toString().substring(4, 7).toInteger()
131+
println("newVersionName: ${newVersionName}")
132+
133+
def file = file("build.gradle")
134+
def originalFileContent = file.getText()
135+
def newFileContent = originalFileContent.replaceFirst("versionCode \\d+", "versionCode ${newVersionCode}")
136+
newFileContent = newFileContent.replaceFirst("versionName \"\\d+\\.\\d+\\.\\d+\"", "versionName \"${newVersionName}\"")
137+
file.write(newFileContent)
138+
}
139+
}
140+
task addSnapshot {
141+
doLast {
142+
println("addSnapshot")
143+
144+
def file = file("build.gradle")
145+
def originalFileContent = file.getText()
146+
147+
Pattern pattern = Pattern.compile("versionName \"\\d+\\.\\d+\\.\\d+\"")
148+
Matcher matcher = pattern.matcher(originalFileContent)
149+
matcher.find()
150+
println("match: ${matcher.group()}")
151+
152+
def newVersionName = "${matcher.group().substring(12, matcher.group().length() - 1)}-SNAPSHOT\""
153+
println("newVersionName: ${newVersionName}")
154+
155+
def newFileContent = originalFileContent.replaceFirst("versionName \"\\d+\\.\\d+\\.\\d+\"", "versionName ${newVersionName}")
156+
file.write(newFileContent)
157+
}
158+
}
159+

0 commit comments

Comments
 (0)