Knowledge Hub

How is Flutter app distribution managed with Codemagic CI/CD and Melos?

Professional Flutter app distribution can be efficiently streamlined using a combination of Codemagic CI/CD pipelines and Melos for robust mono-repository management. This synergistic setup ensures consistent builds, automated testing, and seamless delivery across various platforms, significantly accelerating the release cycle and reducing human error.

Why Combine Codemagic and Melos?

As Flutter projects scale, teams often maintain multiple applications and shared packages inside a single mono-repository (monorepo) for maximum code reuse and organizational clarity. This setup, while powerful, introduces challenges in dependency management, build consistency, and automated distribution—challenges that Melos and Codemagic solve together.

  • Melos: Manages the mono-repo and orchestrates scripts, dependencies, and workflows across multiple packages and apps.
  • Codemagic CI/CD: Automates code testing, building, code signing, and app publishing to multiple stores/distribution channels.

Setting up Melos for a Flutter Monorepo

The first step is to structure your repository for success. Melos excels when you organize by features and responsibilities:


my_flutter_monorepo/
├─ apps/
│   ├─ user_app/
│   └─ admin_app/
├─ packages/
│   ├─ shared_ui/
│   └─ api_client/
├─ melos.yaml
├─ codemagic.yaml
└─ README.md

To begin, install Melos globally:

dart pub global activate melos

Then, at your project's root, create a melos.yaml:

name: super_monorepo
packages:
  - packages/**
  - apps/**
scripts:
  analyze:
    run: flutter analyze .
  test:
    run: flutter test

With your workspace defined, run bootstrap:

melos bootstrap

This command links local packages, ensuring your development and CI environments have consistent dependencies.

Picture: Diagram illustrating a Melos-managed monorepo with interconnected packages and apps, showing dependency flow.

Codemagic: Automating Build, Test, and Distribution

Codemagic's codemagic.yaml allows granular control over your CI/CD pipelines. Codemagic provides cloud-based macOS and Linux build machines, secure secret storage, and pre-built integrations for Google Play, App Store Connect, Firebase App Distribution, and more.

Example Codemagic Workflow

workflows:
  release-user-app:
    name: User App Release
    environment:
      flutter: stable
      groups:
        - secrets # Manage your secret keys here in the Codemagic UI
    scripts:
      - name: Install Melos
        script: dart pub global activate melos
      - name: Melos Bootstrap
        script: melos bootstrap
      - name: Run Analysis
        script: melos run analyze
      - name: Run Tests
        script: melos run test
      - name: Build APK
        script: melos exec --scope="user_app" -- flutter build apk --release
      - name: Build iOS
        script: melos exec --scope="user_app" -- flutter build ipa --release
    artifacts:
      - apps/user_app/build/**/outputs/**/*.apk
      - apps/user_app/build/**/outputs/**/*.ipa
    publishing:
      app_store_connect:
        api_key: $APP_STORE_CONNECT_PRIVATE_KEY
        api_issuer: $APP_STORE_CONNECT_ISSUER_ID
      google_play:
        credentials: $GCLOUD_SERVICE_ACCOUNT_CREDENTIALS
        track: beta

Highlights:

  • Install and bootstrap Melos—ensuring consistent dependencies.
  • Analysis and tests are reliably orchestrated across all packages and apps.
  • Specific apps are targeted via Melos’s --scope for building and publishing.
  • Artifacts are gathered and published automatically.

Picture: Screenshot of Codemagic workflow UI showing a successful build and release pipeline for multiple apps.

Workflow Steps Explained

  1. Dependency Management:
    Melos' bootstrap guarantees all apps and packages use the right versions and local dependencies. This avoids "works on my machine" syndrome.
  2. Automated Quality Gates:
    melos run analyze and melos run test ensure only validated and linted code is built and shipped.
  3. Selective App Building:
    With melos exec --scope="user_app" -- flutter build ..., Codemagic can target one or many apps—crucial for monorepos.
  4. Distribution Automation:
    Codemagic's publishing options push builds directly to app stores, saving manual upload steps.

Picture: Diagram showing the pipeline: developer push → Codemagic builds/tests → artifacts published to Google Play and App Store.

Integrating More Advanced Use Cases

  • Multiple Flutter Apps: Codemagic workflows can be customized to publish each app in your monorepo, by repeating scoped build steps or using multiple workflows.
  • Shared Packages: You can add steps to version and publish internal Dart/Flutter packages to a private or public pub.dev server directly from Codemagic.
  • Custom Scripts: Melos can orchestrate custom scripts for codegen, formatting, or publishing packages, making it the backbone for sophisticated pipelines.

Picture: Flowchart showing Melos and Codemagic interacting with internal package registry and app distribution endpoints.

Best Practices for Professional Flutter CI/CD

  • Environment Variables: Store secrets (API keys, signing credentials) in Codemagic's secure environment variables, never in code.
  • Immutable Artifacts: Use "clean" builds every time to ensure reproducibility.
  • Tag-based Deployments: Trigger releases only on Git tags or main branch merges for safe, auditable release workflows.
  • Incremental Builds: Use Melos's filtering features (--scope, --ignore, etc.) to only build/test affected packages.
  • Monitoring & Notifications: Configure Codemagic to send build status updates via email, Slack, or custom webhooks.

Picture: Sample dashboard showing Codemagic pipeline status and code coverage metrics for a monorepo.

Conclusion

By integrating Melos and Codemagic, Flutter teams can professionally manage mono-repositories, automate quality assurance, build, and distribute apps across all platforms—from a single source of truth. This not only streamlines engineering workflows, but also drastically reduces time-to-release for new features and bugfixes.

Whether you’re managing a solo app with internal packages, or scaling up to a full suite of Flutter apps and libraries, this robust setup prepares your team for fast, reliable, and consistent releases.

Picture: An abstract graphic representing professional automation: a conveyor belt assembling Flutter apps and sending them efficiently to app stores.

Further Reading & Resources