'Android CI/CD with Github Actions and Fastlane

I am using fastlane to automate my deployments to the play store. Here is a alpha lane as an example.

lane :alpha do    
    gradle(task: 'clean')
    gradle(
      task: 'assemble',
      build_type: 'Release',
      properties: {
        "android.injected.signing.store.file" => ENV["ANDROID_KEYSTORE"],
        "android.injected.signing.store.password" => ENV["ANDROID_KEYSTORE_PASSWORD"],
        "android.injected.signing.key.alias" => ENV["ANDROID_KEY_ALIAS"],
        "android.injected.signing.key.password" => ENV["ANDROID_KEY_PASSWORD"],
      }
    )
    upload_to_play_store(track: 'alpha')
end

I want to automate this deployment with Github actions. Whenever a commit to staging occurs, run fastlane alpha. The issue I am running into, however, is versioning. I need to bump the versionCode in my build.gradle file. I have been doing this manually before running fastlane alpha.

I want to remove the need to bump this value manually before I commit, how can I achieve this?

I have seen this fastlane plugin to increment the version code. If I, for example, commit once while the versionCode is at 1, then my workflow will automatically increment the versionCode to 2. When I commit again, the versionCode will still be 1, and Github will bump it to 2. However, the play store needs versionCode 3. The ideal solution is to have Github query from the Google Play Store the current versionCode of my app and supply that + 1 to fastlane. I have searched for a way to do this but cannot find a solution.



Solution 1:[1]

You can add a lane to do this as described in this Medium post by Atul Anand:

lane:IncrementBuildNumber do

    path = '../app/build.gradle'
    re = /versionCode\s+(\d+)/
    s = File.read(path)
    versionCode = s[re, 1].to_i
    s[re, 1] = (versionCode + 1).to_s
    f = File.new(path, 'w')
    f.write(s)
    f.close

end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Patrick Kenny