最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - System.getenv returns the number plus 1 in Github Actions - Stack Overflow

programmeradmin1浏览0评论

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???

Share edited Mar 3 at 22:22 toshiomagic asked Mar 3 at 20:57 toshiomagictoshiomagic 1,6971 gold badge21 silver badges48 bronze badges 10
  • Any chance there's an implicit conversion to single precision float somewhere? – Andras Deak -- Слава Україні Commented Mar 3 at 21:11
  • Can you show the relevant parts of the getVersionCodeRelease.sh script? – Benjamin W. Commented Mar 3 at 21:16
  • And when you say "later on in the script", are you referring to the Actions workflow, or the same shell script? – Benjamin W. Commented Mar 3 at 21:17
  • What are the current contents of that file? – user207421 Commented Mar 3 at 21:58
  • I've updated the question with the requested information. Nothing fancy. – toshiomagic Commented Mar 3 at 22:23
 |  Show 5 more comments

1 Answer 1

Reset to default 0

We 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

发布评论

评论列表(0)

  1. 暂无评论