> ## Documentation Index
> Fetch the complete documentation index at: https://e2e.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Codemagic

> Run e2e end-to-end tests on Codemagic, with iOS on a Mac and Android on a Linux instance, and release only the builds that pass.

export const setupPrompt = `Add Codemagic workflows that run this project's e2e tests on iOS and Android. Docs: \`https://e2e.com/docs/ci/codemagic.md\` and \`https://e2e.com/docs/ci.md\`. A complete working example is \`https://github.com/codemagic-ci-cd/codemagic-e2e-flutter-sample\`.

1. Read both pages and the example's \`codemagic.yaml\`.
2. Add the workflows to \`codemagic.yaml\`. Adapt the build step, project paths, scheme, and Node version to this project.
3. Ask me to create an \`e2e\` variable group and add the model key to it, marked as secure.
4. After I start a build, read the result of each step and fix errors until both workflows pass.`;

This setup runs iOS on a Mac instance and Android on a Linux instance, one
workflow each, following the rules in [Mobile jobs](https://e2e.com/docs/ci#mobile-jobs). The
same tests can then gate a release workflow that signs and publishes your
app.

<SetupPrompt>

<Prompt description={setupPrompt} children={setupPrompt} actions={["copy", "cursor"]} />

</SetupPrompt>

## What you can do

Codemagic builds the app on the machine that runs the simulator or emulator,
so one `codemagic.yaml` covers the whole path from commit to store:

- Run the suite on every push and pull request. Each platform gets the
  machine it needs, with Xcode, simulators, and Android emulator images
  already installed.
- Read the results in the build's Tests tab, and keep e2e's JSON report, log,
  and trace pages as artifacts.
- Replay agent steps from a committed recording. A build with a matching
  recording needs no model key and makes no model call.
- Run the same e2e step in a tag-triggered release workflow, ahead of the
  build you ship. A failing test stops the build before anything is signed or
  uploaded.
- Sign with a certificate or keystore you upload once, and publish to App
  Store Connect, TestFlight, or Google Play from the same file.
- Let a coding agent start builds and read their step logs through the
  Codemagic REST API, then fix what fails.

## Workflow

A complete example for a Flutter app is in the
[codemagic-e2e-flutter-sample](https://github.com/codemagic-ci-cd/codemagic-e2e-flutter-sample)
repository. It has the tests, the workflows below, and a committed replay
cache, and runs on a fork with no accounts or keys. The steps that boot the
device and run e2e are the same for any framework. Replace the dependency,
build, and app-path lines with your app's own.

Leave `instance_type` out of the iOS workflow to use your team's default Mac
machine. Android needs `instance_type: linux_x2`, because the emulator
needs hardware virtualization. Linux instances need billing enabled on the
Codemagic team, as described in
[Codemagic's YAML getting started](https://docs.codemagic.io/yaml-basic-configuration/yaml-getting-started/).
`test_report` sends `junit.xml` to the Tests tab of the build.

```yaml title="codemagic.yaml" expandable
definitions:
  e2e_artifacts: &e2e_artifacts
    - e2e/.e2e/report.json
    - e2e/.e2e/junit.xml
    - e2e/.e2e/run.log
    - e2e/.e2e/results/**

  # The tests run in the same steps in the check and the release workflows, so
  # a release is only built from a revision that passed exactly these tests.
  steps:
    install_dependencies: &install_dependencies
      name: Install dependencies
      script: |
        flutter pub get
        (cd e2e && npm ci)

    build_ios_simulator: &build_ios_simulator
      name: Build for the simulator
      # Flutter cannot build a Release app for the simulator, so this is Debug.
      script: flutter build ios --simulator --debug

    start_ios_simulator: &start_ios_simulator
      name: Start a simulator
      # Starts booting in the background while the app builds.
      script: |
        set -euo pipefail
        xcrun simctl list runtimes
        UDID=$(xcrun simctl list devices available -j | python3 -c "
        import json,sys
        d=json.load(sys.stdin)['devices']
        for rt in sorted(d, reverse=True):
            for dev in d[rt]:
                if dev['name'].startswith('iPhone') and 'Pro' in dev['name'] and 'Max' not in dev['name']:
                    print(dev['udid']); sys.exit()
        ")
        test -n "$UDID"
        echo "Using simulator $UDID"
        xcrun simctl boot "$UDID" || true
        echo "E2E_IOS_DEVICE=$UDID" >> "$CM_ENV"

    install_ios_app: &install_ios_app
      name: Install the app
      script: |
        set -euo pipefail
        xcrun simctl bootstatus "$E2E_IOS_DEVICE" -b
        xcrun simctl install "$E2E_IOS_DEVICE" build/ios/iphonesimulator/Runner.app

    run_e2e_ios: &run_e2e_ios
      name: Run e2e
      script: |
        set -o pipefail
        cd e2e
        mkdir -p .e2e
        # Sample only: list your own test files here, or drop $FILES below to run the whole suite.
        FILES="tests/sign-in.e2e.ts tests/order.e2e.ts"
        # Sample only: the agent test runs with AI_GATEWAY_API_KEY or a committed recording for this
        # platform. For another model provider, check its key variable here instead.
        if [ -n "${AI_GATEWAY_API_KEY:-}" ] || grep -qs '"targetId": "ios"' .e2e/cache/*.json; then
          FILES="$FILES tests/agent.e2e.ts"
        fi
        npx e2e run --target ios --reporter list,junit $FILES 2>&1 | tee .e2e/run.log
      test_report: e2e/.e2e/junit.xml

    start_android_emulator: &start_android_emulator
      name: Start the emulator
      # Boots in the background while the APK builds.
      script: |
        # The machine already runs an adb server that does not know this emulator.
        # Create an adb key and restart the server, or the device shows as "unauthorized".
        adb kill-server || true
        mkdir -p "$HOME/.android"
        [ -f "$HOME/.android/adbkey" ] || adb keygen "$HOME/.android/adbkey"
        adb start-server
        nohup emulator -avd emulator-35 -no-window -no-audio -no-boot-anim -no-snapshot -gpu swiftshader_indirect > "$HOME/emulator.log" 2>&1 &

    build_android_apk: &build_android_apk
      name: Build the APK
      script: flutter build apk --release --target-platform android-x64

    install_android_app: &install_android_app
      name: Install the app
      script: |
        for i in $(seq 1 150); do
          [ "$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" = "1" ] && break
          sleep 2
        done
        if [ "$(adb shell getprop sys.boot_completed 2>/dev/null | tr -d '\r')" != "1" ]; then
          echo "Emulator did not boot. adb devices:"; adb devices
          echo "emulator.log:"; tail -n 60 "$HOME/emulator.log"
          exit 1
        fi
        sleep 10 # let the UI settle before the first test
        adb install -r build/app/outputs/flutter-apk/app-release.apk

    run_e2e_android: &run_e2e_android
      name: Run e2e
      script: |
        set -o pipefail
        cd e2e
        mkdir -p .e2e
        # Sample only: list your own test files here, or drop $FILES below to run the whole suite.
        FILES="tests/sign-in.e2e.ts tests/order.e2e.ts"
        # Sample only: the agent test runs with AI_GATEWAY_API_KEY or a committed recording for this
        # platform. For another model provider, check its key variable here instead.
        if [ -n "${AI_GATEWAY_API_KEY:-}" ] || grep -qs '"targetId": "android"' .e2e/cache/*.json; then
          FILES="$FILES tests/agent.e2e.ts"
        fi
        npx e2e run --target android --reporter list,junit $FILES 2>&1 | tee .e2e/run.log
      test_report: e2e/.e2e/junit.xml

workflows:
  e2e-ios:
    name: e2e iOS
    max_build_duration: 60
    environment:
      groups:
        - e2e
      flutter: 3.44.6
      xcode: latest
      node: 24
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/.agent-device/apple-runner/derived
    triggering:
      events:
        - push
        - pull_request
    scripts:
      - *start_ios_simulator
      - *install_dependencies
      - *build_ios_simulator
      - *install_ios_app
      - *run_e2e_ios
    artifacts: *e2e_artifacts

  e2e-android:
    name: e2e Android
    # The emulator needs a Linux machine (hardware virtualization).
    instance_type: linux_x2
    max_build_duration: 60
    environment:
      groups:
        - e2e
      flutter: 3.44.6
      node: 24
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/.gradle/caches
    triggering:
      events:
        - push
        - pull_request
    scripts:
      - *start_android_emulator
      - *install_dependencies
      - *build_android_apk
      - *install_android_app
      - *run_e2e_android
    artifacts: *e2e_artifacts
```

The Ubuntu image has AVDs named `emulator` (API 34), `emulator-35`, and
`emulator-36`, but none is running at the start of a build. Start one while
the app builds, so it boots in the background. The build targets
`android-x64` because the emulator is x86_64. Flutter can't build a Release
app for the iOS simulator, so the iOS build is Debug. A React Native or Expo
project builds Release on both platforms, as in
[Mobile jobs](https://e2e.com/docs/ci#mobile-jobs).

## Model key

Create an `e2e` variable group on the Codemagic app, add the model key to it
marked as **secure**, and list the group in the workflow as above.

<Warning>
Any build that reads the group can read the key, and a pull request build
runs the code on the pull request's branch. The sample's group is empty, so
its workflows are safe to run on pull requests. Once you add a key, list the
group only in workflows that run on branches and tags you trust, and run
pull requests without it. See [Model credentials](https://e2e.com/docs/ci#model-credentials).
</Warning>

Agent steps don't need the key on every run. Once a later check confirms the
outcome, e2e records what an `agent.act` step did, and later runs replay it
from the cache, by default `.e2e/cache`, without a model call. Commit the
recording and Codemagic replays it on every build. CI only reads the cache.
If the app changes and a recording goes stale, the live agent takes over and
calls the model. Add `--strict-cache` to fail instead. See
[Cache](https://e2e.com/docs/cache#fail-on-a-stale-recording).

## Release only what passed

Run the same e2e steps in a release workflow, before the build you publish.
A failing test stops the build, so nothing is signed or uploaded. Codemagic
signs with a certificate and profile you upload once under **Team settings** >
**Code signing identities**, and publishes with an App Store Connect API key
you add under **Developer Portal**. The workflow refers to both by name.

This is the sample's `release-ios` workflow with its signing and publishing
blocks switched on. It reuses the steps defined above, so the tests that
gate a release are the ones that run on every push.
Only let people you trust push `v*` tags, because a tag starts a build that
holds your signing and publishing credentials.

```yaml title="codemagic.yaml" expandable
workflows:
  release-ios:
    name: Release iOS
    max_build_duration: 90
    environment:
      groups:
        - e2e
      flutter: 3.44.6
      xcode: latest
      node: 24
      # A certificate and profile you uploaded once under Team settings >
      # Code signing identities. Codemagic fetches them by distribution type
      # and bundle id, so no keychain scripts are needed.
      ios_signing:
        distribution_type: app_store
        bundle_identifier: io.codemagic.samples.beanline # use your own
    # The App Store Connect API key you added under Team settings > Developer Portal.
    integrations:
      app_store_connect: YOUR_APP_STORE_CONNECT_KEY_NAME
    cache:
      cache_paths:
        - $HOME/.pub-cache
        - $HOME/.agent-device/apple-runner/derived
    triggering:
      events:
        - tag
      tag_patterns:
        - pattern: 'v*'
          include: true
    scripts:
      # The gate: if any e2e test fails, the build stops here and nothing below runs.
      - *start_ios_simulator
      - *install_dependencies
      - *build_ios_simulator
      - *install_ios_app
      - *run_e2e_ios
      - name: Apply the provisioning profile
        script: xcode-project use-profiles
      - name: Build the signed IPA
        script: |
          flutter build ipa --release \
            --build-number="$BUILD_NUMBER" \
            --export-options-plist=/Users/builder/export_options.plist
    artifacts:
      - build/ios/ipa/*.ipa
      - e2e/.e2e/report.json
      - e2e/.e2e/junit.xml
      - e2e/.e2e/run.log
      - e2e/.e2e/results/**
    # Needs the signed IPA above, and an app record created by hand in App Store Connect.
    publishing:
      app_store_connect:
        auth: integration
        submit_to_testflight: false
```

Android works the same way: upload the keystore, reference it with
`android_signing`, build the bundle, and publish with `google_play`. The
sample repository ships these settings as commented blocks, so it runs as it
is. Switch signing and publishing on once your app exists in App Store
Connect or Google Play.

## Use a coding agent

The sample repository has two skills for coding agents in its `skills`
folder. One sets up e2e and the workflows. The other starts a Codemagic build
through the REST API, waits for it, and reads the failing step's log. Copy
them into your project's `.claude/skills` folder.

## Next

<CardGroup cols={2}>
  <Card title="Codemagic example" icon="github" href="https://github.com/codemagic-ci-cd/codemagic-e2e-flutter-sample">
    A Flutter app with these workflows, release gates, and a replayed agent test, maintained by Codemagic.
  </Card>
  <Card title="Continuous integration" icon="rotate" href="/ci">
    CI defaults, sharding, reruns, and exit codes.
  </Card>
  <Card title="Hosted devices" icon="cloud" href="/mobile#hosted-devices">
    Lease devices from a service. Then the runner boots no device.
  </Card>
</CardGroup>
