Build and Upload Android APK to Version-Matched Release #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build and Upload Android APK to Version-Matched Release | |
| # 触发条件:推送到 main 分支或手动触发 | |
| on: | |
| push: | |
| branches: | |
| - main | |
| paths: | |
| - 'android/**' | |
| workflow_dispatch: # 允许手动触发 | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. 检出代码 | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| # 2. 设置 JDK 环境 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # 3. 安装 Android SDK | |
| - name: Setup Android SDK | |
| uses: android-actions/setup-android@v3 | |
| # 4. 进入 android 目录并设置 Gradle 可执行权限 | |
| - name: Set up Android build environment | |
| run: | | |
| cd android | |
| chmod +x ./gradlew | |
| # 5. 构建 Debug APK 并重命名为时间戳 | |
| - name: Build Debug APK with Timestamp | |
| run: | | |
| cd android | |
| ./gradlew assembleDebug | |
| # Set timezone to Beijing (UTC+8) for the timestamp | |
| export TZ='Asia/Shanghai' | |
| TIMESTAMP=$(date +"%Y%m%d_%H%M%S") | |
| cp app/build/outputs/apk/debug/app-debug.apk app/build/outputs/apk/debug/app-debug-${TIMESTAMP}.apk | |
| echo "APK_PATH=android/app/build/outputs/apk/debug/app-debug-${TIMESTAMP}.apk" >> $GITHUB_ENV | |
| # 6. 提取版本号 | |
| - name: Extract versionName | |
| id: version | |
| run: | | |
| cd android | |
| VERSION_NAME=$(grep "DIMINA_VERSION" gradle.properties | cut -d'=' -f2 | tr -d ' ') | |
| echo "VERSION_NAME=$VERSION_NAME" >> $GITHUB_ENV | |
| echo "Extracted versionName: $VERSION_NAME" | |
| # 7. 检查目标 Release 是否存在 | |
| - name: Check if Release exists | |
| id: check_release | |
| run: | | |
| TAG_NAME="v${{ env.VERSION_NAME }}" | |
| RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
| "https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG_NAME") | |
| if echo "$RESPONSE" | grep -q '"id":'; then | |
| echo "Release $TAG_NAME exists" | |
| echo "release_exists=true" >> $GITHUB_ENV | |
| else | |
| echo "Release $TAG_NAME does not exist" | |
| echo "release_exists=false" >> $GITHUB_ENV | |
| fi | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| # 8. 上传 APK 到现有 Release(如果存在) | |
| - name: Upload APK to Existing Release | |
| if: env.release_exists == 'true' | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: v${{ env.VERSION_NAME }} | |
| files: ${{ env.APK_PATH }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |