[Flutter] Deploy Flutter app witn GitHub Actions and Fastlane

2022-11-27 hit count image

Let's see how to deploy the Flutter app that is configured Fastlane by using GitHub Actions.

Outline

In the previous blog post, I will introduce how to use GitHub Actions to do the static analysis and execute the test code on the Flutter project.

Also, I’ve written a blog post about how to configure Fastlane on the Flutter project to deploy the iOS and Android apps to the App store.

In this blog post, I will show you how to use GitHub Actions to deploy the Flutter app that is configured Fastlane.

GitHub Actions file for deployment

To deploy the iOS and Android apps of the Flutter project to the App store, let’s create a GitHub Actions file. To create the GitHub Actions file, create ./.github/workflows/main.yml file in the Flutter project and modify it like the following.

name: Deploy iOS and Android App to App Store and Play Store
on:
  push:
    tags:
      - 'v*'

This GitHub Actions is executed when the Git tag that is started the v(v1.2.3) is added.

Next, let’s how to deploy the Flutter app by adding GitHub Actions for iOS and Android

GitHub Actions to execute Fastlane for iOS

Let’s create GitHub Actions to execute Fastlane which is configured to deploy Flutter apps to iOS’s App store. Open the ./.github/workflows/main.yml file and modify it like the following.

name: Deploy iOS and Android App to App Store and Play Store
on:
  ...
jobs:
  release-ios:
    name: Build and release iOS app
    runs-on: macos-latest
    steps:
      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: latest-stable
      - uses: actions/checkout@v3
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.3.8'
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'
      - name: Install Fastlane
        run: cd ios && bundle install && cd ..
      - name: Install packages
        run: flutter pub get
      - name: Install pods
        run: cd ios && pod install && cd ..
      - name: Prebuild
        run: flutter build ios --release --no-codesign
      - name: Execute Fastlane command
        run: cd ios && fastlane release type:github
        timeout-minutes: 40

Let’s see the details of the GitHub Actions above.

...
jobs:
  release-ios:
    name: Build and release iOS app
    runs-on: macos-latest
    ...

This GitHub Actions runs on the latest macOS (macos-lates).

...
jobs:
  release-ios:
    ...
    steps:
      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: latest-stable
    ...

And, install XCode for building and deploying the iOS app of the Flutter project.

...
jobs:
  release-ios:
    ...
    steps:
      ...
      - uses: actions/checkout@v3
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.3.8'
      ...

And then, install checkout for getting the code and Flutter for building the Flutter project.

...
jobs:
  release-ios:
    ...
    steps:
      ...
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'
      - name: Install Fastlane
        run: cd ios && bundle install && cd ..
      ...

Fastlane is developed by Ruby, so to install and execute Fastlane, we need to install Ruby. Install Fastlane and required libraries by using Ruby installed.

...
jobs:
  release-ios:
    ...
    steps:
      ...
      - name: Install packages
        run: flutter pub get
      - name: Install pods
        run: cd ios && pod install && cd ..
      ...

Next, install the required packages for building the Flutter project and libraries for iOS.

...
jobs:
  release-ios:
    ...
    steps:
      ...
      - name: Prebuild
        run: flutter build ios --release --no-codesign
      - name: Execute Fastlane command
        run: cd ios && fastlane release type:github
        timeout-minutes: 40

Lastly, use the Flutter command to build the iOS app of the Flutter project for the release. And then, deploy the app by using Fastlane.

Deploying an app using Fastlane takes a lot of time. I set the timeout to 40 minutes to ensure that I can use the free time of GitHub Actions up to 5 times.

GitHub Actions to execute Fastlane for Android

Let’s create GitHub Actions to execute Fastlane which is configured to deploy Flutter apps to Android’s Google Play. Open the ./.github/workflows/main.yml file and modify it like the following.

name: Publish iOS and Android App to App Store and Play Store
...
jobs:
  release-ios:
    ...
  release-android:
    name: Build and release Android app
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '12.x'
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.3.8'
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'
      - name: Install Fastlane
        run: cd android && bundle install && cd ..
      - name: Install packages
        run: flutter pub get
      - name: Prebuild
        run: flutter build appbundle
      - name: Execute Fastlane command
        run: cd android && fastlane release

Let’s see the details of the GitHub Actions above.

...
jobs:
  release-ios:
    ...
  release-android:
    name: Build and release Android app
    runs-on: ubuntu-latest
    ...

This GitHub Actions runs on the latest ubuntu (ubuntu-lates).

...
jobs:
  release-ios:
    ...
  release-android:
    ...
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '12.x'
      ...

And then, install checkout for getting the code and Java for building the Android app of the Flutter project.

...
jobs:
  release-ios:
    ...
  release-android:
    ...
    steps:
      ...
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.3.8'
      ...

And then, install Flutter for building the Flutter project.

...
jobs:
  release-ios:
    ...
  release-android:
    ...
    steps:
      ...
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'
      - name: Install Fastlane
        run: cd android && bundle install && cd ..
      ...

Fastlane is developed by Ruby, so to install and execute Fastlane, we need to install Ruby. Install Fastlane and required libraries by using Ruby installed.

...
jobs:
  release-ios:
    ...
  release-android:
    ...
    steps:
      ...
      - name: Install packages
        run: flutter pub get
      ...

Next, install the required packages for building the Flutter project.

...
jobs:
  release-ios:
    ...
  release-android:
    ...
    steps:
      ...
      - name: Prebuild
        run: flutter build appbundle
      - name: Execute Fastlane command
        run: cd android && fastlane release

Lastly, use the Flutter command to build the Android app of the Flutter project for the release. And then, deploy the app by using Fastlane.

GitHub Actions full code

The full code of GitHub Actions introduced above is like the following. Use this GitHub Actions code to deploy your Flutter app.

name: Deploy iOS and Android App to App Store and Play Store
on:
  push:
    tags:
      - 'v*'
jobs:
  release-ios:
    name: Build and release iOS app
    runs-on: macos-latest
    steps:
      - uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: latest-stable
      - uses: actions/checkout@v3
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.3.8'
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'
      - name: Install Fastlane
        run: cd ios && bundle install && cd ..
      - name: Install packages
        run: flutter pub get
      - name: Install pods
        run: cd ios && pod install && cd ..
      - name: Prebuild
        run: flutter build ios --release --no-codesign
      - name: Execute Fastlane command
        run: cd ios && fastlane release type:github
        timeout-minutes: 40
  release-android:
    name: Build and release Android app
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: 'zulu'
          java-version: '12.x'
      - uses: subosito/[email protected]
        with:
          flutter-version: '3.3.8'
      - uses: ruby/setup-ruby@v1
        with:
          ruby-version: '3.1.2'
      - name: Install Fastlane
        run: cd android && bundle install && cd ..
      - name: Install packages
        run: flutter pub get
      - name: Prebuild
        run: flutter build appbundle
      - name: Execute Fastlane command
        run: cd android && fastlane release

Execution

To execute GitHub Actions created above, you need to create a Git tag. Execute the following command to create the Git tag.

git tag -a v1.2.3 -m "release v1.2.3"

And then, execute the command below to push the created tag to GitHub.

git push origin v1.2.3

And then, you can see GitHub Actions that we’ve created above is running on the Actions tab in GitHub.

Completed

Done! we’ve seen how to use GitHub Actions for deploying the Flutter app that is configured Fastlane for the automation deployment. Next, try to implement deployment automation for your app by using Fastlane and GitHub Actions.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts