Flutter / Flutter Testing

Setting Up Continuous Integration for Flutter

This tutorial will guide you through the process of setting up Continuous Integration for your Flutter project. You'll learn how to automate your build and test process, ensuring …

Tutorial 5 of 5 5 resources in this section

Section overview

5 resources

Learn about testing framework and approaches in Flutter.

1. Introduction

In this tutorial, we are going to set up Continuous Integration (CI) for a Flutter project. CI is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests.

By the end of this tutorial, you will have a Flutter project with a CI pipeline that automatically builds and tests your application whenever you push changes to your repository.

Prerequisites:
- Basic knowledge of Flutter and Dart
- A Flutter project hosted on GitHub
- Familiarity with YAML files

2. Step-by-Step Guide

We will be using GitHub Actions, a CI/CD system provided by GitHub. It uses a YAML file in your repository to define the actions that should be taken whenever certain events occur.

Step 1: Create a GitHub Actions Workflow

Create a new file in your repository at .github/workflows/ci.yaml and open it in your text editor.

Step 2: Define the Workflow

In the ci.yaml file, define your workflow. Here's an example:

name: Flutter CI

on:
  push:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-java@v1
      with:
        java-version: '12.x'
    - uses: subosito/flutter-action@v1
      with:
        flutter-version: '1.22.x'
    - run: flutter pub get
    - run: flutter test
    - run: flutter build apk

This configuration will trigger the CI pipeline whenever you push to the master branch. It runs on the latest version of Ubuntu and executes the following steps:

  • Checks out your code
  • Sets up Java (required for Flutter)
  • Sets up Flutter (here version 1.22.x)
  • Installs dependencies (flutter pub get)
  • Runs tests (flutter test)
  • Builds the APK (flutter build apk)

Step 3: Push Your Changes

Once you've saved your ci.yaml file, add it to your repository and push your changes:

git add .github/workflows/ci.yaml
git commit -m "Setup CI"
git push

3. Code Examples

Let's add more actions to our workflow.

Example 1: Caching Dependencies

To avoid downloading dependencies every time the workflow runs, we can cache them:

# ... previous steps
- uses: actions/cache@v2
  with:
    path: "~/.pub-cache"
    key: ${{ runner.os }}-pub-cache-${{ hashFiles('**/pubspec.lock') }}
    restore-keys: |
      ${{ runner.os }}-pub-cache-
# ... next steps

This will cache the ~/.pub-cache directory (where Flutter dependencies are stored) and use the hash of pubspec.lock to know when the dependencies have changed.

Example 2: Uploading Artifacts

You might want to keep the APK that was built. You can do this by uploading it as an artifact:

# ... previous steps
- uses: actions/upload-artifact@v2
  with:
    name: app
    path: build/app/outputs/apk/release/app-release.apk

This will upload the APK built by flutter build apk and make it available on the GitHub Actions page for the workflow run.

4. Summary

You've learned how to setup a CI pipeline for a Flutter project using GitHub Actions. This pipeline will automatically build and test your app whenever you push changes, catching bugs early.

Next, you could look into setting up Continuous Deployment, which is the next step in the CI/CD process. There are many services available for this, including GitHub Pages, Firebase, and others.

For additional resources, check the GitHub Actions documentation and the Flutter documentation.

5. Practice Exercises

Exercise 1: Setup a Flutter project locally and push it to a new GitHub repository. Then, setup a CI pipeline that runs tests whenever you push changes.

Exercise 2: Extend the CI pipeline from Exercise 1 by adding caching of dependencies.

Solution: The solutions for these exercises will be the same as the steps and examples provided above. The only difference is that the exact contents of the Flutter project will vary. The important part is that you have a pubspec.yaml file and some Dart files to test.

Exercise 3: Extend the CI pipeline from Exercise 2 by building the APK and uploading it as an artifact.

Solution: The solution for this exercise will be the same as Example 2 above. The important part is that you add the actions/upload-artifact action after the flutter build apk command.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help