Home > Software engineering >  Codemagic CI: Get android version from Git Tag
Codemagic CI: Get android version from Git Tag

Time:04-06

I have a Flutter app that i'm running through CI using Codemagic to build out the app to iOS and Android. A big part of the CI is a semantic version control which takes my git commits and builds my version as a tag in gitlab. When these tags are created, this triggers the build CI which should take the tag and set the version on build.

For iOS this has not been a problem as in my pre-build script I can get the version and set it using the agvtools

echo "getting latest version..."
export NEW_VERSION_NAME=$((git describe --tags) | cut -c 2-)
NEW_IOS_VERSION_NAME=$((git describe --tags) | cut -c 2-6)
echo $NEW_VERSION_NAME
echo "VERSION_NAME=$NEW_VERSION_NAME" > app/version.properties
...
echo "updating ios version"
agvtool new-marketing-version $NEW_IOS_VERSION_NAME

but with Android I'm having much more trouble getting it work as there isn't any tools in Android Studio that will update the version in the pre-build. The only option in the CI is to pass the $NEW_VERSION_NAME into the flutter build apk --release --build-name=$NEW_VERSION_NAME since the variables don't seem to be passed between scripts.

instead I've found an example repo for android version control from Codemagic (https://github.com/codemagic-ci-cd/android-versioning-example) and instead am manipulating the version inside the app/build.gradle.

locally I got the following working where I get the tag from exec {} inside a kotlin function and then return the string:

def gitVersionName = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine 'git', 'describe', '--tags',
            standardOutput = stdout
        }
        println("Git Version: ${stdout.toString().trim()}")
        return stdout.toString().trim()
    }
    catch (ignored) {
        return null;
    }
}
...
android {
defaultConfig {
        applicationId "XXX"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode flutterVersionCode.toInteger()
        versionName gitVersionName() ?: flutterVersionName
    }
...

Locally this works, but when I try to run this in the Codemagic CI I get fatal: Not a valid object name printed in the Android Build. I can only assume that this means the terminal is not inside the git repo and so is not returning any git commands.

If someone knows how to CD into the correct directory inside kotlin exec then I could continue this method. or if someone has an alternative way to get the version from git tag then even better!

CodePudding user response:

I'd recommend to use the same approach as iOS build. You just need to save $NEW_VERSION_NAME to special $CM_ENV file at the end your script like

echo "NEW_VERSION_NAME=$NEW_VERSION_NAME" >> $CM_ENV

And it will allow you to use the variable for flutter build command

You can find more information and example here https://docs.codemagic.io/variables/using-environment-variables/#setting-an-environment-variable-on-macos-and-linux

  • Related