Setup a Flutter CI/CD with GitLab CI — Part 5

Scripting the CD pipeline with Fastlane

Roger Tan
Kin + Carta Created
4 min readOct 8, 2019

--

GitLab CI/CD + Flutter illustrations

In the previous post, we learnt how to distribute apps using App Center.

In this article, we will describe the process for scripting CD using Fastlane.

Setting up Fastlane

Fastlane logo

Before we begin, ensure Bundler is installed on your local and CI machine.

Next step is to generate a Gemfile on your local machine. Run the following command-line:
$ bundler init

Then open and edit the Gemfile by adding the following below.

source "https://rubygems.org"
gem "fastlane"

Then, install Fastlane with Bundler by running the following command line.
$ bundle install

Setting up Fastlane for Android

The next step will be to create a Fastfile for Android by running the following command lines.
$ cd android
$ bundle exec fastlane init

You will then be asked several questions which need to be answered (eg. define the package name, etc…).

Within the Fastfile, paste the following text below which will create a lane to build the APK.

default_platform(:android)
platform :android do
desc "Build a Flutter apk"
lane :build do
Dir.chdir("../..") do
sh "flutter packages get"
sh "flutter clean"
sh "flutter build apk"
end
end
end

Now, let’s run the following command line to build the APK.
$ bundle exec fastlane build

After the APK is created a new fastlane plugin needs to be installed so that the APK can be deployed to the App Center. Run the following command line shown below.
$ fastlane add_plugin appcenter

After installation, edit the Fastfile to add a new lane called deploy, beneath the build lane by pasting the following text (in bold).

default_platform(:android)
platform :android do
desc "Build a Flutter apk"
lane :build do
### BUILDING ###
end
desc "Submit a new Beta Build to AppCenter"
lane :deploy do
if File.exist?(File.dirname(__FILE__) + "./../build/app/outputs/apk/release/app-release.apk")
appcenter_upload(
api_token: "APP_CENTER_TOKEN", # Your secret api token from app center
owner_name: "APP_CENTER_USERNAME", # Your username on AppCenter
app_name: "APP_CENTER_APP_NAME", # Your AppCenter name
apk: "../build/app/outputs/apk/release/app-release.apk",
)
else
UI.message('Unable to find apk')
end
end
end

Now, run the following command line.
$ bundle exec fastlane deploy

The APK should now be deployed to the App Center from your local machine.

You will then need to edit.gitlab-ci.yml by adding and updating the values beneath the before_script and script sections shown below (in bold).

flutter_build_android:
stage: build
before_script:
- cd android
- bundle install --deployment
script:
- bundle exec fastlane build

artifacts:
paths:
- build/app/outputs/apk/release/app-release.apk
tags:
- flutter
deploy_android:
stage: deploy
before_script:
- cd android
- bundle install --deployment
dependencies:
- flutter_build_android
script:
- bundle exec fastlane deploy_beta
tags:
- flutter

Commit and push your changes. You will see the CI and CD pipeline running.

In the next step, we will setup iOS CI/CD which requires a little bit more work.

Setting up Fastlane for iOS

Google recommends having Fastlane for each platform (https://flutter.dev/docs/deployment/fastlane-cd)

Go to the iOS directory of your Flutter project and run the following command line below.
$ bundle exec fastlane

Fastlane will ask "What would you like to use fastlane for?".
Your answer should be "4.🛠 Manual setup - manually setup your project to automate your tasks"

Within the Fastfile, paste the following text below, this will create a lane which will build the IPA.

default_platform(:ios)
platform :ios do
desc "Build an ipa"
lane :build do
Dir.chdir("../..") do
sh "flutter packages get"
sh "flutter clean"
sh "flutter build ios"
end
build_ios_app(
scheme: "Runner",
clean: true,
output_directory: './build',
export_options: {
method: "adhoc", #adhoc or enterprise
provisioningProfiles: {
"BUNDLE_IDENTIFIER" => "PROVISIONNING_PROFILES_NAME",
}
}
)
end
end

Don’t forget to update PROVISIONNING_PROFILES_NAME with the relevant value.

Next, edit the Fastfile by adding a new lane called deploy, beneath the build lane section by pasting the following text (in bold).

default_platform(:ios)
platform :ios do
# other lanes #
desc "Submit a new iOS build to AppCenter"
lane :deploy do
if File.exist?(File.dirname(__FILE__) + "./build/Runner.ipa")
appcenter_upload(
api_token: "APP_CENTER_TOKEN", # Your secret api token from app center
owner_name: "APP_CENTER_USERNAME", # Your username on AppCenter
app_name: "APP_CENTER_APP_NAME", # Your AppCenter name
ipa: "./build/Runner.ipa",
)
else
UI.message('Unable to find ipa')
end
end
end

Now, commit and push the changes. You will now see the CI and CD pipeline running for iOS.

And that’s it for this series. You’ve now learnt how CI/CD works within a Flutter project.

--

--