Codemagic setup with flavors. Flutter CI/CD

Valentyna Polienova
5 min readJul 11, 2024

--

Hello! This is a step-by-step guide how to config Codemagic for your flutter app if you use flavors. This can be very useful if you want automize building and distribution of your app and save a lot of time.
Especially it can be useful if you don’t have a Mac, but you want to upload new builds to Apple Connect.

In this guide we will consider a case if you have a dev and prod flavors in your app with different bundle ids and you have different apps in Apple Connect. What we will do:

  1. Create an app in a Codemagic.
  2. Create a dev workflow
  3. Create a prod workflow

So, let’s start!

1. Create a new application

Log-in your Codemagic account (you can use a free one). Go to the apps tab and click Add application. Here you can choose a Git provider and select a desired repository:

Choose a repository (they are fetched automatically)

2. Create a dev workflow

After creating an app you will be navigated to the workflow screen.
Firstly we will rename workflow to the Dev Workflow.

Your app’s settings. Rename a workflow.

After renaming, let’s set everything up. You can add a build trigger, if you want, so after pushing to a concrete branch, your app will be automatically built. Check Trigger on push, specify a branch name (develop in my case) and click ‘Add pattern’. But you always can run your build manually.

Adding a build trigger

Then, you can turn on tests if you want and have them. Or you can uncheck all this checkboxes. As I don’t have tests, I prefer to uncheck ‘Stop build if tests or analysis fail’.

Turn on or off tests

Next, go to Build, select release and add your build arguments. For example, I am adding:

--dart-define=envFlavor=dev --flavor dev

Everything else you can leave as it is.

Add your build arguments

Then, go to Distribution and configure signing and distribution to stores. I will provide an example how to do it for Apple Connect.

Under iOS code signing select Automatic.

Config iOS Code Signing

Here you have to choose an API Key. To do it, account owner of your Apple Connect account has to turn on the Integrations API (go to this link). Ask him to do it in advance! After it is turned on, you can create a new key. Provide an Application Manager role, save the key (don’t lose it!) and copy the Key ID.

Create a Key

Now, you can go to the Teams tab, under Team Integrations connect an iOS Developer portal.

Connect a Developer portal

Now you can select an API Key, under Provisioning profile type select App Store and choose the bundle identifier of your dev app.

Now under App Store Connect select any configuration you want. Enable publishing checkbox, you also can publish even if tests fail or automatically add to the external testing to the desired test groups.

Config App Store Connect

Great! Now you can save changes. The last configuration is required. You have to add apple certificates and provisioning profiles to the Codemagic Team settings.

Go to Teams tab, scroll down to the Code signing identities. Here you can use this guide. You can try to fetch certificates from your Apple Connect. If they are not found, tap Create certificate and add 2 certificates — Development and Distribution. They will be created via API and added to the Apple account.

Add apple certificates

Next, go to iOS provisioning profiles. Try to fetch them. If they are no found, navigate here and create 2 profiles (development and distribution) for your app. Upload it manually.

Add provisioning profiles

Thats all! You can now click Start new build, select a desired branch and Dev workflow and build a new app, which will be uploaded directly to TestFlight. In artifacts you also can download an .apk file for android.

3. Create a prod workflow

This one will be easy :) Firstly, in settings tap Duplicate workflow. Rename it to Prod Workflow.

Copy workflow

Next, change build trigger to other branch (master for example), change build args to prod.

Change build args

Next, under iOS code signing, you can change bundle id (or even change an account and then change a bundle id). Select the right API Key under App Store Connect too.

If you use another Apple Connect Acount for a prod app, you have to add an API Key, certificates and profiles again (!!!!!!).

That’s all!! Now you can run a build using the Prod Workflow.

Successful build

If you have private dependencies with SSH Access

You can use this guide to avoid an error in Installing dependencies phase.

--

--