I am trying to author a build script for Github Actions in yaml. It goes something like this:
jobs:
build-test-and-deploy:
runs-on: ubuntu-latest
timeout-minutes: 90
steps:
- name: Calculate Version Code
run: |
echo "VERSION_CODE=$(scripts/getVersionCodeRelease.sh ${{ github.run_number }})" >> $GITHUB_ENV
- name: Print Version COde
run: |
echo "VERSION_CODE=$VERSION_CODE"
The getVersionCodeRelease.sh
file contains the following script
date=$(date "+%y")
runNumber=$(printf "%06d\n" $runNumber)
versionCode="$date$runNumber"
echo $versionCode
Everything is fine up to this point. The script just grabs the year and adds some zeroes. So the above script prints the following output in GHA:
echo "VERSION_CODE=$(scripts/getVersionCodeRelease.sh 191)" >> $GITHUB_ENV
shell: /usr/bin/bash -e {0}
env:
JAVA_HOME: /opt/hostedtoolcache/Java_Zulu_jdk/17.0.14-7/x64
echo VERSION_CODE=2025000191
Later on in the GHA build script, I run ./gradlew bundleProdRelease -PdisablePreDex
(I've tried adding --no-configuration-cache
to this command, but it doesn't make a difference.)
This runs a build defined in my build.gradle.kts
which contains this kotlin function:
fun getVersionCodeOverride(): Int {
val versionCode = System.getenv("VERSION_CODE").parseIntOrDefault(1)
println("Version Code: $versionCode")
return versionCode
}
This prints 2025000192
to the console. WHAT? Why did it increment the value from 2025000191
to 2025000192
???
I am trying to author a build script for Github Actions in yaml. It goes something like this:
jobs:
build-test-and-deploy:
runs-on: ubuntu-latest
timeout-minutes: 90
steps:
- name: Calculate Version Code
run: |
echo "VERSION_CODE=$(scripts/getVersionCodeRelease.sh ${{ github.run_number }})" >> $GITHUB_ENV
- name: Print Version COde
run: |
echo "VERSION_CODE=$VERSION_CODE"
The getVersionCodeRelease.sh
file contains the following script
date=$(date "+%y")
runNumber=$(printf "%06d\n" $runNumber)
versionCode="$date$runNumber"
echo $versionCode
Everything is fine up to this point. The script just grabs the year and adds some zeroes. So the above script prints the following output in GHA:
echo "VERSION_CODE=$(scripts/getVersionCodeRelease.sh 191)" >> $GITHUB_ENV
shell: /usr/bin/bash -e {0}
env:
JAVA_HOME: /opt/hostedtoolcache/Java_Zulu_jdk/17.0.14-7/x64
echo VERSION_CODE=2025000191
Later on in the GHA build script, I run ./gradlew bundleProdRelease -PdisablePreDex
(I've tried adding --no-configuration-cache
to this command, but it doesn't make a difference.)
This runs a build defined in my build.gradle.kts
which contains this kotlin function:
fun getVersionCodeOverride(): Int {
val versionCode = System.getenv("VERSION_CODE").parseIntOrDefault(1)
println("Version Code: $versionCode")
return versionCode
}
This prints 2025000192
to the console. WHAT? Why did it increment the value from 2025000191
to 2025000192
???
1 Answer
Reset to default 0We have discovered the problem.
parseIntOrDefault(1)
from com.android.idemon.util
does not correctly convert a string to an integer. We logged both the string and the value after calling parseIntOrDefault(1)
and the values were sometimes different. We have changed the code to now be
val versionCode = System.getenv("VERSION_CODE").toIntOrNull(1) ?: 1
And it works
getVersionCodeRelease.sh
script? – Benjamin W. Commented Mar 3 at 21:16