okaryo.log

Setting the Build Number Based on Commit Count When Building a Flutter App on GitHub Actions | okaryo.log

Setting the Build Number Based on Commit Count When Building a Flutter App on GitHub Actions

    #Flutter#GitHubActions#GitHub#CI/CD

Introduction

Recently, in the following articles, I mentioned that I was undecided on how to assign the build number when building Flutter apps. @idonuntius kindly suggested using the commit count as the build number.

I tried setting it up, and it worked wonderfully, so I’d like to introduce it.

Prerequisites

This method uses a library called cider, so let me give a brief introduction.

What is Cider?

Cider is a command-line utility for automating package management, available on pub.dev.

You can install it globally and use it as follows:

pub global activate cider

Here’s a simple usage example for the version we’ll be using:

# Get the current version
cider version

# Change the version in pubspec.yaml to 5.0.0+1
cider version 5.0.0+1

Besides this, cider can also handle CHANGELOG.md and version bumping. For more details, check out the official README.

GitHub Actions Setup

Now that the introduction to cider is done, let’s move on to the main topic.

Here’s the GitHub Actions configuration. Unnecessary steps and build arguments have been omitted.

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Set up Flutter
        uses: subosito/flutter-action@v2
        with:
          channel: 'stable'
          cache: true
      - name: Set up cider
        run: flutter pub global activate cider
      - name: Set up build number
        run: |
          VERSION=$(cider version)
          BUILD_NUMBER=$(git rev-list HEAD --count)
          cider version $VERSION+$BUILD_NUMBER
      - name: Build aab
        run: flutter build appbundle

Step-by-Step Explanation

actions/checkout@v3

This action checks out the repository. In this case, it’s necessary to specify fetch-depth: 0.

By default, only the most recent commit is fetched, but with this setting, you can retrieve the entire commit history. I’ve also written about this in the following article:

Allow GitHub Actions to Retrieve All Commit History

Set up Flutter

This step installs Flutter, so there’s nothing special to explain here.

Set up cider

This installs cider.

Set up build number

This step rewrites the build number in pubspec.yaml.

# Get the current app version
VERSION=$(cider version)

# Get the commit count and set it as the build number
BUILD_NUMBER=$(git rev-list HEAD --count)

# Use cider to rewrite the version in pubspec.yaml
# For example, it would become something like 3.0.0+500
cider version $VERSION+$BUILD_NUMBER

Note (2022-10-06 update)

If you’ve already released an app with a version number like 10000, this method might not work. I’ve written my own solution for this issue below:

Android App Version Codes Must Always Be Greater Than Previous Versions

End of note

Build aab

This step builds the Flutter app.

At this point, cider has already rewritten the version and build number in pubspec.yaml, so there’s no need to pass the build number as an argument.

Conclusion

With this setup, there’s no need to pass the build number as an argument during the build process, and there’s no need to manage the build number in GitHub Secrets anymore, making the process much more convenient.

Thank you, @idonuntius, for teaching me!


Related Posts
Related Posts
Promotion

This site uses Google Analytics.