Expo应用的部署和发布流程是开发周期的最后一步,也是确保应用成功上线的关键环节。Expo提供了多种部署选项,从开发测试到生产发布都有完善的工具支持。
部署流程概览:
- 开发阶段:使用Expo Go进行快速迭代
- 测试阶段:使用Development Build进行真实设备测试
- 预发布阶段:使用EAS Build生成测试版本
- 生产阶段:使用EAS Build和Submit发布到应用商店
EAS Build部署:
- 配置EAS
bash# 安装EAS CLI npm install -g eas-cli # 登录Expo账户 eas login # 配置项目 eas build:configure
- 配置eas.json
json{ "cli": { "version": ">= 5.2.0" }, "build": { "development": { "developmentClient": true, "distribution": "internal", "android": { "buildType": "apk" }, "ios": { "simulator": false } }, "preview": { "distribution": "internal", "android": { "buildType": "apk" }, "ios": { "simulator": true } }, "production": { "android": { "buildType": "app-bundle" }, "ios": { "autoIncrement": true } } }, "submit": { "production": { "android": { "serviceAccountKeyPath": "./google-service-account.json", "track": "internal" }, "ios": { "appleId": "your-apple-id@email.com", "ascAppId": "YOUR_APP_STORE_CONNECT_APP_ID", "appleTeamId": "YOUR_TEAM_ID", "skipWorkflow": false } } } }
- 构建应用
bash# 构建开发版本 eas build --profile development --platform android # 构建预览版本 eas build --profile preview --platform ios # 构建生产版本 eas build --profile production --platform android # 本地构建 eas build --local --platform android
Android部署:
- 准备Google Play账户
bash# 创建Google Play开发者账户 # 访问 https://play.google.com/console
- 配置Google Play服务账户
bash# 创建服务账户 # 1. 访问 Google Play Console # 2. 进入 Settings > API access # 3. 创建服务账户 # 4. 下载JSON密钥文件 # 5. 授予服务账户权限
- 提交到Google Play
bash# 提交应用 eas submit --platform android --latest # 提交特定构建 eas submit --platform android --build-id BUILD_ID # 指定发布轨道 eas submit --platform android --track internal
- Google Play发布轨道
- Internal:内部测试(最多100名测试者)
- Alpha:封闭测试(无限制)
- Beta:开放测试
- Production:正式发布
iOS部署:
- 准备Apple开发者账户
bash# 注册Apple开发者计划 # 访问 https://developer.apple.com/programs/
- 配置证书和配置文件
bash# 使用EAS自动管理证书 # 或手动配置: # 1. 创建App ID # 2. 创建开发证书 # 3. 创建发布证书 # 4. 创建配置文件
- 配置app.json
json{ "expo": { "ios": { "bundleIdentifier": "com.yourcompany.yourapp", "buildNumber": "1", "supportsTablet": true, "infoPlist": { "NSCameraUsageDescription": "Need camera permission", "NSLocationWhenInUseUsageDescription": "Need location permission" } } } }
- 提交到App Store
bash# 提交应用 eas submit --platform ios --latest # 使用TestFlight进行测试 # 1. 上传到App Store Connect # 2. 添加测试者 # 3. 分发TestFlight版本
OTA更新部署:
- 配置更新
json{ "expo": { "updates": { "url": "https://u.expo.dev/your-project-id" }, "runtimeVersion": { "policy": "appVersion" } } }
- 发布更新
bash# 创建更新 eas update --branch production --message "Fix bug" # 查看更新历史 eas update:list # 回滚更新 eas update:rollback --branch production
Web部署:
- 构建Web版本
bash# 构建生产版本 npx expo export:web # 本地预览 npx expo start --web
- 部署到Vercel
bash# 安装Vercel CLI npm i -g vercel # 部署 vercel # 生产部署 vercel --prod
- 部署到Netlify
bash# 安装Netlify CLI npm i -g netlify-cli # 部署 netlify deploy --prod
环境变量管理:
- 配置环境变量
bash# 设置EAS环境变量 eas secret:create --name API_KEY --value "your-api-key" # 查看环境变量 eas secret:list # 删除环境变量 eas secret:delete --name API_KEY
- 在代码中使用
typescript// 访问环境变量 const API_KEY = process.env.EXPO_PUBLIC_API_KEY; const API_URL = process.env.EXPO_PUBLIC_API_URL;
版本管理:
- 版本号规范
json{ "expo": { "version": "1.0.0", "ios": { "buildNumber": "1" }, "android": { "versionCode": 1 } } }
- 自动化版本管理
bash# 使用semantic-release npm install --save-dev semantic-release # 配置.releaserc.json { "branches": ["main"], "plugins": [ "@semantic-release/commit-analyzer", "@semantic-release/release-notes-generator", "@semantic-release/npm", "@semantic-release/github" ] }
CI/CD集成:
- GitHub Actions配置
yamlname: Build and Deploy on: push: branches: [main] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '18' - run: npm ci - run: npm test - run: eas build --platform android --non-interactive env: EXPO_TOKEN: ${{ secrets.EXPO_TOKEN }}
- 自动化测试和部署
yamlname: CI/CD Pipeline on: push: branches: [main, develop] pull_request: branches: [main] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - run: npm ci - run: npm test build-android: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v2 - run: eas build --platform android --non-interactive env: EXPO_TOKEN: ${{ secrets.EXPO_TOKEN }} build-ios: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v2 - run: eas build --platform ios --non-interactive env: EXPO_TOKEN: ${{ secrets.EXPO_TOKEN }}
最佳实践:
- 版本控制:使用语义化版本号
- 自动化测试:在部署前运行所有测试
- 渐进式发布:先发布到测试轨道,再发布到生产环境
- 监控和日志:部署后监控应用性能和错误
- 回滚计划:准备好快速回滚的方案
- 文档记录:记录每次发布的内容和变更
常见问题:
- 构建失败:检查配置文件和依赖版本
- 提交被拒绝:确保符合应用商店的审核指南
- 更新不生效:检查运行时版本和更新配置
- 签名问题:确保证书和配置文件正确配置
通过完善的部署流程,可以确保Expo应用顺利上线并保持稳定运行。