Detox构建命令本地正常,GitHub Action中执行失败求助
问题:Detox在GitHub Action中构建失败,本地正常运行
我的.detoxrc.json配置如下:
"apps": { "ios": { "type": "ios.app", "binaryPath": "./ios/build/Build/Products/Debug-iphonesimulator/<my-app-demo>.app", "build": "xcodebuild -workspace ios/<my-app>.xcworkspace -configuration Debug -scheme '<my-app-demo>' -sdk iphonesimulator -derivedDataPath ios/build" } },
本地可正常完成构建和测试,但在GitHub Action中执行yarn detox build -c ios或直接运行配置内的build命令均失败。当前Action的yml配置:
env: AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} jobs: build: name: iOS Simulator Tests runs-on: macos-12 steps: - name: Checkout uses: actions/checkout@v3 - name: Node uses: actions/setup-node@v3 with: node-version: 16 cache: "yarn" - name: Cache node_modules id: cache uses: actions/cache@v2 with: path: ./node_modules key: ${{ runner.os }}-modules-${{ hashFiles('yarn.lock') }} - name: Install node_modules if: steps.cache.outputs.cache-hit != 'true' run: yarn install --frozen-lockfile - name: Pod Install run: cd ios && pod install && cd .. - name: Install Applesimutils run: | brew tap wix/brew brew install applesimutils - name: Detox build run: yarn detox build -c ios - name: Detox test run: yarn detox test -c ios --cleanup
失败日志核心内容:
error: /Users/runner/work/buyer-app/buyer-app/ios/my-app/main.jsbundle: No such file or directory (in target 'my-app Feat' from project 'my-app') error: /Users/runner/work/buyer-app/buyer-app/ios/my-app/assets: No such file or directory (in target 'my-app Feat' from project 'my-app') ** BUILD FAILED ** The following build commands failed: CpResource /Users/runner/work/buyer-app/buyer-app/ios/my-app/main.jsbundle /Users/runner/work/buyer-app/buyer-app/ios/build/Build/Products/Debug-iphonesimulator/my-app\ Feat.app/main.jsbundle (in target 'my-app Feat' from project 'my-app') CpResource /Users/runner/work/buyer-app/buyer-app/ios/my-app/assets /Users/runner/work/buyer-app/buyer-app/ios/build/Build/Products/Debug-iphonesimulator/my-app\ Feat.app/assets (in target 'my-app Feat' from project 'my-app')
原因与解决方案
从错误日志可明确:GitHub Runner环境中缺少main.jsbundle和ios/my-app/assets目录,这两个是Xcode构建时必须复制的资源文件。本地正常是因为这些文件可能已通过预构建脚本生成、存在本地缓存,或已提交至仓库,但CI环境是全新的干净环境,需手动处理。
解决步骤:
确认资源生成逻辑
- 检查
main.jsbundle是否由React Native的metro打包生成:本地开发时可能自动生成,但CI环境需显式执行打包命令。 - 检查
assets目录是否由脚本生成(如图标、静态资源打包),或是否被.gitignore忽略导致CI拉取代码后不存在。
- 检查
在GitHub Action中添加资源生成步骤
在Pod Install步骤之后、Detox build步骤之前,插入生成资源的命令:- name: Generate main.jsbundle and assets run: | # 生成iOS的main.jsbundle,根据项目实际入口文件调整(如index.js或index.ios.js) yarn react-native bundle --entry-file index.js --bundle-output ios/my-app/main.jsbundle --platform ios --dev false # 若assets需脚本生成,执行对应命令,例如: # yarn generate-assets检查.gitignore配置
如果main.jsbundle或assets目录被.gitignore排除,要么取消忽略并提交至仓库,要么确保CI环境通过命令生成这些文件。验证Xcode项目资源引用
打开Xcode项目,确认main.jsbundle和assets目录的引用路径是否正确,避免因路径大小写、相对路径问题导致CI环境找不到文件(macOS本地文件系统大小写不敏感,但CI环境可能存在差异)。调整Detox build命令(可选)
若项目的Xcode构建流程本身包含资源生成脚本,检查是否需在build命令中添加额外参数,确保资源生成步骤被触发。
内容的提问来源于stack exchange,提问作者crevulus
相关产品推荐
相关产品推荐

