Chapters

Hide chapters

Real-World Flutter by Tutorials

First Edition · Flutter 3.3 · Dart 2.18 · Android Studio or VS Code

Real-World Flutter by Tutorials

Section 1: 16 chapters
Show chapters Hide chapters

15. Automating Test Executions & Build Distributions
Written by Vid Palčar

Heads up... You're reading this book for free, with parts of this chapter shown beyond this point as scrambled text.

Software development is always an iterative process. You’ll always need to update your app with bug fixes to your existing features and new features to improve your project. Mobile development in Flutter is no different. Quite a few repetitive tasks are connected to both bug fixes and feature development. You have to test the code by running automated tests, create a build for all the supported platforms and upload just-created builds to some platform where other team members, the QA team and end-users can test and use them. This can be very time-consuming. Therefore, in this chapter, you’ll learn how to automate these tests.

In software development, CI/CD are practices dealing with automating operating activities so you can focus on development activities. CI stands for continuous integration and covers automated test execution and building code artifacts such as Android builds. On the other hand, CD stands for continuous delivery, which primarily focuses on deploying code artifacts.

Automating all repetitive processes sounds great, but one important question must be answered to make it happen: How will the system know when to execute automated tests and build and deploy the apps? Keep reading to find out.

In this chapter, you’ll:

  • Learn how to use GitHub Actions to set up and execute CI/CD for your Flutter project.
  • Learn how to automate test execution.
  • Learn how to automate building mobile apps for both iOS and Android.
  • Become familiar with different development workflows and dive deeper into Gitflow.

Throughout this chapter, you’ll work on the starter project from this chapter’s assets folder. This chapter requires basic knowledge of Git. Therefore, if you don’t have experience using it, check out the Git Apprentice book.

Note: If you’re having trouble running the app, you might have forgotten to propagate the configurations you did in the first chapter’s starter project to the following chapters’ materials. If that’s the case, please revisit Chapter 1, “Setting up Your Environment”.

If you want to use the final project for this app, you still have to follow along with this chapter, as it requires a few additional steps to work. These include creating a GitHub repository, adding GitHub secrets, and generating access tokens and certificates for GitHub.

Software Development Workflows

The debate over the best way to accomplish something never ends. Therefore, this section will cover the software development workflows that are most commonly used in the developer community. When working on a project alone, it often feels like you don’t need strictly defined workflows. But when more people join your team or you try to automate some parts of your work, sticking to a workflow becomes much more important.

When developing software, you most often encounter two development workflows — Gitflow and trunk-based development.

Gitflow

The Gitflow development workflow uses multiple long-lived branches. To better understand the following explanation, look at the scheme below:

Hiop Nikquk Moogeqi Jepauko Nofuvad Xaumowi z5.8.1 f0.2.1 v3.6.0

Trunk-based Development

A trunk-based development workflow, on the other hand, keeps the stable code on the main branch. Work is divided into very small batches, which are developed on short-lived branches. Those branches are merged back to the main branch quickly, ensuring the building of code on a daily basis. This allows internal testers to provide developers with regular feedback, which contributes to more agile work. Despite merging often, this doesn’t solve the problems that occur due to merge conflicts, which occur during the phases of code reviews.

Faih Waches Xifcal Joopubo Seihafa Yiasovu s5.3.9 p8.1.6 c4.7.7 k4.2.5

Setting up CI/CD for a Flutter App

The workflow you choose for your app’s development will definitely affect the CI/CD pipeline, despite the general concepts always staying the same. With that being said, you’ll start by creating a new repository on GitHub named wonder_words:

git init
git remote add origin <REPOSITORY_URL>
git branch -M main
git add -f *
git commit -m 'Initial commit'
git push -u origin main
git checkout -b develop
git push -u origin develop

Creating Your First GitHub Action Job

Now, you can finally open the project and start programming. Start by creating two nested folders at the root of the project .github/workflows, and add the file named cicd.yml. Your folder structure should look something like this:

# 1
name: Test, build and deploy
# 2
on:
  pull_request:
    branches:
      - develop
  push:
    branches:
      - develop

# 3
permissions: read-all

# 4
jobs:
  # TODO: Remove the example job after testing
  # 5
  example:
    name: Example of a job
    # 6
    runs-on: ubuntu-latest
    # 7
    steps:
      # 8
      - name: Echo text
        run: echo This\ is\ my\ first\ Github\ actions\ job

Automating Test Execution

Now, as you understand the basics, you can start to move to the real deal. Locate # TODO: Remove the example job after testing, and replace the code below it with the following:

test:
  name: Test
  runs-on: ubuntu-latest
  steps:
    # 1
    - name: Clone flutter repository with master channel
      uses: subosito/flutter-action@v2
      with:
        channel: master

    # 2
    - name: Run flutter doctor
      run: flutter doctor -v

    # 3
    - name: Checkout code
      uses: actions/checkout@v2

    # 4
    - name: Get all packages and test
      run: make get && make testing
# TODO: add a job for building Android app

Automating Android Builds

As your tests have run through successfully, you may proceed with building the apps. You’ll start with the Android app. To do so, replace # TODO: add a job for building Android app with the following code snippet:

android:
  name: Build Android
  runs-on: ubuntu-latest
  steps:
    # 1
    - name: Clone flutter repository with master channel
      uses: subosito/flutter-action@v2
      with:
        channel: master
    - name: Run flutter doctor
      run: flutter doctor -v
    - name: Checkout code
      uses: actions/checkout@v2
    # 2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    # 3
    - name: Clean and fetch packages
      run: make clean && make get
    #4
    - name: Build apk
      run: flutter build apk
    # TODO: add deployment logic

Deploying Android App to Firebase App Distribution

Before continuing with coding, you need to take care of a few things. Go to your Firebase console, and locate the App Distribution tab in the left-hand sidebar. When opening it, this is what you should see:

npm install -g firebase-tools
firebase login:ci

- name: Firebase App Distribution
  uses: wzieba/Firebase-Distribution-Github-Action@v1
  with:
    appId: ${{ secrets.FIREBASE_APP_ID_ANDROID }}
    token: ${{ secrets.FIREBASE_CLI_TOKEN }}
    groups: ${{ secrets.TESTERS_GROUPS }}
    file: build/app/outputs/flutter-apk/app-release.apk

Automating iOS Builds and Deployment

Unfortunately, iOS build and deployment automation is much more complicated than Android. To build the app, you’re required to sign it with Apple developer certificates. The process is very time-consuming and requires installing the whole set of tools. Because this topic is almost broad enough for its own book, check the following video course covering building and deploying iOS apps using fastlane.

Key Points

  • By automating test execution, software building and deployment, you can save a lot of time by avoiding repetitive work.
  • The practice of automating operational tasks is called CI/CD, which stands for continuous integration and continuous delivery.
  • Multiple workflows make continuous integration and delivery more efficient. The most common are Gitflow and trunk-based development.
  • GitHub Actions is a great tool to help you execute automated testing, software building and deployment.
  • To make your life easier, use fastlane when implementing a pipeline for iOS app building. Check this video course to see what fastlane offers.

Where to Go From Here?

CI/CD is a very big topic; therefore, it’s worth exploring a bit deeper than was explained in this chapter. Take a look at how you can use fastlane to deploy both Android and iOS to Google Play and the App Store. Nevertheless, your end users will download your app from the stores, not from the Firebase App Distribution, which can be very convenient for an internal testing team. Also, the current implementation of the pipeline for Android has a smaller flaw; therefore, think about how you could implement an automatic build number incrementation for Android too.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.

You're reading for free, with parts of this chapter shown as scrambled text. Unlock this book, and our entire catalogue of books and videos, with a Kodeco Personal Plan.

Unlock now