The personal finance app for everyone (by everyone)
Ruby
Generate Android version codes at build time (#4719)
Sure Admin (bot)ee1e32eb7eae
diff --git a/.github/workflows/flutter-build.yml b/.github/workflows/flutter-build.ymlindex 71636356db..46d7ec1444 100644--- a/.github/workflows/flutter-build.yml+++ b/.github/workflows/flutter-build.yml@@ -21,6 +21,11 @@ jobs:name: Build Android APKruns-on: ubuntu-latesttimeout-minutes: 30+ # Shared across all callers, not scoped to the tag or caller workflow.+ concurrency:+ group: android-build-number+ cancel-in-progress: false+ queue: maxoutputs:has_app_release_aab: ${{ steps.check_secrets.outputs.has_keystore }}@@ -60,6 +65,21 @@ jobs:printf '%s\n' "$version" > .sure-versionecho "SURE_BUILD_NAME=$version" >> "$GITHUB_ENV"+ - name: Resolve Android build number+ shell: bash+ run: |+ set -euo pipefail+ # Seconds since 2020-01-01 UTC: newer than the legacy 20260402 code,+ # with room below Google Play's limit until 2086. Allocate once per+ # build attempt, not from the tag date or workflow-specific run number.+ build_number=$(( $(date -u +%s) - 1577836800 ))+ if (( build_number <= 20260402 || build_number > 2100000000 )); then+ echo "::error::Android build number outside the supported range: $build_number"+ exit 1+ fi+ echo "SURE_BUILD_NUMBER=$build_number" >> "$GITHUB_ENV"+ echo "Android version code: $build_number"+- name: Get dependenciesworking-directory: mobilerun: flutter pub get@@ -110,10 +130,10 @@ jobs:run: |if [ "${{ steps.check_secrets.outputs.has_keystore }}" == "true" ]; thenecho "Building signed release APK..."- flutter build apk --release --build-name="$SURE_BUILD_NAME"+ flutter build apk --release --build-name="$SURE_BUILD_NAME" --build-number="$SURE_BUILD_NUMBER"elseecho "Building debug-signed APK..."- flutter build apk --debug --build-name="$SURE_BUILD_NAME"+ flutter build apk --debug --build-name="$SURE_BUILD_NAME" --build-number="$SURE_BUILD_NUMBER"fi- name: Upload APK artifact@@ -129,7 +149,7 @@ jobs:- name: Build App Bundle (Release)if: steps.check_secrets.outputs.has_keystore == 'true'working-directory: mobile- run: flutter build appbundle --release --build-name="$SURE_BUILD_NAME"+ run: flutter build appbundle --release --build-name="$SURE_BUILD_NAME" --build-number="$SURE_BUILD_NUMBER"- name: Upload AAB artifactif: steps.check_secrets.outputs.has_keystore == 'true'diff --git a/mobile/README.md b/mobile/README.mdindex 2fa582b26e..7c93708ac5 100644--- a/mobile/README.md+++ b/mobile/README.md@@ -198,6 +198,22 @@ This app provides a foundation for additional features:- Push notifications- Biometric authentication+## Android release version codes++Release CI leaves `pubspec.yaml` unchanged and supplies Flutter's `--build-name`+(CalVer for canonical releases) and `--build-number` (Android `versionCode`).+The shared `flutter-build.yml` allocates UTC seconds since 2020-01-01 once per+Android build attempt, using the same code for its APK and AAB. Android builds+are serialized across callers to avoid simultaneous allocations. A later rebuild,+including a retry of a run containing this workflow change, receives a newer code.+Uploading an already-built artifact preserves its original code; do not upload+older bundles after newer ones or reuse a code already accepted by Google Play.++The allocator rejects codes at or below the legacy `20260402` baseline or above+Google Play's `2100000000` limit (reached in 2086). It does not query Play Console:+any independently uploaded higher code must be accounted for before switching.+Local builds retain the pubspec fallback. iOS/TestFlight numbering is unchanged.+## LicenseThis project is distributed under the AGPLv3 license.diff --git a/mobile/pubspec.yaml b/mobile/pubspec.yamlindex b2c414ea0f..c793858e51 100644--- a/mobile/pubspec.yaml+++ b/mobile/pubspec.yaml@@ -1,6 +1,7 @@name: sure_mobiledescription: A mobile app for Sure personal finance managementpublish_to: 'none'+# Local fallback only. Release CI overrides the name and Android build number.version: 0.6.9+20260402environment:diff --git a/test/workflows/mobile_release_workflow_test.rb b/test/workflows/mobile_release_workflow_test.rbindex 0aa46471a5..c497917552 100644--- a/test/workflows/mobile_release_workflow_test.rb+++ b/test/workflows/mobile_release_workflow_test.rb@@ -66,7 +66,77 @@ class MobileReleaseWorkflowTest < ActiveSupport::TestCaseassert eligible?("workflow_dispatch", "refs/heads/main", "v2026.11.1-p202611011234")end+ test "Android build numbers advance across days years and retries of the same tag" do+ timestamps = %w[2026-11-03T23:59:59Z 2026-11-04T00:00:00Z 2026-12-31T23:59:59Z 2027-01-01T00:00:00Z]+ codes = timestamps.map do |timestamp|+ output, status, env = android_number(Time.iso8601(timestamp).to_i)+ assert status.success?, output+ code = env.delete_prefix("SURE_BUILD_NUMBER=").to_i+ assert_operator code, :>, 20260402+ assert_operator code, :<=, 2100000000+ code+ end+ assert_equal codes.sort.uniq, codes+ assert_equal 1, codes[1] - codes[0]+ assert_equal 1, codes[3] - codes[2]+ end++ test "Android rejects legacy or overflowing version codes" do+ [ 1577836800, 1577836800 + 20260402, 1577836800 + 2100000001 ].each do |timestamp|+ output, status, env = android_number(timestamp)+ assert_not status.success?, output+ assert_includes output, "outside the supported range"+ assert_empty env+ end+ output, status, = android_number(1577836800 + 2100000000)+ assert status.success?, output+ end++ test "Android APK and AAB commands receive the same dynamic build number" do+ job = workflow("flutter-build").fetch("jobs").fetch("build-android")+ assert_equal({ "group" => "android-build-number", "cancel-in-progress" => false, "queue" => "max" }, job.fetch("concurrency"))+ %w[true false].each do |signed|+ Dir.mktmpdir do |dir|+ File.write(File.join(dir, "flutter"), "#!/bin/bash\nprintf '%s\\n' \"$*\" >> \"$COMMAND_LOG\"\n")+ FileUtils.chmod(0755, File.join(dir, "flutter"))+ env = { "PATH" => "#{dir}:#{ENV.fetch('PATH')}", "COMMAND_LOG" => File.join(dir, "commands"),+ "SURE_BUILD_NAME" => "2026.11.3-alpha", "SURE_BUILD_NUMBER" => "216000001" }+ [ "Build APK (Release)", "Build App Bundle (Release)" ].each do |name|+ script = job.fetch("steps").find { |step| step["name"] == name }.fetch("run")+ script = script.gsub("${{ steps.check_secrets.outputs.has_keystore }}", signed)+ output, status = Open3.capture2e(env, "bash", "-e", "-c", script)+ assert status.success?, output+ end+ commands = File.readlines(env.fetch("COMMAND_LOG"), chomp: true)+ mode = signed == "true" ? "release" : "debug"+ assert_equal [ "build apk --#{mode} --build-name=2026.11.3-alpha --build-number=216000001",+ "build appbundle --release --build-name=2026.11.3-alpha --build-number=216000001" ], commands+ end+ end+ end++ test "Android release bundle steps require a keystore" do+ steps = workflow("flutter-build").fetch("jobs").fetch("build-android").fetch("steps")+ [ "Build App Bundle (Release)", "Upload AAB artifact" ].each do |name|+ step = steps.find { |candidate| candidate["name"] == name }+ assert_equal "steps.check_secrets.outputs.has_keystore == 'true'", step.fetch("if")+ end+ end+private+ def android_number(timestamp)+ Dir.mktmpdir do |dir|+ File.write(File.join(dir, "date"), "#!/bin/bash\necho #{timestamp}\n")+ FileUtils.chmod(0755, File.join(dir, "date"))+ env_path = File.join(dir, "env")+ script = workflow("flutter-build").fetch("jobs").fetch("build-android").fetch("steps")+ .find { |step| step["name"] == "Resolve Android build number" }.fetch("run")+ output, status = Open3.capture2e({ "PATH" => "#{dir}:#{ENV.fetch('PATH')}", "GITHUB_ENV" => env_path,+ "GITHUB_REF_NAME" => "v2026.11.3-alpha" }, "bash", "-c", script)+ [ output, status, File.exist?(env_path) ? File.read(env_path).strip : "" ]+ end+ end+def workflow(name)YAML.load_file(Rails.root.join(".github/workflows/#{name}.yml"))end