iOS Project CI/CD From Day One with Fastlane
Every new iOS project starts the same way: a clean Xcode template, a bundle identifier, and the best intentions. Three months later the build only works on one laptop, signing breaks every time someone adds a device, and nobody remembers which Ruby the fastlane setup needs.
We have started enough apps to know this is avoidable if you set up the iOS CI/CD pipeline on day one, when the project is empty and every decision is cheap. So we turned our setup into a public repository, xcode-scripts, that generates it for any new Swift Package Manager project, and we just rewrote it as version 2.0. This post explains what the setup contains and, more usefully, the traps we hit while building it, because most of them will bite you even if you never use our scripts.
Summary
- Gather the signing requirements before you create the Xcode project: bundle ID, team ID, API key, match repo.
- Make the toolchain reproducible: Ruby, Xcode and gems pinned in files, plus a
doctorscript that explains what is wrong. - Give builds one entry point: the same fastlane lanes run on your machine and on CI, with zero warnings allowed.
- Write test and coverage reports even when tests fail, otherwise your CI shows nothing exactly when you need it.
- Check certificates on a schedule, and test your tooling like you test your app.
Start with the checklist, not with Xcode
The most boring step saves the most time. Before touching the project, collect everything the pipeline will ask for:
- A GitHub repository for the app.
- The app name and a bundle identifier already registered on the developer portal.
- Your Apple team ID.
- A private git repository for match, which stores your certificates and provisioning profiles encrypted so the whole team shares them.
- An App Store Connect API key: key ID, issuer ID and the
.p8file. - The app created in App Store Connect.
Then create the Xcode project at the root of the cloned repository, close Xcode and run the generator from the project root. It asks for a few values (team ID, match URL, which CI to add) and writes scripts/, a Gemfile, a fastlane/Fastfile, the CI workflows and a .gitignore. It never overwrites an existing file, and it can run unattended with environment variables, which is how we test it.
A toolchain you can rebuild in one command
"Works on my machine" usually means a Ruby, an Xcode or a gem version that nobody wrote down. The generated project writes them down:
.ruby-versionpins Ruby. The generator takes the version from one file in our repo,versions.env, and from then on the project owns it..xcode-versionrecords the Xcode you generated with. The build script and the CI workflow select it when it is installed.Gemfile.lockpins bundler and fastlane. Commit it.scripts/setup.shinstalls rbenv, the right Ruby and the gems. It is idempotent, so running it twice is harmless.scripts/doctor.shchecks Xcode, simulator, Ruby, gems, the shared scheme, signing and CI, and prints a fix for each problem it finds.
Two details came from running this on real, clean machines. Homebrew's ruby-build can be too old to know a recent Ruby, so setup.sh falls back to the upstream plugin. And fastlane crashes with invalid byte sequence in US-ASCII when the shell locale is not UTF-8, so doctor now warns about it.
For installing Xcode itself on a build machine we use xcodes. Its README says downloads need an Apple ID and, the first time, an interactive two-factor code, so do that once by hand.
Keeping the pinned versions fresh
Pinned versions rot. Our repo has a weekly GitHub Actions job that tries the newest stable Ruby against the full smoke test and opens a pull request bumping versions.env only if it passes, or an issue if it fails. Generated projects also get a Dependabot config for GitHub Actions and gems. Nobody has to remember to update anything, and nothing is updated without a green test.
One entry point: the same lane everywhere
scripts/build.sh is deliberately thin. With no arguments it runs fastlane build; with INTENT=test it runs fastlane test; with firebase or testflight it runs the distribution lanes. Locally and on CI you call the same lanes, so "it passed on CI but not here" has far fewer places to hide.
The build lane fails on warnings. The default is zero:
MAX_WARNINGS=0 ./scripts/build.sh
The lane counts the compiler and linker warnings in the xcodebuild log and fails above the limit. It ignores warnings that come from inside Swift packages, because those are your dependencies' warnings, not yours. Starting at zero on an empty project costs nothing. Introducing the rule on a two-year-old codebase is a different story, and that is why it belongs on day one.
The simulator is chosen automatically (newest installed iOS runtime and iPhone), and you can override it with RUNTIME and DEVICE. On Jenkins, where several jobs share one machine, the lane creates a throw-away simulator per job so they do not step on each other.
Tests that still report when they fail
This part cost us the most debugging. Our first test lane looked simple: build with gym, then run the tests. It built fine and found no tests.
The cause is that gym appends its own plain build action after the arguments you pass, which silently drops the test bundles. The fix is to build for testing with fastlane's scan (build_) and then run the tests with test_. We only found this because of the end-to-end smoke test described below.
Reporting had its own surprises:
- Reports must survive failures. The lane reads the
.xcresultbundle after the tests, writes the JUnit and coverage files, and only then fails. A red build with no report is the worst combination. - No extra tools. JUnit comes from fastlane's
trainerand coverage fromxccov, converted tocoverage/cobertura.xml. TheGemfilecontains justfastlaneandxcodeproj. We dropped slather and xchtmlreport. - Empty files can break CI plugins. A zero-byte JUnit file made a Jenkins plugin fail to parse it, so build-only runs write an empty but well-formed one. The Cobertura case is the opposite: the Jenkins Coverage plugin rejects an empty file, so build-only runs write none.
- UI tests are reported, not enforced, by default. They are flaky enough that most teams start by watching them.
fail_flips that when you are ready.on_ ui_ test_ failure: true
CI that calls your lanes
The generator can add GitHub Actions, a Jenkinsfile, or both. The pull request workflow is short because the work lives in the lanes:
- uses: ruby/setup-ruby@v1
with:
bundler-cache: true
- name: Build and test
run: bundle exec fastlane test
It runs on every pull request and push to main, selects the Xcode from .xcode-version, and uploads the coverage and test results as artifacts even when the run fails. A second, manually triggered workflow sends a build to Firebase App Distribution or TestFlight.
The template uses macos-latest. According to the runner images repository, the -latest labels point to the newest stable OS image and move gradually, so pin a specific label if you need a fixed macOS.
The API key never enters the repository
Distribution needs the App Store Connect key. fastlane's documentation lists "no 2FA needed" as a benefit of API key authentication, which is exactly what you want on a build machine. In the generated Fastfile a helper reads ASC_, ASC_ and ASC_ from the environment (CI secrets) and falls back to the key file for local runs. The build and test lanes never need the key at all, so pull requests from forks do not need access to your secrets.
One small trap: an unset CI secret arrives as an empty string, and an empty ASC_ was overriding the value in the Fastfile. Empty values are now ignored.
Signing without the yearly panic
Certificates expire, usually when nobody is thinking about them. The lanes cover the whole lifecycle:
| Lane | When you use it |
|---|---|
sync_ | Writes the enabled devices of the developer portal to devices.txt, with a diff (dry_ to only look) |
update_ | Registers devices and regenerates the development, ad hoc and App Store profiles with match |
install_ | Installs existing profiles read-only, for people without portal access |
check_ | Lists profiles and certificates with their expiry; strict:true fails when something is expired or about to expire |
renew_ | Deletes the stale profiles and regenerates everything; dry_ only lists what would be deleted |
The one we recommend wiring up is check_ with strict:true as a weekly scheduled CI job. It is read-only and needs just the API key, so an expiring certificate becomes a failed job a month ahead, not an unsigned release on a Friday. For installing profiles, match has a readonly option that, per its documentation, only fetches existing credentials and does not generate new ones, which is the safe mode for CI.
Test the tooling like you test the app
Scripts that nobody tests stop working silently. The most valuable thing we added in 2.0 is tests/smoke.sh: it generates a throwaway iOS project, runs the generator unattended, and checks the result. That means the generated files, bump_, a real build, unit and UI tests, JUnit and coverage output, a deliberate warning failing the build, and a failing test still writing its report. It runs in GitHub Actions on every change to the generator, lanes or templates, next to an always-on syntax lint.
It paid for itself immediately. It found the gym problem above, plus a build script that rejected any bundler other than 2.x. A second round, run from a clean env -i shell to mimic a fresh machine, found the UTF-8 and ruby-build issues. A few lessons from that:
- Test the generator output, not just the generator.
- Run it in a clean environment; your own shell hides assumptions.
- Let a hosted runner be the second opinion. Ours found a bug on its first run.
Smaller traps worth knowing
- Do not name a lane
testflight. It clashes with fastlane's action of the same name. Ours isship_, and the oldtestflight iq_helper stays as a deprecated alias that warns.beta xcodebuild -showBuildSettingscan be slow. fastlane gave it three seconds and timed out on a busy machine before running anything. We raised it to 30 (FASTLANE_changes it).XCODEBUILD_ SETTINGS_ TIMEOUT - Share the scheme and enable coverage for all targets, or the generated test and coverage steps find nothing.
doctorchecks both. - Build numbers from a timestamp (
YYYYMMddHHmm) avoid collisions between machines and need no counter in the repo.
Your first-week checklist
- Register the bundle ID, create the app in App Store Connect, create the API key.
- Create the match repository and the Xcode project, share the scheme.
- Run the generator, then
./scripts/setup.shand./scripts/doctor.sh. - Run
./scripts/build.shandINTENT=test ./scripts/build.shlocally. Both must be green. - Push, and confirm the CI workflow passes on a pull request.
- Add the distribution secrets and ship a build with
fastlane ship_.testflight - Schedule
check_withcerts strict:trueweekly.
When you would rather not own this
A pipeline like this is a one-time investment, but it needs someone who knows signing, fastlane and CI well enough to fix it at the worst moment. At Inqbarna we build and ship iOS apps end to end, and we start every project with this setup. If you want a second pair of eyes on your delivery process, see our services or get in touch. And if you are debugging a different kind of iOS problem, our post on Universal Links opening Safari may help.
FAQ
Do I need fastlane to have CI/CD for an iOS app?
No, you can call xcodebuild directly. We use fastlane because signing with match, uploading to TestFlight and Firebase, and reading test results are already solved there, and because lanes give you one command that behaves the same on a laptop and on CI.
Does xcode-scripts support CocoaPods?
No. Version 2.0 is Swift Package Manager only; we removed the CocoaPods steps.
Can I use it on an existing project?
It is designed for new projects, where the generator can write everything without conflicts. On an existing one, the shared lanes in fastlane/CommonFastfile and the templates are still a useful reference to copy from.
Why fail the build on any warning?
Because a warning that is allowed today hides the one that matters tomorrow. Warnings from Swift packages are ignored, so you only fix what you own, and MAX_ lets you loosen the limit on a legacy codebase.
Where do the signing certificates live?
In your private match repository, encrypted, per the match documentation. CI only needs read access to it plus the passphrase, and the App Store Connect key stays in CI secrets.
Sources
- Inqbarna: xcode-scripts repository and README
- fastlane: match
- fastlane: scan
- fastlane: App Store Connect API
- xcodes: XcodesOrg/xcodes
- GitHub: actions/runner-images



