How to build and deploy Flutter apps with Gitlab pipeline on Playstore and Appstore
This post is targeted to those who possess a basic knowledge of Flutter and wish understand the process of setting up tools that support CI/CD practic
Hello there, in this article, we will look at how to write a GitLab pipeline to build and deploy our Flutter apps to Playstore and Appstore.
One of the most popular development methodologies in software development is continuous integration or CI. By automatically constructing, testing, and delivering earlier feedback, modern continuous integration systems help development teams feel more confident.
The process after Continuous Integration is called Continuous Delivery (CD). Every time a code change is made to the codebase, your application is not only built and tested but also continually deployed as an additional step, even though the deployments are manually initiated.
The Gitlab Runners are the build executors used by Gitlab CI. They are only the devices on which your pipeline will run. Installing, configuring (if necessary), and registering Gitlab Runner on a computer—preferably a Mac computer—will be necessary for your Flutter pipeline to run in order to support the iOS builds in Flutter with Android.
You can install software for the Gitlab Runner and register your mac machine following this documentation:
https://docs.gitlab.com/runner/install/osx.html
https://docs.gitlab.com/runner/register/
NOTE: In this example, we have used shell as our executor inside the runner.
This is equivalent to our pipeline being executed directly in a shell inside our Gitlab CI Runner machine.
Now, you are all set to use a Gitlab pipeline. But in order for the build to take place, you will need some of these essential building tools installed on your GitLab runner machine. These are listed in the Prerequisites.
Prerequisites:
- Gitlab Repository containing your code
- Homebrew
- Flutter
- Android Studio
- Xcode
- The certificates and mobileprovisions required for the project build installed on the Gitlab CI machine
Finally, all you require is a Gitlab pipeline.
Gitlab utilizes a file at the project root called ".gitlab-ci.yml" (don't forget the dot as well as the case) to determine whether to execute a pipeline for a certain repository. The .gitlab-ci.yml file contains the pipeline settings that Gitlab uses.
To build and deploy for android, here's the script:
image: flutter/flutter:stable
stages:
- build
- deploy
build:
stage: build
script:
- flutter doctor --android-licenses
- flutter clean
- flutter build appbundle
artifacts:
paths:
- "**/**/**/**/**/**/*.appbundle"
expire_in: 1 day
deploy:
stage: deploy
script:
- gcloud auth activate-service-account --key-file=<service-account-key.json>
- gcloud config set project <project-id>
- gcloud firebase app dist-tags <app-id> --version=1.0.$CI_PIPELINE_ID
In this script, we first specify the flutter/flutter:stable Docker image as the environment for the pipeline. Then, we define two stages: build and deploy.
In the build stage, we run the flutter build apk/appbundle command to build the APK/AppBundle file.
In the deploy stage, we authenticate the service account, set the project, and use the gcloud firebase app dist-tags command to upload the APK/AppBundle file to the Play Store. Make sure to replace the placeholder values with the actual values for your project.
and for ios
image: flutter/flutter:stable
stages:
- build
- deploy
build:
stage: build
script:
- flutter clean
- flutter pub get
- flutter build ios
- cd ios
- pod install;
artifacts:
paths:
- "artifact/*.ipa"
deploy:
stage: deploy
script:
- gem install fastlane
- fastlane release
In this script, we first specify the flutter/flutter:stable Docker image as the environment for the pipeline. Then, we define two stages: build and deploy.
In the build stage, we run the flutter build ios command to build the iOS app.
In the deploy stage, we install fastlane, a tool that automates the process of releasing mobile apps to the App Store. We then run the fastlane release command to build and upload the app to the App Store.
Make sure to set up fastlane and configure your App Store credentials before running this pipeline. You can find detailed instructions on how to do this in the fastlane documentation.
That's all for now 🥳. If you have any questions, kindly leave a comment or connect with me on Twitter (Etornam Sunu)