|
| 1 | +name: Build Android APK |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [ main, develop ] |
| 6 | + pull_request: |
| 7 | + branches: [ main ] |
| 8 | + workflow_dispatch: # 允许手动触发 |
| 9 | + |
| 10 | +jobs: |
| 11 | + build-android: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: 检出代码 |
| 16 | + uses: actions/checkout@v4 |
| 17 | + |
| 18 | + - name: 设置 Node.js |
| 19 | + uses: actions/setup-node@v4 |
| 20 | + with: |
| 21 | + node-version: '18' |
| 22 | + cache: 'yarn' |
| 23 | + |
| 24 | + - name: 设置 Java 17 |
| 25 | + uses: actions/setup-java@v4 |
| 26 | + with: |
| 27 | + distribution: 'temurin' |
| 28 | + java-version: '17' |
| 29 | + |
| 30 | + - name: 设置 Android SDK |
| 31 | + uses: android-actions/setup-android@v3 |
| 32 | + |
| 33 | + - name: 安装依赖 |
| 34 | + run: yarn install --frozen-lockfile |
| 35 | + |
| 36 | + - name: 生成数据库 |
| 37 | + run: npx drizzle-kit generate |
| 38 | + |
| 39 | + - name: 安装 Expo CLI |
| 40 | + run: npm install -g @expo/cli |
| 41 | + |
| 42 | + - name: 预构建 Android |
| 43 | + run: npx expo prebuild -p android --clear |
| 44 | + |
| 45 | + - name: 创建 local.properties |
| 46 | + run: | |
| 47 | + echo "sdk.dir=$ANDROID_HOME" > android/local.properties |
| 48 | + |
| 49 | + - name: 构建 APK (Debug) |
| 50 | + run: | |
| 51 | + cd android |
| 52 | + ./gradlew assembleDebug |
| 53 | + |
| 54 | + - name: 上传 APK 构建产物 |
| 55 | + uses: actions/upload-artifact@v4 |
| 56 | + with: |
| 57 | + name: android-apk-debug |
| 58 | + path: android/app/build/outputs/apk/debug/app-debug.apk |
| 59 | + retention-days: 30 |
| 60 | + |
| 61 | + # 可选:构建 Release APK (需要签名配置) |
| 62 | + build-android-release: |
| 63 | + runs-on: ubuntu-latest |
| 64 | + if: github.ref == 'refs/heads/main' # 仅在 main 分支触发 |
| 65 | + |
| 66 | + steps: |
| 67 | + - name: 检出代码 |
| 68 | + uses: actions/checkout@v4 |
| 69 | + |
| 70 | + - name: 设置 Node.js |
| 71 | + uses: actions/setup-node@v4 |
| 72 | + with: |
| 73 | + node-version: '18' |
| 74 | + cache: 'yarn' |
| 75 | + |
| 76 | + - name: 设置 Java 17 |
| 77 | + uses: actions/setup-java@v4 |
| 78 | + with: |
| 79 | + distribution: 'temurin' |
| 80 | + java-version: '17' |
| 81 | + |
| 82 | + - name: 设置 Android SDK |
| 83 | + uses: android-actions/setup-android@v3 |
| 84 | + |
| 85 | + - name: 安装依赖 |
| 86 | + run: yarn install --frozen-lockfile |
| 87 | + |
| 88 | + - name: 生成数据库 |
| 89 | + run: npx drizzle-kit generate |
| 90 | + |
| 91 | + - name: 安装 Expo CLI |
| 92 | + run: npm install -g @expo/cli |
| 93 | + |
| 94 | + - name: 预构建 Android |
| 95 | + run: npx expo prebuild -p android --clear |
| 96 | + |
| 97 | + - name: 创建 local.properties |
| 98 | + run: | |
| 99 | + echo "sdk.dir=$ANDROID_HOME" > android/local.properties |
| 100 | + |
| 101 | + # 配置签名 (需要在 GitHub Secrets 中添加相关密钥) |
| 102 | + - name: 配置签名 |
| 103 | + if: ${{ env.KEYSTORE_BASE64 != '' }} |
| 104 | + env: |
| 105 | + KEYSTORE_BASE64: ${{ secrets.KEYSTORE_BASE64 }} |
| 106 | + KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }} |
| 107 | + KEY_ALIAS: ${{ secrets.KEY_ALIAS }} |
| 108 | + KEY_PASSWORD: ${{ secrets.KEY_PASSWORD }} |
| 109 | + run: | |
| 110 | + echo $KEYSTORE_BASE64 | base64 --decode > android/app/my-release-key.keystore |
| 111 | + echo "MYAPP_RELEASE_STORE_FILE=my-release-key.keystore" >> android/gradle.properties |
| 112 | + echo "MYAPP_RELEASE_KEY_ALIAS=$KEY_ALIAS" >> android/gradle.properties |
| 113 | + echo "MYAPP_RELEASE_STORE_PASSWORD=$KEYSTORE_PASSWORD" >> android/gradle.properties |
| 114 | + echo "MYAPP_RELEASE_KEY_PASSWORD=$KEY_PASSWORD" >> android/gradle.properties |
| 115 | + |
| 116 | + - name: 构建 Release APK |
| 117 | + run: | |
| 118 | + cd android |
| 119 | + if [ -f "app/my-release-key.keystore" ]; then |
| 120 | + ./gradlew assembleRelease |
| 121 | + else |
| 122 | + echo "未找到签名文件,构建 Debug APK" |
| 123 | + ./gradlew assembleDebug |
| 124 | + fi |
| 125 | + |
| 126 | + - name: 上传 Release APK |
| 127 | + uses: actions/upload-artifact@v4 |
| 128 | + with: |
| 129 | + name: android-apk-release |
| 130 | + path: | |
| 131 | + android/app/build/outputs/apk/release/app-release.apk |
| 132 | + android/app/build/outputs/apk/debug/app-debug.apk |
| 133 | + retention-days: 90 |
0 commit comments