Skip to content

Instantly share code, notes, and snippets.

@roger-tan
Last active June 25, 2019 17:32
Show Gist options
  • Save roger-tan/875eea91152131e8a93b3d2206cdd4cc to your computer and use it in GitHub Desktop.
Save roger-tan/875eea91152131e8a93b3d2206cdd4cc to your computer and use it in GitHub Desktop.

How to setup with Fastlane

Before to start, please ensure to install https://bundler.io on your local and ci machine so we can have a consistent environment between our machine for Ruby project installed.

To install Bundler for your local machine and ci.

$ gem install bundler

The next step is to generate a Gemfile from your local machine. So please run this following command:

$ bundler init

The last step will be setup our Gemfile by adding a Fastlane project

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

Now, we need to install Fastlane with bundler by running this command line.

$ bundle install

The next step will be to setup a Fastfile for Android so we can have a multiple Fastlane lane to build easily an android flutter app, distribute and why not get a slack or what do you need from Fastlane plugin. So let's create our Fastfile project.

$ bundle exec fastlane init

You will be asked to have for the package name, the path json for delivery app to Google Play Store (I won't cover the delivery for TestFlight or AppStore because the goal of the articles is to ensure that you understand the concept how to set up the ci for a flutter project with GitLab CI or alternative) and also metadata so you can automate the process of updating your metadata from your git repository.

For the part of the Fastfile, we will create a lane to build an 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

Let's test in our local machine if it's works by using this command line: $ bundle exec fastlane build

The next part will be to create a lane deploy. So first, we need to add a new plugin AppCenter for Fastlane so we can upload our apk to AppCenter

$ fastlane add_plugin appcenter

Now let's go back to out Fastfile to add new lane call deploy

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 let's run the new lane we just created

$ bundle exec fastlane deploy

The next part it's to update our .gitlab-ci.yml so we can use fastlane to build apk and deploy easily

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 updated work and you should see in our job pipeline using fastlane building android part.

The next part we will talk is the iOS world where there is a little bit work to do but thanks to fastlane to simplify the job!

First, let's copy the same Gemfile from the android. The recommendation by Google it's to have Fastlane for each platfrom. (https://flutter.dev/docs/deployment/fastlane-cd)

$ cd android & cp ../android/Gemfile $ bundle exec fastlane

Fastlane will ask "What would you like to use fastlane for?". Answer "4 - Manual setup - manually setup your project to automate your tasks"

The first part will be to setup the way to build an iOS app. We will use all the action from fastlane to make a build.

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

The next step will be to deploy from fastlane

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

And don't forget to commit and push. You should be able to run your lane from a local machine or CI machine. So it give your more options.

And that's it for all this series articles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment