Skip to main content

Using Continuous Integration

1. Setting Up CI/CD Pipelines

Example: GitHub Actions Workflow

name: Flutter CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.19.0"
      - name: Install dependencies
        run: flutter pub get
      - name: Run tests
        run: flutter test
      - name: Build APK
        run: flutter build apk --release
Step Description Command Notes
Checkout Code Fetches the repository code actions/checkout@v3 Required for all CI workflows
Setup Flutter Installs the specified Flutter version subosito/flutter-action@v2 Use latest stable version
Install Dependencies Installs project dependencies flutter pub get Ensures consistent builds
Run Tests Executes unit and widget tests flutter test Reports test coverage and failures
Build APK Generates a release APK flutter build apk --release For Android deployment
Note: Ensure environment variables like GOOGLE_SERVICES_JSON are securely stored in GitHub Secrets for sensitive builds.
Warning: Avoid using flutter build without specifying a target platform in CI environments to prevent unexpected builds.

CI/CD Pipeline Setup Steps

  • Define triggers (push, pull request)
  • Use a reliable Flutter action (e.g., subosito/flutter-action)
  • Install dependencies with flutter pub get
  • Run tests to validate code quality
  • Build artifacts for deployment (APK, IPA, etc.)

2. Using GitHub Actions

Example: Basic Flutter CI Workflow

name: Flutter CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.24.0"
      - name: Install dependencies
        run: flutter pub get
      - name: Run tests
        run: flutter test
Step Description Notes
actions/checkout uses: actions/checkout@v4 Checks out the repository code
subosito/flutter-action uses: subosito/flutter-action@v2 Installs Flutter SDK
flutter pub get run: flutter pub get Installs project dependencies
flutter test run: flutter test Runs unit and widget tests
Note: Ensure flutter pub get is run before any test or build commands to fetch dependencies.

Example: Build and Deploy to GitHub Pages

name: Build and Deploy

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.24.0"
      - name: Build Web
        run: flutter build web --release
      - name: Deploy to GitHub Pages
        uses: peaceiris/actions-gh-pages@v3
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          publish_dir: ./build/web

GitHub Actions CI Workflow Summary

  • Use subosito/flutter-action to install Flutter
  • Run flutter pub get to fetch dependencies
  • Execute tests with flutter test
  • Build apps using flutter build commands
  • Deploy artifacts with GitHub Pages or other services

3. Using Fastlane

Example: Fastlane Setup for Flutter

# Fastfile
lane :release do
  flutter_build(
    target: "lib/main.dart",
    build_number: get_build_number,
    version: get_version
  )
  upload_to_app_store(
    username: "appstore@example.com",
    app_identifier: "com.example.app"
  )
end
Option Description Default
username App Store Connect username None
app_identifier Bundle identifier of the app None
build_number Build number to upload Current build number
Note: Fastlane requires Xcode command line tools to be installed.
Warning: Ensure that your Apple Developer account credentials are securely managed.

Fastlane Integration Benefits

  • Automates app builds and deployments
  • Manages code signing certificates
  • Supports multiple app stores (App Store, Google Play)
  • Integrates with CI/CD pipelines

4. Automating Tests

Example: Running Flutter tests in CI

flutter test
Command Description Flags
flutter test Runs all tests in the project --coverage, --machine
flutter test --coverage Generates coverage reports --output
flutter test --machine Outputs test results in JSON format --timeout
Note: Ensure your CI environment has the required dependencies installed to run Flutter tests.
Warning: Tests must be configured to run in a headless environment.

Test Automation Best Practices

  • Run tests on every commit to detect regressions early
  • Use flutter test --coverage to ensure code coverage is maintained
  • Integrate with tools like Codecov or Coveralls for coverage reporting

5. Automating Builds

Example: CI Configuration for Flutter

# .github/workflows/flutter.yml
name: Flutter CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.24.0"
      - run: flutter pub get
      - run: flutter test
      - run: flutter build apk --release
Step Description Command
Checkout Code Fetches the repository code actions/checkout@v4
Setup Flutter Installs Flutter SDK subosito/flutter-action@v2
Get Dependencies Installs project dependencies flutter pub get
Run Tests Executes unit and widget tests flutter test
Build APK Creates a release Android APK flutter build apk --release
Note: Ensure that your CI environment has sufficient resources for building Flutter apps.

Build Automation Summary

  • Automated builds reduce manual errors
  • Consistent builds across environments
  • Supports continuous delivery pipelines

6. Using Codemagic

Example: Codemagic Configuration

workflows:
  flutter-workflow:
    name: Flutter CI/CD
    steps:
      - checkout
      - cache:
          paths:
            - .pub-cache
            - build/
      - flutter:
          command: pub get
      - flutter:
          command: test
      - flutter:
          command: build
            arguments: --release
Feature Description Status
Flutter Build Builds Flutter apps for iOS and Android NEW
Test Automation Runs unit and widget tests automatically DEPRECATED
Cache Management Caches dependencies to speed up builds BETA
Note: Codemagic supports both iOS and Android builds with a single configuration.
Warning: Ensure all secrets are properly encrypted in Codemagic.

Key Features

  • Automated builds for multiple platforms
  • Integration with GitHub and GitLab
  • Supports custom scripts and workflows

7. Implementing Automated Deployment

Example: GitHub Actions Workflow for Flutter Deployment

name: Flutter Deployment
on:
  push:
    branches: [ main ]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Setup Flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: "3.19.0"
      - name: Install Dependencies
        run: flutter pub get
      - name: Build APK
        run: flutter build apk --release
      - name: Upload APK
        uses: actions/upload-artifact@v3
        with:
          name: app-release.apk
          path: build/app/outputs/flutter-apk/app-release.apk
Step Description Command Notes
Checkout Code Retrieve source code from repository actions/checkout@v3 Required for all CI workflows
Setup Flutter Install Flutter SDK subosito/flutter-action@v2 Use latest stable version
Install Dependencies Fetch project dependencies flutter pub get Ensures all packages are up-to-date
Build APK Generate release build flutter build apk --release For Android deployment
Upload Artifact Store build output actions/upload-artifact@v3 Can be used for app store upload
Note: Ensure proper permissions and secrets are configured for app store uploads.

Deployment Options Comparison

  • GitHub Actions: Free for public repos, easy setup, good for open-source
  • GitLab CI: Built-in with GitLab, supports private repos
  • CircleCI: Fast builds, good for large projects
  • Fastlane: Specialized for iOS/macOS deployment, powerful

8. Running Code Analysis

Example: Analyzing Flutter Project

# pubspec.yaml
dev_dependencies:
  flutter_lints: ^2.0.0

# analysis_options.yaml
include: package:flutter_lints/flutter.yaml

linter:
  rules:
    - camel_case_types
    - prefer_const_constructors
Rule Description Enabled by Default Severity
camel_case_types Enforces camelCase for type names Yes Warning
prefer_const_constructors Prefer const constructors for widgets that don't change Yes Hint
use_key_in_widget_constructors Requires keys in widget constructors No Warning
Note: The flutter_lints package provides a set of recommended lints for Flutter projects.

Workflow: Setting up Code Analysis in CI

  1. Add flutter_lints to dev_dependencies
  2. Create or update analysis_options.yaml to include lints
  3. Configure CI pipeline to run flutter analyze
  4. Fail build if analysis fails

9. Generating Code Coverage Reports

Example: Setting up code coverage in Flutter

dev_dependencies:
  coverage: ^1.6.0
  flutter_test:
    sdk: flutter
Option Values Description
coverage 1.6.0 Code coverage package for Flutter
Note: Code coverage reports help identify untested parts of your codebase.

Example: Running coverage with Flutter test

flutter test --coverage

Example: Generating lcov report

genhtml coverage/lcov.info -o coverage/html

Workflow: Setting up code coverage in CI

  1. Install coverage package
  2. Run tests with --coverage flag
  3. Generate HTML report using genhtml

10. Using Bitrise for Mobile CI/CD

Feature Description Notes
Bitrise Workflow bitrise.yml Configuration file for defining CI/CD workflows
Trigger Git push, pull request, manual trigger Automatically starts builds based on events
Platforms Supported iOS, Android, Flutter Native support for mobile and web apps

Example: Bitrise Workflow Configuration

workflows:
  main:
    steps:
    - git-clone: {}
    - flutter-installer:
        inputs:
        - flutter_version: "3.24.0"
    - flutter-build:
        inputs:
        - project_path: "."
        - build_mode: "release"
Note: Bitrise supports Flutter builds with automatic dependency resolution and device testing.

Bitrise vs Other CI Services

  • Bitrise: Dedicated mobile CI with Flutter support
  • GitHub Actions: General-purpose CI with custom workflows
  • CircleCI: Fast builds, less mobile-specific features

Was this article helpful?