跳到主要内容

CI/CD 实践

问题

如何为 iOS 项目搭建 CI/CD 流水线?

答案

流水线设计

Fastlane 配置

fastlane/Fastfile
default_platform(:ios)

platform :ios do
desc "Run tests"
lane :test do
scan(
scheme: "MyApp",
devices: ["iPhone 15"],
clean: true
)
end

desc "Build and upload to TestFlight"
lane :beta do
increment_build_number
build_app(scheme: "MyApp")
upload_to_testflight
slack(message: "New TestFlight build uploaded!")
end
end

GitHub Actions

.github/workflows/ios.yml
name: iOS CI
on:
push:
branches: [main]
pull_request:

jobs:
test:
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_15.4.app
- name: Run Tests
run: |
xcodebuild test \
-scheme MyApp \
-destination 'platform=iOS Simulator,name=iPhone 15' \
-resultBundlePath TestResults \
| xcpretty

PR 门禁

检查项工具
编译通过xcodebuild
单元测试XCTest
SwiftLintswiftlint --strict
包体积检查比对 IPA 大小
覆盖率xcresult → Codecov

常见面试问题

Q1: iOS CI 最大的挑战是什么?

答案:macOS 环境依赖(只能在 Mac 上构建)、签名证书管理(需要 .p12 + provisioning profile)、模拟器启动慢。建议用 Fastlane match 管理证书(Git 仓库存储),GitHub Actions 的 macOS runner 虽贵但省维护。

相关链接