Flutter: Gitlab CI /CD
Continuous Integration /Continuous Delivery within for a Flutter App can be achieved with various Platforms and Tools. In this article I will show a example which uses Gitlab with a shared runner. As usual a Flutter project has to be created connected to a Gitlap repository. With the first commit we can deploy a standard .yml file. This .yml file will be the starting point of this blog post.
- Setup the project with a .gitlab-ci.yml
- Get a docker:image
- Define your stages
- Setup a runner
Create .gitlab-ci.yml
Once you successfully created your project it is time to setup your .yml which will be executed by a selected runner from Gitlap. The name ist defined by Flutter.dev as .gitlab-ci.yml and it has to be saved in your root package.
Declare a docker:image
In order to build your Flutter Application you need to get a valid docker image. In this example I used the docker image cirrusci/flutter:stable.
image: cirrusci/flutter:stable
If you like to have a closer look into the docker image than please follow the link to Docker Hub.
Define your stages
Each stage in your .yml file will represent a specific task executed from the Gitlab runner. Here I used three stages which should be executed and successfully passed before a merge to a branch is possible.
stages:
- build
- analyze
- test
Stage 1: build
Within the build, the runner builds the project. Please keep in mind that the build command has additional subcommands for each target platform. Run the flutter build commad without parameters in your Terminal and you will find all available target platforms. Here I selected aot as target, which stands for: “Build an ahead-of-time compiled snapshot of your app’s Dart code”.
build:
stage: build
script:
- flutter build aot
tags:
- shared
only:
- merge_requests
Stage 2: test
All your implemented unit test will be executed, as long as the follow the path /root_package/test/*.
unit_test:
stage: test
script:
- flutter test test/*
tags:
- shared
only:
- merge_requests
Stage 3: analyze
The analyze stage has been left with a echo statement only. But in general it is possible to run the flutter analyze command in it. But if you encounter any linting errors your build will be failed.
analyze:
stage: analyze
script:
- echo job excluded
tags:
- shared
only:
- merge_requests
For each stage in your .gitlab-ci.yml file you can find a script, a tag and a only condition.
The tag is used in this example this, since this .yml file is executed with shared runner within GitLab. The script uses the only condition in order to ensure that only valid code can be merged between branches.
Full Example
image: cirrusci/flutter:stable
stages:
- build
- analyze
- test
before_script:
- flutter pub get
- flutter clean
build:
stage: build
script:
- flutter build aot
tags:
- shared
only:
- merge_requests
unit_test:
stage: test
script:
- flutter test test/*
tags:
- shared
only:
- merge_requestsanalyze:
stage: analyze
script:
- echo job excluded
tags:
- shared
only:
- merge_requests
Setup runner
Please the Gitlab documentation for the installation and registration of a Gitlab runner.
Happy coding …
Business Blog
The Villan Project
2D Animation Repository
Githup Repository