I was wondering whether is it possible to have automatic versioning (following the SemVer convention) in Flutter, set in a GitLab/GitHub/BitBucket pipeline.
Obviously, all git commits will follow the Conventional Commits structure.
I searched a lot but could not find anything helpful. All I could find was a way to create automatic changelog based on the commits (but that is not my situation here).
Has anyone encountered the same problem and found a solution?
2
Answers
Yes you can by integrating Fastlane in your project
link : https://docs.flutter.dev/deployment/cd#fastlane
Yes, it’s definitely possible.
Step 1: Add Version Plugin
Add the
version
plugin to yourpubspec.yaml
file under thedev_dependencies
section:The
version
plugin provides a way to manage the version number of your application and generate version-related code.Step 2: Configure Version Plugin
Create a new file named
version.dart
in thelib/
directory of your project, and add the following code to it:This creates a
version
variable with an initial version number of0.1.0
. This version number will be automatically incremented based on the commits following the Conventional Commits structure.Step 3: Configure GitLab Pipeline
Create a
.gitlab-ci.yml
file in the root directory of your project and add the following code to it:This pipeline consists of two stages:
build
andrelease
. In thebuild
stage, we build the Flutter APK and save it as an artifact. In therelease
stage, we increment the version number using theversion patch
command, commit the changes to thepubspec.yaml
andlib/version.dart
files, and tag the commit with the new version number. Finally, we push the tag to the remote Git repository.Step 4: Configure GitLab Environment Variables
In order to use the
version
plugin, we need to set up two environment variables in the GitLab pipeline:FLUTTER_ROOT
: the path to the Flutter SDKPUB_CACHE
: the path to the pub cache directoryAdd the following environment variables to your GitLab project:
FLUTTER_ROOT
:/opt/flutter
PUB_CACHE
:$CI_PROJECT_DIR/.pub-cache
Step 5: Create a New Release
To create a new release, follow these steps:
Push a commit following the Conventional Commits structure. For example:
Create a new tag with the
v
prefix and the new version number. For example:Push the tag to the remote Git repository:
The GitLab pipeline will automatically trigger the
release
stage and increment the version number in theversion.dart
file, commit the changes, and tag the commit with the new version number.The new release APK will be available as an artifact in the GitLab pipeline, under the
build/app/outputs/flutter-apk/
directory.