Yes, it's definitely possible.
Step 1: Add Version Plugin
Add the version plugin to your pubspec.yaml file under the dev_dependencies section:
dev_dependencies:
version: ^2.0.0
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 the lib/ directory of your project, and add the following code to it:
import 'package:version/version.dart';
final version = Version(0, 1, 0);
This creates a version variable with an initial version number of 0.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:
stages:
- build
- release
flutter_build:
stage: build
image: cirrusci/flutter:stable
script:
- flutter packages get
- flutter build apk
artifacts:
paths:
- build/app/outputs/flutter-apk/app-release.apk
release:
stage: release
image: cirrusci/flutter:stable
script:
- flutter packages get
- flutter pub get
- version patch
- git add pubspec.yaml lib/version.dart
- git commit -m "Bump version to \${CI_COMMIT_TAG}"
- git tag -a "\${CI_COMMIT_TAG}" -m "Version \${CI_COMMIT_TAG}"
- git push origin "\${CI_COMMIT_TAG}"
This pipeline consists of two stages: build and release. In the build stage, we build the Flutter APK and save it as an artifact. In the release stage, we increment the version number using the version patch command, commit the changes to the pubspec.yaml and lib/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 SDK
PUB_CACHE: the path to the pub cache directory
Add 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:
feat: add new feature
Create a new tag with the v prefix and the new version number. For example:
git tag -a v0.1.1 -m "Version 0.1.1"
Push the tag to the remote Git repository:
git push origin v0.1.1
The GitLab pipeline will automatically trigger the release stage and increment the version number in the version.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.