CODEMAGIC CI:从Git标签获取Android版本

发布于 2025-01-19 06:17:28 字数 1757 浏览 4 评论 0 原文

我有一个使用CODEMAGIC通过CI运行的Flutter应用程序,将该应用程序构建为iOS和Android。 CI的很大一部分是语义版本控件,它将我的git提交并构建我的版本作为Gitlab中的标签。创建这些标签时,这将触发构建CI,该构建CI应采用标签并在build上设置版本。

对于iOS来说,这不是一个问题,因为我可以在我的前构建脚本中获得版本并使用AgvTool进行设置,

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

但是使用Android,我在Android Studio中没有任何工具,因为Android Studio中没有任何工具将在预构建中更新版本。 CI中唯一的选项是将 $ new_version_name 传递到 flutter build apk-release -release-build-name = $ new_version_name ,因为变量似乎没有在脚本之间通过。

相反/Github.com/codemagic-ci-cd/android-versioning-example ),而是在操纵App/build.gradle中的版本。

在本地,我从 exec {} 内部获得了以下工作,然后在kotlin函数内部获得标签,然后返回字符串:

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
    }
...

本地效法,但是当我尝试在CodeMagic CI中运行此功能时,我会得到<代码>致命:不是在Android构建中打印的有效对象名称。我只能假设这意味着终端不在git存储库中,因此不会返回任何git命令。

如果有人知道如何将CD降为Kotlin Exec中的正确目录,那么我可以继续此方法。或者,如果某人有一种从git标签中获取版本的替代方法,那么更好!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迎风吟唱 2025-01-26 06:17:28

我建议使用与iOS构建相同的方法。您只需要保存
$ new_version_name to special $ cm_env 在您的脚本末尾的文件中

echo "NEW_VERSION_NAME=$NEW_VERSION_NAME" >> $CM_ENV

,它将允许您将变量用于 flutter build build 命令,

您可以找到更多信息和示例此处

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文