Skip to content

chore(deps): bump androidx.lifecycle:lifecycle-runtime-ktx in /androi… #29

chore(deps): bump androidx.lifecycle:lifecycle-runtime-ktx in /androi…

chore(deps): bump androidx.lifecycle:lifecycle-runtime-ktx in /androi… #29

Workflow file for this run

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 是否存在以及是否已有 APK
- name: Check if Release exists and has APK
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
# 检查是否已有 APK 文件
ASSETS_URL=$(echo "$RESPONSE" | jq -r '.assets_url')
if [ "$ASSETS_URL" != "null" ]; then
ASSETS_RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$ASSETS_URL")
APK_COUNT=$(echo "$ASSETS_RESPONSE" | jq '[.[] | select(.name | endswith(".apk"))] | length')
if [ "$APK_COUNT" -gt 0 ]; then
echo "Release already has $APK_COUNT APK file(s), skipping upload"
echo "has_apk=true" >> $GITHUB_ENV
else
echo "Release has no APK files"
echo "has_apk=false" >> $GITHUB_ENV
fi
else
echo "has_apk=false" >> $GITHUB_ENV
fi
else
echo "Release $TAG_NAME does not exist"
echo "release_exists=false" >> $GITHUB_ENV
echo "has_apk=false" >> $GITHUB_ENV
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 8. 上传 APK 到现有 Release(仅当 Release 存在且没有 APK 时)
- name: Upload APK to Existing Release
if: env.release_exists == 'true' && env.has_apk == 'false'
uses: softprops/action-gh-release@v2
with:
tag_name: v${{ env.VERSION_NAME }}
files: ${{ env.APK_PATH }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 9. 删除 Release 中的旧 APK(保留最新上传的)
- name: Clean up old APKs from Release
if: env.release_exists == 'true' && env.has_apk == 'false'
run: |
TAG_NAME="v${{ env.VERSION_NAME }}"
# 获取 release 中的 assets
RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG_NAME")
# 提取 assets 信息
ASSETS_URL=$(echo "$RESPONSE" | jq -r '.assets_url')
# 获取所有 assets 并删除旧的 .apk 文件(保留最新的)
if [ "$ASSETS_URL" != "null" ]; then
# 获取所有 APK 资产,按创建时间排序(最新的在前)
ASSETS_RESPONSE=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$ASSETS_URL")
echo "$ASSETS_RESPONSE" | jq -r 'sort_by(.created_at) | reverse | .[] | select(.name | endswith(".apk")) | .id' | tail -n +2 | while read -r asset_id; do
if [ -n "$asset_id" ]; then
echo "Deleting old APK asset ID: $asset_id"
curl -s -X DELETE -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/releases/assets/$asset_id"
fi
done
fi