Automating the Build and Deployment Process in Flutter

Automating the build and deployment process in Flutter is crucial for continuous integration and continuous deployment (CI/CD). Automation not only saves time but also reduces the chances of human error, ensuring consistent and reliable deployments. In this blog post, we’ll explore how to automate your Flutter app’s build and deployment process using various tools and platforms.

Why Automate the Build and Deployment Process?

  • Consistency: Automated builds are consistent, reducing the likelihood of errors due to manual processes.
  • Efficiency: Saves time by automating repetitive tasks.
  • Faster Feedback: Enables quicker testing and feedback cycles.
  • Reliability: Ensures that deployment is standardized and reliable.

Tools and Platforms for Automation

Several tools and platforms can help automate the Flutter build and deployment process. Here are some popular options:

  • Codemagic: A CI/CD platform specifically designed for Flutter.
  • GitHub Actions: GitHub’s built-in CI/CD solution.
  • Bitrise: A mobile-focused CI/CD platform.
  • Fastlane: An open-source platform for automating mobile app deployments.

Automating with Codemagic

Codemagic is a popular choice for Flutter CI/CD due to its ease of use and Flutter-specific features.

Step 1: Setting Up Your Flutter Project in Codemagic

  1. Sign up for a Codemagic account at Codemagic.
  2. Connect your repository (e.g., GitHub, GitLab, Bitbucket).
  3. Select your Flutter project.

Step 2: Configuring Your Build

Configure your build settings in Codemagic:


workflows:
  flutter-android:
    name: Flutter Android Build
    instance_type: mac_mini_m1
    max_build_duration: 120
    environment:
      flutter: stable
      android_signing:
        keystore: example.jks
        keystore_properties:
          - alias=keyAlias
          - keyPassword=keyPassword
          - storePassword=storePassword
    scripts:
      - name: Flutter Build
        script: |
          flutter clean
          flutter pub get
          flutter build appbundle
    artifacts:
      - build/app/outputs/bundle/release/*.aab
    publish:
      email:
        recipients:
          - email@example.com

Key configurations in the YAML file:

  • flutter-android: Workflow for Android builds.
  • instance_type: Choose the build machine (e.g., mac_mini_m1).
  • max_build_duration: Maximum build time in minutes.
  • environment: Sets Flutter version and signing details.
  • android_signing: Configuration for signing the Android app bundle.
  • scripts: Commands to run for building the app.
  • artifacts: Defines the build outputs to be saved.
  • publish: Specifies how to publish the build artifacts (e.g., via email).

Step 3: Configuring Code Signing

Upload your Keystore file to Codemagic and configure the necessary environment variables:

  • alias: The alias of the key.
  • keyPassword: The password for the key.
  • storePassword: The password for the Keystore file.

Step 4: Triggering the Build

You can trigger the build manually or set up automatic triggers (e.g., on every commit to the main branch).

Automating with GitHub Actions

GitHub Actions provides a flexible way to automate workflows directly from your GitHub repository.

Step 1: Creating a Workflow File

Create a YAML file (e.g., .github/workflows/flutter_build.yml) in your repository:


name: Flutter CI/CD

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

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@v2
        with:
          flutter-version: '3.0.0'
      - run: flutter pub get
      - run: flutter analyze
      - run: flutter test
      - run: flutter build apk --release
        env:
          KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }}
          KEY_ALIAS: ${{ secrets.KEY_ALIAS }}
          KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }}
          STORE_PASSWORD: ${{ secrets.STORE_PASSWORD }}
      - name: Upload APK
        uses: actions/upload-artifact@v2
        with:
          name: release-apk
          path: build/app/outputs/apk/release/app-release.apk

Key aspects of the workflow file:

  • name: Workflow name.
  • on: Triggers for the workflow (e.g., push, pull_request).
  • jobs: Defined jobs to be executed.
  • runs-on: Operating system for the build environment.
  • steps: Individual steps to perform.

Step 2: Configuring Secrets

Store sensitive information like Keystore data, API keys, and passwords as encrypted secrets in your GitHub repository settings.

  • KEYSTORE_BASE64: Base64 encoded Keystore file.
  • KEY_ALIAS: Key alias.
  • KEY_PASSWORD: Key password.
  • STORE_PASSWORD: Store password.

Step 3: Encoding the Keystore File

Encode your Keystore file to Base64 and store it as a secret in GitHub:


base64 example.jks | tr -d 'n'

Step 4: Updating build.gradle

Add the necessary code signing configurations to your android/app/build.gradle file:


android {
    signingConfigs {
        release {
            storeFile file(decodeBase64(System.getenv('KEYSTORE_BASE64')))
            storePassword System.getenv('STORE_PASSWORD')
            keyAlias System.getenv('KEY_ALIAS')
            keyPassword System.getenv('KEY_PASSWORD')
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

def decodeBase64(String encodedString) {
    return encodedString.decode('base64').toString()
}

Step 5: Triggering the Workflow

Push changes to the main branch, and the GitHub Actions workflow will automatically trigger the build process.

Automating with Bitrise

Bitrise is another excellent platform for automating mobile app development workflows. It offers a visual workflow editor, making it easy to configure build steps.

Step 1: Setting Up Your Flutter Project in Bitrise

  1. Sign up for a Bitrise account at Bitrise.
  2. Connect your repository.
  3. Select your Flutter project.

Step 2: Configuring Your Workflow

Use the Bitrise Workflow Editor to define the build steps:

  • Activate SSH key: Add your SSH key for accessing private repositories.
  • Git Clone Repository: Clone the repository.
  • Flutter Install: Install the Flutter SDK.
  • Flutter Analyzer: Analyze Flutter code.
  • Flutter Test: Run Flutter tests.
  • Flutter Build: Build the APK or App Bundle.
  • Code Signing: Sign the APK/App Bundle with your Keystore.
  • Deploy to Bitrise.io: Deploy the artifacts.

Step 3: Code Signing Configuration

Upload your Keystore file and set up the necessary code signing configurations.

Step 4: Triggering the Build

Configure Bitrise to trigger builds on every push to your repository or manually trigger builds via the Bitrise dashboard.

Automating with Fastlane

Fastlane is an open-source platform for automating mobile app deployments. It is highly customizable and supports a wide range of actions.

Step 1: Installing Fastlane

Install Fastlane on your machine:


brew install fastlane

Step 2: Setting Up Fastlane in Your Flutter Project

Navigate to your Flutter project directory and run:


fastlane init

Step 3: Configuring the Fastfile

Edit the Fastfile located in the fastlane directory of your Flutter project:


platform :android do
  desc "Build and deploy the Android app"
  lane :deploy do
    gradle(task: "clean assembleRelease")
    upload_to_play_store(
      track: 'production',
      skip_upload_apk: true,
      skip_upload_aab: false
    )
  end
end

Step 4: Setting Up Supply

Supply is a Fastlane tool for uploading metadata, screenshots, and binaries to the Google Play Store:


fastlane supply init

Step 5: Running the Fastlane Lane

Execute the deploy lane to build and deploy your Android app:


fastlane deploy

Conclusion

Automating the build and deployment process is an essential part of modern Flutter development. By leveraging tools like Codemagic, GitHub Actions, Bitrise, and Fastlane, you can streamline your workflows, reduce errors, and accelerate the delivery of high-quality Flutter applications. Whether you choose a dedicated CI/CD platform or integrate with existing tools, automating your builds and deployments will undoubtedly improve your productivity and the reliability of your releases. Take the time to set up an automated workflow and enjoy the benefits of a streamlined, efficient, and consistent development process.