> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# CI/CD — Fastlane + GitLab CI

Mobile builds for Magic Piano are produced by **Fastlane** lanes invoked from GitLab CI ([`.gitlab-ci.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.gitlab-ci.yml): internal and release pipelines). **GitHub** is the source repo; GitLab mirrors it. Auto tag on merge to `main` runs only on GitHub ([`release-tag.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.github/workflows/release-tag.yml)); GitLab does not build on tags, so the mirrored `v*` tag starts no pipeline here. Layout: [`.gitlab-ci.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.gitlab-ci.yml) holds workflow rules, variables, shared script fragments and `validate`; the Biome lint runs inside the iOS jobs; [`iOS.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/iOS.yml) holds the iOS jobs and [`runtime.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/runtime.yml) the prebuilt-runtime job. **Android is not built on CI yet** (the Android Fastlane lanes remain in the Fastfile for local runs). There is no EAS — iOS code signing is fetched by Fastlane **`match`** (readonly, `appstore`) into a temporary keychain, run for `APP_BUNDLE_ID` only, with the rest of its configuration (`MATCH_*`) coming from the project's CI/CD variables, so no certificates or profiles are decoded in the pipeline. The App Store Connect API key is used for the build-number lookup and the TestFlight upload.

## `.env` is the single source of truth

App identity (display name, bundle id, scheme, Xcode project paths) lives in **one** file at the repo root: `.env`. Both Expo and Fastlane read from it. **Marketing version** lives in [package.json](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/package.json) — bump `"version"` there and both [app.config.ts](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app.config.ts) and [fastlane/Fastfile](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/fastlane/Fastfile) pick it up automatically.

- [app.config.ts](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app.config.ts) reads `version` from `package.json` and `APP_NAME`, `APP_SLUG`, `APP_SCHEME`, `APP_BUNDLE_ID`, `IOS_DEPLOYMENT_TARGET` via `process.env.*` — the Expo CLI auto-loads `.env` into `process.env` before evaluating the config (via `@expo/env`, SDK 49+).
- [fastlane/Appfile](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/fastlane/Appfile) and [fastlane/Fastfile](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/fastlane/Fastfile) `require "dotenv"; Dotenv.load(<repo>/.env)` at the top and read `APP_BUNDLE_ID`, `IOS_SCHEME`, `IOS_XCODE_PROJECT`, `IOS_XCODE_WORKSPACE`, etc. via `ENV.fetch(...)`.

### Setup

```bash
cp .env.example .env   # edit to match your app
```

The committed [.env.example](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.env.example) lists every variable with documentation.

### Why `.env` is safe to share between Expo and Fastlane

`Dotenv.load` and `@expo/env` both follow the **non-clobbering** rule: values already present in `process.env` / `ENV` are preserved. That means:

- **Locally**, `.env` populates everything.
- **In CI**, GitLab injects the CI/CD variables of the same names into the job environment before Fastlane / Expo run, so the CI value wins.

**Public client ids stay in the tracked `.env`.** The AdMob App ID and ad-unit ids
(`EXPO_PUBLIC_ADMOB_*`) are not secrets: prebuild and the JS bundle read them from the checked-out
`.env` on CI too. Never define one as a CI/CD variable or in `.gitlab-ci.yml` `variables:` — a variable defined in the job env,
even empty, shadows `.env`. The `ios_internal` job fails fast on either mistake (`.verify_admob_ids`).

### Native files vs `.env`

The `ios/` and `android/` folders are **gitignored** — they're regenerated from `app.config.ts` by **`expo prebuild`**. CI runs `expo prebuild --platform ios` after `bun install`; prebuild runs `pod install` itself (see [Pipeline topology](#pipeline-topology)). iOS **In-App Purchase** is re-applied on every prebuild by [`plugins/withIosInAppPurchase.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/plugins/withIosInAppPurchase.ts) (StoreKit.framework + Xcode `com.apple.InAppPurchase` capability) — do not add it by hand in Xcode. The same goes for the app icon test's alternate icons: [`plugins/withAlternateAppIcons.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/plugins/withAlternateAppIcons.ts) writes one `AppIcon-<name>.appiconset` per image in [`assets/app-icons/`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/assets/app-icons/) and sets `ASSETCATALOG_COMPILER_ALTERNATE_APPICON_NAMES` + `ASSETCATALOG_COMPILER_INCLUDE_ALL_APPICON_ASSETS = YES` on the app target (see [App icon test](#3-app-icon-test-product-page-optimization)).

Locally, when `APP_BUNDLE_ID` (or any other identity value in `.env`) changes:

```bash
# update .env, then:
bun run prebuild:clean
```

`prebuild:clean` deletes the existing `ios/` + `android/` and regenerates them so gradle/xcode and `.env` stay in sync. `app.config.ts` ships fallbacks for every variable, so a fresh clone with no `.env` still produces a buildable project (with the default bundle id `com.musicapp.magicpiano`).

## Pipeline topology

```mermaid
flowchart LR
  push["push to dev / [dev] commit<br/>or manual run (internal)"] --> validateA["validate<br/>install · lint · typecheck · security · all unit tests (JUnit)"]
  validateA --> iosA["ios_internal<br/>install · bun run lint · catalog sync · fastlane ios beta_testflight"]

  rel["branch release/ios*<br/>or manual run (release)"] --> validate["validate<br/>install · lint · typecheck · security · all unit tests (JUnit)"]
  validate --> iosB["ios_release<br/>install · bun run lint · fastlane ios beta_testflight"]

  main["push to main"] --> validateM["validate"]

  iosA --> testflightA["TestFlight"]
  iosB --> testflightB["TestFlight"]
```

| Pipeline      | Starts when                                                                                                                                           | Jobs                                                                                                                                                                                                                                                                                                                                               |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| internal      | push to `dev`, a push whose HEAD commit message starts with `[dev]`, or **Run pipeline** with `PIPELINE_TARGET=internal`                              | `validate` → `ios_internal` (runs `bun run lint` after its own install)                                                                                                                                                                                                                                                                            |
| release       | push to a `release/ios*` branch, or **Run pipeline** with `PIPELINE_TARGET=release`. A `v*` tag (including the one GitHub mirrors) starts no pipeline | `validate` → `ios_release` (runs `bun run lint` after its own install)                                                                                                                                                                                                                                                                             |
| main          | push to `main` on the GitLab mirror                                                                                                                   | `validate` only (lint + typecheck + security + tests; no iOS build). Tag + GitHub Release: merge to `main` on **GitHub** ([`release-tag.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.github/workflows/release-tag.yml))                                                                         |
| sim-release   | **Run pipeline** with `PIPELINE_TARGET=sim-release` (manual only, never on a push or merge)                                                           | `sim_release_publish` only (Release simulator `.app`, JS bundled → package registry `magic-piano-sim-release`; see [runtime-provider.md](/docs/runtime-provider.md#release-simulator-build-merge-to-dev))                                                                                                                               |
| e2e           | **Run pipeline** with `PIPELINE_TARGET=e2e` (manual only, never on a push to `dev` or a merge). `E2E_COMMIT` = sha to test, default newest build      | `e2e_maestro`: installs the registry's Release simulator build, boots a simulator, runs `.maestro/flows`; JUnit report, failure screenshots and the run video in the job's artifacts ([runtime-provider.md](/docs/runtime-provider.md#e2e-with-maestro))                                                                                |
| other branch  | push to any other branch that has no open merge request                                                                                               | `validate` only (no build)                                                                                                                                                                                                                                                                                                                         |
| merge request | opening a merge request, or pushing to its branch                                                                                                     | `validate` runs automatically. **`e2e_mr`** is a **manual** job on that pipeline: play it to build the MR's own Release simulator app and run the Maestro flows. It does not start on its own. Artifacts (video + report) are kept for a pass and a failure and linked from the MR. An unplayed or failed run does not block the merge — see below |
| runtime       | **Run pipeline** on the web with `PIPELINE_TARGET=runtime`. Never on push/tag                                                                         | `runtime_sim_publish` only, no quality gate (Debug simulator `.app` → GitLab generic registry; see [runtime-provider.md](/docs/runtime-provider.md))                                                                                                                                                                                    |

`workflow:rules` creates a pipeline for the triggers above and for merge requests. A feature branch's pushes create no pipeline of their own (nothing matches them), so with an open MR only the MR pipeline runs and nothing runs twice. A merge request pipeline never ships a build: `.rules_internal` and `.rules_ios_release` exclude it, even from a `release/ios*` source branch. `workflow:auto_cancel:on_new_commit: interruptible` cancels a superseded pipeline on the same ref.

### Merge requests: e2e is a manual job

`e2e_mr` is created on every merge request pipeline with `when: manual` and `allow_failure: true`. Opening or updating an MR does not start it. Play the job from the pipeline when you want the Maestro suite against that MR's Release simulator build. An unplayed job, or a failed run, does not fail the pipeline and does not block the merge. `validate` still runs on its own.

What a reviewer gets after the job has run, for a pass and for a failure alike (`artifacts: when: always`, kept 2 weeks):

- the **e2e video and report** link in the pipeline widget: `build/e2e/video/e2e-run.mp4` (the whole run, constant 60 fps), `report.xml`, and `artifacts/` (a screenshot + command log of the failing flow; on a failed build also `.ci-logs/gym/`);
- the JUnit results on the MR's **Tests** tab, one test per flow.

The job runs Maestro **2.5.1** (`MAESTRO_VERSION`, the version the flows were written with; bump it on purpose) and needs the same Tart runners as the TestFlight jobs; it takes \~15 min to build plus \~5 min of flows.

## Runners and the Tart image

Every job runs on a runner tagged `tart-studio-macos-builder` in a **fresh Tart macOS VM** cloned from the Amanotes golden image `ociregistry.amanotes.net/macos-tahoe-react-native-bun` (built by the `tart-vm-golden-image` repo). It bundles Xcode, Homebrew, Node through [nvm](https://github.com/nvm-sh/nvm), Bun through [BunVM](https://bunvm.com), Ruby through rbenv, Bundler, Fastlane (incl. `match`) and CocoaPods. The image holds tools only: the project selects versions from its own pins — `.nvmrc` (`.install_node`), `packageManager` in `package.json` (`.install_bun`) and `.ruby-version` (`.install_fastlane`) — and a pinned version the image lacks is installed on the fly by nvm / BunVM / rbenv.

| Variable          | Default                                                       | Purpose                                                                                                                                                                                                                                                                                        |
| ----------------- | ------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `TART_REGISTRY`   | `ociregistry.amanotes.net`                                    | OCI registry that hosts the Tart images.                                                                                                                                                                                                                                                       |
| `TART_IMAGE_TAG`  | `bun1.3.13-xcode27.1-r1`                                      | Golden image tag. Pin it (e.g. `bun1.3.13-xcode27.1-r1`) for reproducible builds.                                                                                                                                                                                                              |
| `TART_IMAGE`      | `$TART_REGISTRY/macos-tahoe-react-native-bun:$TART_IMAGE_TAG` | Full image reference used by every job.                                                                                                                                                                                                                                                        |
| `PIPELINE_TARGET` | `internal`                                                    | Web runs only, a dropdown: `internal` (TestFlight internal), `release` (TestFlight release) `runtime` (prebuilt native Debug simulator app, versioned by `package.json`) `sim-release` (Release simulator app with bundled JS) or `e2e` (Maestro flows against a published sim-release build). |

Because each VM starts clean, nothing is reused between jobs except **artifacts** and **host-mounted native caches**: `validate` packs the catalog snapshot (`.ci-catalog-stage/<CI_PIPELINE_ID>/`) and `ios_release` restores it, so the release ships the exact catalog that was validated. `ios_internal` is a single job that syncs the catalog itself. The Bun install cache (`.bun-install-cache/`) is cached by GitLab keyed on `bun.lock`.

**ccache (compiler cache).** The golden image `bun1.3.13-xcode27.1-r1` ships ccache. iOS build jobs run `.setup_tart_build_caches` before `expo prebuild`: `USE_CCACHE=1` so React Native's `react_native_post_install` wires the ccache clang wrappers into the Xcode projects, and `CCACHE_DIR` points at persistent storage when the Tart runner mounts `/Volumes/My Shared Files/cache` (`…/magic-piano/ccache`). **CocoaPods stays on the VM-local** `~/Library/Caches/CocoaPods` — a shared CocoaPods tree on the host mount races across concurrent jobs and has produced corrupt `Pods/Release/…` entries that fail `pod install`. Without the Tart mount, ccache still runs but only within that job (`$CI_PROJECT_DIR/.ci-ccache`). Confirm in the job log: `pod install` should print `[Ccache]: Setting CC, LD, CXX & LDPLUSPLUS build settings`, and the job ends with `ccache -s` stats via `.print_ccache_stats`.

## Reusable script fragments

Cross-job setup lives in hidden jobs (`.js_install`, `.write_env_local`, …) in [`.gitlab-ci.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.gitlab-ci.yml) and [`iOS.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/iOS.yml), pulled in with `!reference [.name, script]` so each job only lists what is unique to its lane. A `default:before_script` rewrites `https://gitlab.amanotes.net/` URLs to use `CI_JOB_TOKEN` so private repos (such as the `match` certificate repo) can be cloned.

\| `.install_node` | Selects the Node pinned in `.nvmrc` (falls back to `engines.node` in package.json) with `nvm use`, installing it with nvm if the image lacks it, and fails on a major mismatch. Metro, Babel and `expo prebuild` — and Xcode's "Bundle React Native code" phase — all run under it. |
\| `.js_install` | `.install_node`, `.install_bun`, then `bun install --frozen-lockfile --ignore-scripts`. Lifecycle scripts are skipped because `postinstall` syncs the catalog from the sheet; the catalog is materialized explicitly afterwards. There is no workspace-package build step — packages resolve to TypeScript source. |
\| `.catalog_sync` / `.catalog_sync_and_pack` / `.catalog_restore` | Sync the catalog in place (`ios_internal`), sync and pack the snapshot (atomic, `validate`), or restore the upstream snapshot and regenerate the asset manifests (`assets:sync`) plus the FTUE tutorial / learn charts. |
\| `.write_env_local` | Writes `.env.local` from the `EXPO_PUBLIC_*` variables, skipping empty values (an empty backend URL would break `createHub`). |
\| `.verify_admob_ids` | Fail fast when an AdMob id is missing from `.env` or defined in the job env. |
\| `.prebuild_ios` | `expo prebuild --clean --platform ios`. Prebuild runs `pod install` itself with the image's CocoaPods (the Gemfile carries only Fastlane), so there is no separate pods step. Expo only logs a warning when its `pod install` fails and exits 0, so the fragment then fails the job if `ios/Pods` / `ios/Podfile.lock` are missing. Skia's prebuilt xcframeworks come from the `react-native-skia-graphite-apple-*` npm packages that `bun install` fetches, and its podspec copies them into place at `pod install`, so no separate Skia step is needed (the old `install-skia` bin was removed in Skia 2.x; Skia 3 ships Graphite binaries). Skips Android's missing `google-services.json`. |
\| `.setup_tart_build_caches` | Sets `USE_CCACHE=1` and `CCACHE_DIR` (Tart host mount or job-local), keeps CocoaPods cache VM-local, prints `ccache -s` before the build. Runs on every job that prebuilds iOS. |
\| `.print_ccache_stats` | `after_script` on native build jobs: prints `ccache -s` after `gym`. |
\| `.install_bun` | Loads BunVM's default Bun onto `PATH` and, if it is not the `packageManager` version, runs `bunvm install` / `bunvm use` for it (`.js_install` calls it; `after_script` hooks load `~/.bunvm/etc/env.sh` themselves). |
\| `.install_fastlane` | Installs the `.ruby-version` Ruby via rbenv if the image does not already have it, then `bundle install` (Fastlane only; CocoaPods is not a gem dependency). |
\| `.verify_asc_api_key` | ASC key check, run before `expo prebuild` so a wrong key fails in \~10s instead of after `pod install`. Signing assets are fetched by the `beta_testflight` lane itself via `match`. |
\| `.notify_slack_on_failure` | `after_script` on iOS jobs: when the job failed, runs [`scripts/notify-slack-failure.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/scripts/notify-slack-failure.ts), which posts a ❌ message to `SLACK_WEBHOOK_URL` (job link, branch, commit, author). It covers failures before the Fastlane lane starts (install, prebuild, pods, ASC check). When a distribution lane fails, Fastlane's `error` hook already posted and wrote `.ci-slack-notified`, so the script skips and each failed job produces exactly one message. |
\| `.print_gym_log_on_failure` | `after_script` on iOS jobs: tails fastlane `gym`'s raw xcodebuild log (`~/Library/Logs/gym/`) because the VM and that file are gone once the job ends. It also copies the full logs to `.ci-logs/gym/`, which `.ios_artifacts` uploads as a job artifact (failed jobs only, kept 30 days; download from the job page). |
\| `.ios_artifacts` | Artifacts of `ios_internal` / `ios_release` (`when: always`, 30 days): `build/size-report/` (app size report, below) and `.ci-logs/gym/` (failed jobs). The runtime job keeps using `.gym_log_artifacts` (logs, failure only). |

## Lanes

| Lane                                         | What it does                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
| -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `fastlane ios beta_testflight`               | Fetches signing with `match` (readonly, `appstore`), then the same build, bumps build number via `next_asc_testflight_build_number` (latest TestFlight build on the current `package.json` marketing version + 1), uploads via `pilot`. `skip_submission: true` — TestFlight processes the build but no external-review submission.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `fastlane ios beta_testflight` (size report) | After the archive and before the upload, `generate_size_report!` re-exports the same `.xcarchive` with App Thinning (`<thin-for-all-variants>`) to get Xcode's `App Thinning Size Report.txt`, then runs [`scripts/app-size-report.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/scripts/app-size-report.ts) to write `app-size-report.md` / `.json` into `build/size-report/` (download + install size of the largest / smallest device and universal variant, plus the biggest paths inside the `.app`). Download it from the job's **Artifacts** (`ios_internal` / `ios_release`, kept 30 days). The success Slack message shows `IPA Size` plus `Download Size` / `Install Size` (largest-device thinned variant) read from that JSON (`artifact_size_fields` in the Fastfile; Android shows `AAB Size`). Failures only warn; they never block the TestFlight upload. Run it locally on any exported ipa: `bun scripts/app-size-report.ts --ipa build/ios/MagicPiano.ipa`. |
| `fastlane ios release_simulator`             | Local, and CI job `sim_release_publish` (manual `PIPELINE_TARGET=sim-release`, with `ARM64_ONLY=1`). Release `build_app` for the iOS Simulator, unsigned, JS bundled; `packages/runtime-provider` packs and publishes it per commit. The Crashlytics dSYM phase is skipped for simulator builds by the same plugin.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| `fastlane ios runtime_simulator`             | CI, web pipeline `PIPELINE_TARGET=runtime` only. Debug `build_app` for the iOS Simulator (unsigned, arm64, no match/ASC/upload, no JS bundled) into `build/ios-runtime/`; the `[RNFB] Crashlytics Configuration` dSYM upload (a network upload that can hang a CI VM) is skipped for Debug by `plugins/withCrashlyticsSkipDebugUpload.ts` on every prebuild; [`packages/runtime-provider`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/packages/runtime-provider) then packs and publishes it ([runtime-provider.md](/docs/runtime-provider.md)).                                                                                                                                                                                                                                                                                                                                                                                                                      |
| `fastlane android beta_playstore`            | `bundleRelease` (aab) → Google Play Internal track as `draft`; `versionCode` = highest code on the internal track + 1.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |

`beta_testflight` sets CFBundleVersion to latest TestFlight on the current marketing version + 1 (each `package.json` version train increments on its own). `beta_playstore` sets `versionCode` from the Play **internal** track max + 1. Slack `Version` shows that store build number only.

Marketing version is **not** touched by CI — bump `"version"` in [package.json](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/package.json) before a release. Run `bun run prebuild` (or `prebuild:clean` if native projects already exist) so regenerated `ios/` / `android/` pick up the new `versionName` / `CFBundleShortVersionString`.

Release notes live in [CHANGELOG.md](/changelog.md) — one `## [version]` section per release. When a version bump merges to **`main` on GitHub**, [`release-tag.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.github/workflows/release-tag.yml) ([`scripts/ci-release-tag.mjs`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/scripts/ci-release-tag.mjs)) creates the annotated tag and GitHub Release. The tag mirrors to GitLab but starts no pipeline there; the TestFlight release build comes from a `release/ios*` branch or a manual **Run pipeline** (`PIPELINE_TARGET=release`). Play Store Internal Slack notes use the same CHANGELOG section (`release_changelog` in [fastlane/Fastfile](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/fastlane/Fastfile)); `### Developer` and `### Breaking` bullets are omitted from tag messages and store formatting.

**TestFlight** uploads skip changelog on upload (faster `pilot`). Slack notifications still show **recent commit history** (`recent_changelog`) for each build from the `ios_release` and `ios_internal` jobs.

### Release checklist

1. While a version is on `dev`, keep its `## [x.y.z] - Unreleased` section in CHANGELOG.md up to date (`### Fixed` / `### Improved` for players, `### Developer`, `### Breaking` for the team). A bump without a section fails `scripts/__tests__/changelog-coverage.test.ts`.

2. Submit to the App Store the TestFlight build of that version — note its build number and the commit it was built from (the `ios_internal` / `ios_release` job logs `TestFlight <version> latest is N → build N+1`).

3. When Apple approves: date the section, then tag the shipped commit. Merged to `main` on GitHub, `release-tag.yml` does it; a release that shipped from `dev` or a hotfix branch is tagged by hand (any machine with `gh` logged in; the tag starts no GitLab pipeline):

   ```bash
   node scripts/ci-release-tag.mjs --version 1.0.12 --ref 35589c6fd
   ```

4. Bump `dev` to the next version right after the submission, so new `dev` builds never carry the version under review.

### Hotfix on top of the store version

A fix that must ship without what `dev` holds (1.0.12, 1.0.13): branch `hotfix/<next-version>-<topic>` from the **commit of the build on the store** (find it as in step 2), cherry-pick or write the fix, bump to the next version, add its CHANGELOG section, then **Run pipeline** on the branch with `PIPELINE_TARGET=internal` (the committed `.env` is store-safe). `dev` moves to the version after it. Once Apple approves, tag the hotfix commit and merge the hotfix branch back into `dev`.

## Triggering builds

| What                                                  | How                                                                                                                                                 |
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| Send an internal build to TestFlight                  | Push to `dev`, or push a commit whose message starts with `[dev]` on any branch.                                                                    |
| Ship an iOS release to TestFlight                     | Push a `release/ios*` branch (e.g. `release/ios-1.0.0`).                                                                                            |
| Build the prebuilt Debug simulator app for developers | **Build → Pipelines → Run pipeline**, set `PIPELINE_TARGET=runtime`. Needed only when native code changed; developers then run `bun runtime:start`. |
| Tag + GitHub Release                                  | Merge the version bump to **`main` on GitHub** (auto tag + GitHub Release). GitLab does not build on the mirrored `v*` tag.                         |

Every build pipeline runs `validate` (after `bun install`: Biome lint, typecheck, the `rnsec` security scan, then the whole unit-test suite); both iOS jobs `need` it, so the Xcode build only starts when it passes. There is no separate lint job: `validate` runs `bun run lint` first, and each iOS job runs it again right after its own `bun install` (`.lint_biome` in [`iOS.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/iOS.yml)), so the build job stays self-sufficient. `validate` publishes JUnit reports (`build/junit/*.xml`: one per test shard from `bun run test:ci`,) to GitLab's Tests tab, and keeps `rnsec-report.{html,json}` as artifacts. A failing `biome_check` or `validate` posts to Slack (same `SLACK_WEBHOOK_URL` and message as the iOS jobs, via `.notify_quality_failure`) on `dev`, `release/*`, tags and merge requests; other branch pushes only show a red pipeline. The same checks run locally through lefthook on pre-commit (lint, typecheck, unit tests, security), but only when a staged file can affect the app; lint checks only the staged TS/TSX files (see [`lefthook.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/lefthook.yml)).

## Required GitLab CI/CD variables

Set these under **Settings → CI/CD → Variables**. Mask secrets, and leave **Protected** off unless the refs that build (`dev`, `main`, `release/ios*`) are protected branches, otherwise the variables are not injected.

### Build flags (pipeline variables)

Declared as global `variables:` in [`.gitlab-ci.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.gitlab-ci.yml), so a pipeline gets the store setting no matter what the committed `.env` says (the process env wins over `.env`). Do not set them under Settings → CI/CD → Variables. To build with a different value (for example a QA build with the DevTool), open **Build → Pipelines → Run pipeline** on the web and change them there.

`EXPO_PUBLIC_ENABLE_DESKTOP_TOOLS` and `EXPO_PUBLIC_APP_VARIANT` are per ref (set by `workflow:rules`; the committed `.env` has the DevTool on and the variant `store`):

| Ref                    | DevTool / variant                                                                                                                                          |
| ---------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `main`, `release/ios*` | Strict `0` / `store`. A pipeline that sets the DevTool to `1`/`true` or the variant to anything but `store` (web form or project variable) is not created. |
| `dev`                  | `0` / `store` by default; the web "Run pipeline" form can change either.                                                                                   |
| any other branch       | No CI default: the committed `.env` decides; the web form can still override it.                                                                           |

| Variable                           | Default                            | Options                   | What                                                                                                                                                                                                                       |
| ---------------------------------- | ---------------------------------- | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `EXPO_PUBLIC_ENABLE_DESKTOP_TOOLS` | `default` (the ref's value, above) | `default`, `0`, `1`       | Piano DevTool on device (analytics WS stream, MIDI/SoundFont sync, local-network Info.plist keys). `config/__tests__/devtool-flag.test.ts` fails if this file defaults it to a truthy value or loses the strict-`0` guard. |
| `EXPO_PUBLIC_APP_VARIANT`          | `default` = per ref (above)        | `default`, `store`, `sim` | `store` = real IAP + ads; `sim` = stub IAP that is always premium, no ads.                                                                                                                                                 |

The `runtime`, `sim-release` and `e2e` jobs source their own env files (`config/runtime-shell.env`, `.maestro/e2e.env`) after these, so they are not affected.

### Apple / iOS

| Variable                                          | What                                                                                                                                                                                                                                               |
| ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `APP_STORE_CONNECT_API_KEY_ID`                    | App Store Connect API Key ID (10-char alphanumeric).                                                                                                                                                                                               |
| `APP_STORE_CONNECT_API_ISSUER_ID`                 | App Store Connect Issuer ID (UUID).                                                                                                                                                                                                                |
| `APP_STORE_CONNECT_API_KEY_CONTENT`               | Contents of the `.p8` private key. The raw text (`-----BEGIN PRIVATE KEY-----`) or its base64 (`base64 -i AuthKey_XXXXXX.p8 \| pbcopy`) both work. Same names as magic-lyrics / leap-sim-guitar.                                                   |
| `DEV_TEAM_ID` (`APPLE_TEAM_ID` still works)       | 10-character Apple Developer Team ID. Used by `fastlane/Appfile` and the signing settings.                                                                                                                                                         |
| `MATCH_GIT_BASIC_AUTHORIZATION`, `MATCH_PASSWORD` | Basic-auth header for the certificates repo (read in `fastlane/Matchfile`) and the repo encryption passphrase (read by `match`). The repo itself (`https://gitlab.amanotes.net/sdk/code-signing.git`, branch `develop`) is fixed in the Matchfile. |

### Firebase / analytics

| Variable | What |
| -------- | ---- |

### Android / Google Play (not used by CI yet)

| Variable                    | What                                                                                               |
| --------------------------- | -------------------------------------------------------------------------------------------------- |
| `ANDROID_KEYSTORE_BASE64`   | Base64-encoded release keystore `.keystore`. Generate with `base64 -i release.keystore \| pbcopy`. |
| `ANDROID_KEYSTORE_PASSWORD` | Keystore store password.                                                                           |
| `ANDROID_KEY_ALIAS`         | Key alias inside the keystore.                                                                     |
| `ANDROID_KEY_PASSWORD`      | Key password (often equal to store password).                                                      |
| `PLAY_STORE_JSON_BASE64`    | Base64-encoded Google Play service-account JSON (from Google Cloud Console).                       |

### Slack / release

| Variable                                                                                                        | What                                                                                                                                                                                                                                                                                                                                                               |
| --------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `SLACK_WEBHOOK_URL` _(masked)_                                                                                  | Incoming webhook for build notifications (optional; Fastlane no-ops when unset).                                                                                                                                                                                                                                                                                   |
| `RELEASE_PAT` _(GitHub secret, optional)_                                                                       | PAT or fine-grained token with `contents: write` for [`.github/workflows/release-tag.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/.github/workflows/release-tag.yml). Without it, the workflow falls back to `GITHUB_TOKEN` (tag + GitHub Release still run; other workflows are not re-triggered by that push). |
| `EXPO_PUBLIC_REVENUECAT_IOS_API_KEY`, `EXPO_PUBLIC_REVENUECAT_ANDROID_API_KEY`, `EXPO_PUBLIC_APPSFLYER_DEV_KEY` | Client-side SDK keys inlined into the JS bundle. The iOS RevenueCat and AppsFlyer dev keys are tracked in `.env`; a non-empty variable overrides them.                                                                                                                                                                                                             |
| `EXPO_PUBLIC_APPSFLYER_APP_ID` _(optional)_                                                                     | Defaults to the public App Store id from `.gitlab-ci.yml`.                                                                                                                                                                                                                                                                                                         |

## One-time prerequisites (humans only)

These steps cannot be automated; do them once before the first green CI run.

### 1. App Store Connect

1. Enroll the team in the Apple Developer Program (if not already).
2. Register an App ID for `com.musicapp.magicpiano` (matches [app.config.ts](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app.config.ts)).
3. Create the app in App Store Connect with the same bundle ID.
4. Generate an **App Store Connect API Key** with the **Admin** role (Users and Access → Integrations → App Store Connect API). Download the `.p8` — Apple only lets you download it once. Record the **Key ID** and **Issuer ID**.

### 3. App icon test (Product Page Optimization)

App Store Connect A/B tests app icons on the product page: up to **three treatments** against the original, **one test at a time**, for up to **90 days**. Every icon a treatment uses must be in the binary of the **published** version, and a test with icons goes through App Review.

- The original is the primary icon (`icon` in [app.config.ts](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/app.config.ts), `assets/icon.png`). The treatments' icons are `APP_ICON_TEST_VARIANTS` in [`plugins/withAlternateAppIcons.ts`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/plugins/withAlternateAppIcons.ts): 1024 × 1024 PNGs in [`assets/app-icons/`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/assets/app-icons/), opaque (App Store Connect rejects an icon with an alpha channel), square corners.
- The App Store picks the icon: an install from a treatment's page shows that treatment's icon on the Home Screen. The app never switches icons itself.
- Steps: ship a version whose build carries the icons and get it approved → App Store Connect → the app → **Product Page Optimization** → new test, one treatment per alternate icon set (`AppIcon-…`) → submit the test for review.
- Keeping a winning icon: make it the primary icon in a later version — a treatment's icon only lives as long as the test.

### 2. Google Play Console

1. Create the app under package `com.musicapp.magicpiano`.
2. Locally generate an **upload keystore** (one-time):
   ```bash
   keytool -genkeypair -v -keystore release.keystore \
     -keyalg RSA -keysize 2048 -validity 10000 \
     -alias magicpiano-upload
   ```
   Keep `release.keystore` somewhere safe (1Password / shared vault) — losing it locks you out of pushing future updates. Base64-encode it for the `ANDROID_KEYSTORE_BASE64` variable.
3. Opt into **Play App Signing** (Google holds the app-signing key; we only ever upload with the upload key).
4. Upload one build manually to the **Internal testing** track so Play knows the app exists and the track is initialized — `supply` cannot create a track from scratch.
5. In Google Cloud Console, create a **Service Account**, grant it `Service Account User`, and create a JSON key. In Play Console → Users and permissions, invite the service account email and grant **Admin (all permissions)** or at minimum **Release manager** for the app. Base64-encode the JSON for `PLAY_STORE_JSON_BASE64`.

## Local invocation

After populating a local `.env` with the same variables (or exporting them in your shell), Fastlane lanes can be run directly. The first time, install gems:

```bash
bundle install
```

Then:

```bash
bun run ios:beta     # bundle exec fastlane ios beta_testflight
bun run android:beta # bundle exec fastlane android beta_playstore
```

For Android lanes locally, copy [`android/keystore.properties.example`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/android/keystore.properties.example) to `android/keystore.properties` and drop `release.jks` at `android/app/release.jks` (both are gitignored).

## Cost & runtime notes

- **All CI jobs** run on the self-hosted `tart-studio-macos-builder` Tart runner, so they consume no hosted CI minutes. Xcode, Node, Bun, Ruby, Fastlane and CocoaPods come from the golden image; jobs only select the pinned versions, then run `bundle install` and `bun install`.
- Each job is a **fresh VM**, so there is no warm `ios/`, `android/`, `node_modules/` or Gradle cache between runs. Expect a cold `pod install` every time; only the Bun install cache is restored (GitLab `cache:`).
- The iOS jobs run `bun run lint` (Biome format + lint) right after their `bun install` (`.lint_biome` in [`iOS.yml`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/iOS.yml)), so a lint error fails fast before the catalog sync, prebuild and Xcode archive, with no extra VM or cache restore. `validate` (every build pipeline, shared `.rules_quality` rules) runs lint, typecheck, the security scan and all unit tests, and both iOS jobs `need` it.
- `interruptible: true` cancels superseded runs on the same ref when **Auto-cancel redundant pipelines** is enabled in the project settings.

## Runner setup notes

The `tart-studio-macos-builder` runner must be the GitLab Tart executor (jobs use the `image:` key to pick the VM) with network access to `ociregistry.amanotes.net` (and credentials for it if the registry is private). Symptom of a runner that is not a Tart executor: `image:` is ignored and Xcode / Bun are missing from `PATH`.

## Known limitations

- **No Android on CI** — no Android jobs exist yet. When they ship, add an image with JDK 17 + the Android SDK, a `GOOGLE_SERVICES_JSON_BASE64` variable (iOS prebuild passes `--platform ios`, so `google-services.json` is not required today), and jobs that decode the keystore / Play Console JSON before `fastlane android beta_playstore`.
- **No IPA artifacts** — build jobs only publish to TestFlight; the binaries are not kept as GitLab job artifacts (the old GitHub uploads were disabled for quota reasons). Add `artifacts:` to the build jobs if you want downloadable copies.
- **E2E simulator build not ported** — the old `e2e-simulator-build` workflow was already disabled; see [.maestro/README.md](/quality/guide.md).
- **`match` is readonly and env-driven** — [`fastlane/Matchfile`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/fastlane/Matchfile) hardcodes git storage, the certificates repo (`https://gitlab.amanotes.net/sdk/code-signing.git`), branch `develop`, type `appstore` and the bundle id (`APP_BUNDLE_ID`); the lane only adds `readonly: true`. `MATCH_GIT_BASIC_AUTHORIZATION` and `MATCH_PASSWORD` come from the GitLab CI/CD variables. If they are missing, `beta_testflight` fails in the `match` step. The Fastfile no longer installs Apple's WWDR intermediates into the temp keychain; if codesign reports "certificate is not trusted", re-add that step.
- **RN 0.85 ↔ Xcode** — CI pins `TART_IMAGE_TAG` (`bun1.3.13-xcode27.1-r1` → Xcode 27.1 in the golden image). To move Xcode versions, bump `ci/.xcode-version` in `tart-vm-golden-image`, rebuild the image, and update `TART_IMAGE_TAG` here.
- **`pilot` skip submission** — TestFlight processes the build but does not auto-submit to external testers. Promote builds in App Store Connect manually after processing finishes.

## Sentry symbols

Sentry runs next to Crashlytics. Set these GitLab CI variables: `EXPO_PUBLIC_SENTRY_DSN` (public),
`SENTRY_ORG`, `SENTRY_PROJECT`, and `SENTRY_AUTH_TOKEN` (**masked secret**, never committed).
The `@sentry/react-native/expo` config plugin adds the Xcode build phase that uploads JS
sourcemaps; `fastlane/Fastfile` `upload_sentry_dsyms!` uploads the dSYMs right after the size
report. Both are skipped when the variables are unset and never fail the lane.

`ios_internal` and `ios_release` run `scripts/verify-sentry-env.ts` (`.verify_sentry` in `iOS.yml`)
before `expo prebuild`. It fails the job when `SENTRY_AUTH_TOKEN`, `SENTRY_ORG`, `SENTRY_PROJECT` or
`EXPO_PUBLIC_SENTRY_DSN` is missing, the DSN is malformed, or Sentry rejects the token (401/403) or
does not know the org (404). A network error or 5xx only warns. Set `SKIP_SENTRY_VERIFY=1` to build
without Sentry symbols.

***

<small>Source: [`docs/ci-cd.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/ci-cd.md)</small>
