Skip to content

Commit

Permalink
🔖 Prepare automatic releaser
Browse files Browse the repository at this point in the history
Closes #784

Signed-off-by: Leonardo Colman Lopes <dev@leonardo.colman.com.br>
  • Loading branch information
LeoColman committed Jan 13, 2025
1 parent e2e805b commit 3a10548
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 11 deletions.
37 changes: 35 additions & 2 deletions .github/workflows/release.main.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import io.github.typesafegithub.workflows.actions.gradle.GradleBuildAction
import io.github.typesafegithub.workflows.actions.ruby.SetupRuby
import io.github.typesafegithub.workflows.actions.softprops.ActionGhRelease
import io.github.typesafegithub.workflows.domain.RunnerType.UbuntuLatest
import io.github.typesafegithub.workflows.domain.triggers.Push
import io.github.typesafegithub.workflows.domain.triggers.WorkflowDispatch
import io.github.typesafegithub.workflows.domain.triggers.WorkflowDispatch.Input
import io.github.typesafegithub.workflows.domain.triggers.WorkflowDispatch.Type.String
import io.github.typesafegithub.workflows.dsl.expressions.Contexts
import io.github.typesafegithub.workflows.dsl.expressions.expr
import io.github.typesafegithub.workflows.dsl.workflow
Expand All @@ -28,14 +30,45 @@ val GITHUB_REF_NAME by Contexts.github

workflow(
name = "Release",
on = listOf(Push(tags = listOf("*"))),
on = listOf(
WorkflowDispatch(
inputs = mapOf(
"version_type" to Input(
description = "Type of version bump (major, minor, patch)",
required = true,
type = WorkflowDispatch.Type.Choice,
options = listOf("major", "minor", "patch")
),
"changelog" to Input(
description = "Changelog content for this release",
required = true,
type = String,
)
)
)
),
sourceFile = __FILE__
) {
job(id = "create-apk", runsOn = UbuntuLatest) {
uses(name = "Set up JDK", action = SetupJava(javaVersion = "17", distribution = SetupJava.Distribution.Adopt))
run(
name = "Set up Kotlin",
command = """
sudo apt-get update &&
sudo apt-get install -y kotlin
""".trimIndent()
)
uses(action = Checkout())
uses(name = "reveal-secrets", action = GitSecretAction(gpgPrivateKey = expr { GPG_KEY }))

val versionTypeExpr = expr { github["event.inputs.version_type"]!! }
val changelogExpr = expr { github["event.inputs.changelog"]!! }
run(
name = "Run Bump Version Script",
command = "app/bump_version.main.kts $versionTypeExpr $changelogExpr"
)


uses(name = "Create APK", action = GradleBuildAction(arguments = "assembleGithubRelease"))

uses(
Expand Down
37 changes: 28 additions & 9 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@

name: 'Release'
on:
push:
tags:
- '*'
workflow_dispatch:
inputs:
version_type:
description: 'Type of version bump (major, minor, patch)'
type: 'choice'
required: true
options:
- 'major'
- 'minor'
- 'patch'
changelog:
description: 'Changelog content for this release'
type: 'string'
required: true
jobs:
check_yaml_consistency:
name: 'Check YAML consistency'
Expand All @@ -33,18 +44,26 @@ jobs:
java-version: '17'
distribution: 'adopt'
- id: 'step-1'
uses: 'actions/checkout@v4'
name: 'Set up Kotlin'
run: |-
sudo apt-get update &&
sudo apt-get install -y kotlin
- id: 'step-2'
uses: 'actions/checkout@v4'
- id: 'step-3'
name: 'reveal-secrets'
uses: 'entrostat/git-secret-action@v4'
with:
gpg-private-key: '${{ secrets.GPG_KEY }}'
- id: 'step-3'
- id: 'step-4'
name: 'Run Bump Version Script'
run: 'app/bump_version.main.kts ${{ github.event.inputs.version_type }} ${{ github.event.inputs.changelog }}'
- id: 'step-5'
name: 'Create APK'
uses: 'gradle/gradle-build-action@v3'
with:
arguments: 'assembleGithubRelease'
- id: 'step-4'
- id: 'step-6'
name: 'Create release'
uses: 'softprops/action-gh-release@v2'
with:
Expand All @@ -57,16 +76,16 @@ jobs:
app/build/outputs/mapping/githubRelease/configuration.txt
app/build/outputs/mapping/githubRelease/seeds.txt
app/build/outputs/mapping/githubRelease/usage.txt
- id: 'step-5'
- id: 'step-7'
name: 'Create Bundle'
uses: 'gradle/gradle-build-action@v3'
with:
arguments: 'clean bundlePlaystoreRelease'
- id: 'step-6'
- id: 'step-8'
uses: 'ruby/setup-ruby@v1'
with:
ruby-version: '3.2.3'
- id: 'step-7'
- id: 'step-9'
name: 'Publish to Playstore'
working-directory: 'fastlane'
run: |-
Expand Down
65 changes: 65 additions & 0 deletions app/bump_version.main.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash kotlin

import java.io.File
import kotlin.system.exitProcess

val buildGradleFile = File("./build.gradle.kts")
val changelogs = "../fastlane/metadata/android/en-US/changelogs"
val buildGradleContent = File("./build.gradle.kts").readText()

val versionCodeRegex = Regex("""versionCode\s*=\s*(\d+)""")
val versionNameRegex = Regex("""versionName\s*=\s*"([\d.]+)"""")

val currentVersionCode = versionCodeRegex.find(buildGradleContent)!!.groupValues[1].toInt()
val currentVersionName = versionNameRegex.find(buildGradleContent)!!.groupValues[1]

println("Current version: $currentVersionName ($currentVersionCode)")
println("Detecting bump type...")
var bumpType = args.getOrNull(0)
if(bumpType == null) {
println("BUMP_TYPE not set, input one of: major, minor, patch")
exitProcess(1)
}

check(bumpType in listOf("major", "minor", "patch")) { "BUMP_TYPE must be one of: major, minor, patch" }


val versionParts = currentVersionName.split(".").map { it.toIntOrNull() ?: 0 }
val (newMajor, newMinor, newPatch) = when (bumpType) {
"major" -> Triple(versionParts[0] + 1, 0, 0)
"minor" -> Triple(versionParts[0], versionParts[1] + 1, 0)
"patch" -> Triple(versionParts[0], versionParts[1], versionParts[2] + 1)
else -> throw IllegalStateException("Unknown bump type: $bumpType")
}
val newVersionName = "$newMajor.$newMinor.$newPatch"
println("New version: $newVersionName")




val newVersionCode = "${newMajor * 1000000 + newMinor * 1000 + newPatch}"
println("New version code: $newVersionCode")


val updatedContent = buildGradleContent
.replace(versionCodeRegex, "versionCode = $newVersionCode")
.replace(versionNameRegex, "versionName = \"$newVersionName\"")

buildGradleFile.writeText(updatedContent)


val changelogFile = File(changelogs, "$newVersionCode.txt")
if (!changelogFile.exists()) {
changelogFile.createNewFile()
}

val changelog = args.getOrNull(1)
if(changelog != null) {
println("Writing changelog...")
changelogFile.writeText(changelog)
}

ProcessBuilder("git", "add", buildGradleFile.absolutePath, changelogFile.absolutePath).inheritIO().start().waitFor()
ProcessBuilder("git", "commit", "-m", "🔖 Prepare Release $newVersionName ($newVersionCode)").inheritIO().start().waitFor()
ProcessBuilder("git", "tag", "-a", newVersionName, "-m", "Release version $newVersionName").inheritIO().start().waitFor()
ProcessBuilder("git", "push", "origin", "main", "--tags").inheritIO().start().waitFor()

0 comments on commit 3a10548

Please sign in to comment.