Skip to content

Commit

Permalink
Automatically set version name on build
Browse files Browse the repository at this point in the history
  • Loading branch information
Him188 committed Apr 5, 2024
1 parent 9760713 commit f971253
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 76 deletions.
60 changes: 1 addition & 59 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,64 +46,6 @@ jobs:
distribution: temurin
java-version: 17

# - name: Get current branch name and short SHA
# uses: satackey/[email protected]
# id: versionName # ${{ steps.versionName.outputs.versionName }}
# with:
# script: |
# const core = require('@actions/core')
#
# // Get branch name
# const ref = process.env.GITHUB_REF // e.g., refs/heads/master
# const branch = ref.split('/').pop() // e.g., master
# core.setOutput('branch', branch)
#
# // Get short commit SHA
# const shortSHA = process.env.GITHUB_SHA.substring(0, 8)
# core.setOutput('shortSHA', shortSHA)
#
# const fs = require('fs');
# const path = require('path');
#
# // Path to your gradle.properties file
# const gradlePropertiesPath = path.join(__dirname, 'gradle.properties');
#
# // Function to read the file and extract the version name synchronously
# function extractVersionNameSync(filePath) {
# try {
# // Read file contents synchronously
# const data = fs.readFileSync(filePath, 'utf8');
#
# // Regular expression to match the version name
# const regex = /version.name=(.+)-dev/;
# const match = data.match(regex);
#
# if (match && match[1]) {
# return match[1];
# } else {
# console.log('No version name found or does not match the pattern.');
# }
# } catch (err) {
# console.error('Error reading the file:', err);
# }
# }
#
# // Extract the version name from the gradle.properties file
# const version = extractVersionNameSync(gradlePropertiesPath);
#
# core.setOutput('versionName', version + branch + shortSHA)
#
# - name: Output current commit ref
# run: echo "The current commit ref is ${{ steps.versionName.outputs.versionName }}"

# - name: Set version
# uses: jacobtomlinson/gha-find-replace@v3
# with:
# find: "version.name=(.+)-dev"
# replace: "version.name=${{ steps.versionName.outputs.versionName }}"
# regex: true
# include: "gradle.properties"

- name: Setup Gradle
uses: gradle/gradle-build-action@v2

Expand Down Expand Up @@ -138,7 +80,7 @@ jobs:
run: ./gradlew clean ${{ env.gradleArgs }}

- name: Update version name
run: ./gradlew updateVersionNameFromGit ${{ env.gradleArgs }}
run: ./gradlew updateDevVersionNameFromGit ${{ env.gradleArgs }}


- if: ${{ env.isMac == 'true' }}
Expand Down
14 changes: 4 additions & 10 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,12 @@ jobs:
- if: ${{ env.isUnix == 'true' }}
run: chmod -R 777 *


- name: Set version
uses: jacobtomlinson/gha-find-replace@v3
with:
find: "version.name=(.+)-dev"
replace: "version.name=${{ steps.tag-version.outputs.substring }}"
regex: true
include: "gradle.properties"

- name: Clean and download dependencies
run: ./gradlew clean ${{ env.gradleArgs }}


- name: Update version name
run: ./gradlew updateReleaseVersionNameFromGit ${{ env.gradleArgs }}

- if: ${{ env.isMac == 'true' }}
name: Prepare Android Signing Key
id: android_signing_key
Expand Down
28 changes: 22 additions & 6 deletions ci-helper/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ open class ReleaseEnvironment {
): String {
return "$base-${branch}-${shaShort}"
}

fun generateReleaseVersionName(): String = tag
}

fun ReleaseEnvironment.uploadDesktopDistributions() {
Expand Down Expand Up @@ -361,17 +363,31 @@ fun ReleaseEnvironment.uploadDesktopDistributions() {
}
}

// ./gradlew updateVersionNameFromGit -DGITHUB_REF=refs/heads/master -DGITHUB_SHA=123456789 --no-configuration-cache
tasks.register("updateVersionNameFromGit") {
// ./gradlew updateDevVersionNameFromGit -DGITHUB_REF=refs/heads/master -DGITHUB_SHA=123456789 --no-configuration-cache
tasks.register("updateDevVersionNameFromGit") {
doLast {
val gradlePropertiesFile = rootProject.file("gradle.properties")
val properties = file(gradlePropertiesFile).readText()
val baseVersion =
(Regex("version.name=(.+)-").find(properties)
?: error("Failed to find base version. Check version.name in gradle.properties")).groupValues[1]
(Regex("version.name=(.+)").find(properties)
?: error("Failed to find base version. Check version.name in gradle.properties"))
.groupValues[1]
.substringBefore("-")
val new = ReleaseEnvironment().generateDevVersionName(base = baseVersion)
file(gradlePropertiesFile).writeText(
properties.replaceFirst(Regex("version.name=(.+)-dev"), "version.name=$new")
properties.replaceFirst(Regex("version.name=(.+)"), "version.name=$new")
)
}
}

// ./gradlew updateReleaseVersionNameFromGit -DGITHUB_REF=refs/heads/master -DGITHUB_SHA=123456789 --no-configuration-cache
tasks.register("updateReleaseVersionNameFromGit") {
doLast {
val gradlePropertiesFile = rootProject.file("gradle.properties")
val properties = file(gradlePropertiesFile).readText()
val new = ReleaseEnvironment().generateReleaseVersionName()
file(gradlePropertiesFile).writeText(
properties.replaceFirst(Regex("version.name=(.+)"), "version.name=$new")
)
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
kotlin.code.style=official
android.useAndroidX=true
org.gradle.jvmargs=-Xmx4000m -Dfile.encoding=UTF-8 --illegal-access=permit -Dkotlin.daemon.jvm.options=--illegal-access=permit --add-opens java.base/java.util=ALL-UNNAMED
# Project version, automatically updated by release.yml
# Project version, automatically updated by task :ci-helper:updateDevVersionNameFromGit
version.name=3.0.0-dev
# package.version must be major.minor.patch without meta
package.version=3.0.0
Expand Down

0 comments on commit f971253

Please sign in to comment.