代码优化
This commit is contained in:
@@ -0,0 +1,346 @@
|
||||
name: Auto Release
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
paths:
|
||||
- '**'
|
||||
# - '!**/assets/**'
|
||||
# - '!**.md'
|
||||
- '!**/ISSUE_TEMPLATE/**'
|
||||
- '!**/modules/web/**'
|
||||
pull_request:
|
||||
paths-ignore:
|
||||
- '**/modules/web/**'
|
||||
workflow_run:
|
||||
workflows: [Build Web]
|
||||
branches: [master]
|
||||
types:
|
||||
- completed
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.workflow }}-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
|
||||
prepare:
|
||||
runs-on: ubuntu-latest
|
||||
if: ${{ !startsWith(github.event.head_commit.message, 'Merge pull request') }}
|
||||
outputs:
|
||||
versionL: ${{ steps.set-ver.outputs.full_version_name }}
|
||||
version: ${{ steps.set-ver.outputs.current_version }}
|
||||
build_number: ${{ steps.set-ver.outputs.build_number }}
|
||||
commit_number: ${{ steps.set-ver.outputs.commit_number }}
|
||||
latest_tag: ${{ steps.set-ver.outputs.last_tag }}
|
||||
is_release: ${{ steps.set-ver.outputs.is_release }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: 计算版本号和构建号
|
||||
id: set-ver
|
||||
run: |
|
||||
COMMIT_COUNT=$(git rev-list --count HEAD)
|
||||
echo "Commit 总数 (Build Number): $COMMIT_COUNT"
|
||||
|
||||
PROPERTIES_FILE="./app/version.properties"
|
||||
if [[ ! -f "$PROPERTIES_FILE" ]]; then
|
||||
echo "错误:version.properties 文件不存在于 $PROPERTIES_FILE"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --- 1. 读取基础版本和发布标志 ---
|
||||
MAJOR=$(cat "$PROPERTIES_FILE" | grep 'VERSION_MAJOR' | cut -d'=' -f2 | tr -d '[:space:]')
|
||||
MINOR=$(cat "$PROPERTIES_FILE" | grep 'VERSION_MINOR' | cut -d'=' -f2 | tr -d '[:space:]')
|
||||
PATCH=$(cat "$PROPERTIES_FILE" | grep 'VERSION_PATCH' | cut -d'=' -f2 | tr -d '[:space:]')
|
||||
RELEASE_FLAG=$(cat "$PROPERTIES_FILE" | grep 'VERSION_SUFFIX' | cut -d'=' -f2 | tr -d '[:space:]')
|
||||
|
||||
CURRENT_VERSION="$MAJOR.$MINOR.$PATCH"
|
||||
NEW_BUILD_NUM=0
|
||||
VERSION_SUFFIX_NAME=""
|
||||
IS_RELEASE="false" # 默认为 Pre-release
|
||||
|
||||
# 2. 确定版本类型和后缀
|
||||
if [[ "${RELEASE_FLAG}" == "0" ]]; then
|
||||
# 标记为正式版 (VERSION_SUFFIX=0)
|
||||
VERSION_SUFFIX_NAME=""
|
||||
IS_RELEASE="true"
|
||||
echo "版本类型: 正式版 (Release), 版本名: $CURRENT_VERSION"
|
||||
|
||||
# 找到最近的正式版本 tag 作为日志起点
|
||||
LAST_TAG_FOR_LOG=$(git tag --list "$MAJOR.$MINOR.*" --sort=-committerdate | grep -v 'beta' | head -n 2 | tail -n 1)
|
||||
if [ -z "$LAST_TAG_FOR_LOG" ]; then
|
||||
# 如果找不到上一个正式版,则从最近的非本次构建的 tag 开始
|
||||
LAST_TAG_FOR_LOG=$(git tag --sort=-committerdate | head -n 2 | tail -n 1)
|
||||
fi
|
||||
|
||||
elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
|
||||
# 标记为测试版 (VERSION_SUFFIX=1 或其他/空),且在 main 分支推送
|
||||
|
||||
# 查找匹配当前 CURRENT_VERSION 的上一个 BETA TAG (e.g., 3.26.4-beta.X)
|
||||
LAST_MATCHING_TAG=$(git tag --list "$CURRENT_VERSION-beta.*" --sort=-committerdate | head -n 1)
|
||||
|
||||
if [[ $LAST_MATCHING_TAG =~ [0-9]+\.[0-9]+\.[0-9]+-beta\.([0-9]+) ]]; then
|
||||
LAST_BUILD_NUM="${BASH_REMATCH[1]}"
|
||||
NEW_BUILD_NUM=$((LAST_BUILD_NUM + 1))
|
||||
else
|
||||
NEW_BUILD_NUM=1
|
||||
fi
|
||||
|
||||
VERSION_SUFFIX_NAME="-beta.$NEW_BUILD_NUM"
|
||||
|
||||
# 日志从最近的任意 TAG 开始 (包括上一个 beta.x tag)
|
||||
LAST_ANY_TAG=$(git tag --sort=-committerdate | head -n 1)
|
||||
LAST_TAG_FOR_LOG="${LAST_ANY_TAG#v}"
|
||||
echo "版本类型: 预发布版 (Pre-release), 构建号: $NEW_BUILD_NUM"
|
||||
|
||||
else
|
||||
# 其它情况 (PR, 非 main 分支推送等),构建号设为 0
|
||||
NEW_BUILD_NUM=0
|
||||
LAST_ANY_TAG=$(git tag --sort=-committerdate | head -n 1)
|
||||
LAST_TAG_FOR_LOG="${LAST_ANY_TAG#v}"
|
||||
echo "非 main 分支推送/PR,构建号设置为 $NEW_BUILD_NUM"
|
||||
fi
|
||||
|
||||
# 3. 构造最终版本信息
|
||||
FULL_VERSION_NAME="${CURRENT_VERSION}${VERSION_SUFFIX_NAME}"
|
||||
|
||||
# 4. 设置输出供后续作业使用
|
||||
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
|
||||
echo "full_version_name=$FULL_VERSION_NAME" >> $GITHUB_OUTPUT
|
||||
echo "build_number=$NEW_BUILD_NUM" >> $GITHUB_OUTPUT
|
||||
echo "commit_number=$COMMIT_COUNT" >> $GITHUB_OUTPUT
|
||||
echo "last_tag=$LAST_TAG_FOR_LOG" >> $GITHUB_OUTPUT
|
||||
echo "is_release=$IS_RELEASE" >> $GITHUB_OUTPUT # 新s的输出标志
|
||||
|
||||
build:
|
||||
needs: prepare
|
||||
strategy:
|
||||
matrix:
|
||||
product: [ app ]
|
||||
# 如果是正式版,只构建 release;如果是测试版,构建 release 和 noR8
|
||||
type: ${{ (needs.prepare.outputs.is_release == 'true') && fromJSON('["release"]') || fromJSON('["release", "noR8"]') }}
|
||||
fail-fast: false
|
||||
runs-on: ubuntu-latest
|
||||
env:
|
||||
product: ${{ matrix.product }}
|
||||
type: ${{ matrix.type }}
|
||||
# 使用 prepare job 的输出
|
||||
VERSION: ${{ needs.prepare.outputs.version }}
|
||||
VERSIONL: ${{ needs.prepare.outputs.versionL }}
|
||||
COMMIT_NUMBER_VAL: ${{ needs.prepare.outputs.commit_number }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v5
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up JDK 17
|
||||
uses: actions/setup-java@v5
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: 17
|
||||
- name: Clear 18PlusList.txt
|
||||
run: |
|
||||
echo "清空18PlusList.txt"
|
||||
echo "">$GITHUB_WORKSPACE/app/src/main/assets/18PlusList.txt
|
||||
- name: Release Apk Sign
|
||||
# 仅在所有者的 PR 或 push 时进行签名
|
||||
if: ${{ github.actor == 'HapeLee' }}
|
||||
run: |
|
||||
echo "${{ secrets.SIGNING_KEY }}" | base64 -d > $GITHUB_WORKSPACE/app/my-release-key.jks
|
||||
echo "RELEASE_STORE_FILE=my-release-key.jks" >> $GITHUB_WORKSPACE/gradle.properties
|
||||
echo "RELEASE_STORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
|
||||
echo "RELEASE_KEY_ALIAS=${{ secrets.KEY_ALIAS }}" >> $GITHUB_WORKSPACE/gradle.properties
|
||||
echo "RELEASE_KEY_PASSWORD=${{ secrets.KEY_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
|
||||
|
||||
- name: Set up Gradle
|
||||
uses: gradle/actions/setup-gradle@v4
|
||||
|
||||
|
||||
- name: Build With Gradle
|
||||
run: |
|
||||
echo "开始${{ env.product }}${{ env.type }}构建"
|
||||
echo "VersionName: $APP_VERSION_NAME"
|
||||
echo "VersionCode (Commit Number): $COMMIT_NUMBER"
|
||||
chmod +x gradlew
|
||||
# 将 type 首字母大写以匹配 Gradle Task
|
||||
TYPE_CAP=$(echo ${{ env.type }} | awk '{print toupper(substr($0,1,1))substr($0,2)}')
|
||||
./gradlew assemble${{ env.product }}${TYPE_CAP}
|
||||
env:
|
||||
COMMIT_NUMBER: ${{ env.COMMIT_NUMBER_VAL }}
|
||||
APP_VERSION_NAME: ${{ env.VERSIONL }}
|
||||
|
||||
- name: Move Missing Rules Files
|
||||
if: matrix.type == 'release'
|
||||
run: |
|
||||
echo "移动missing_rules.txt文件"
|
||||
MAPPING_DIR="${{ github.workspace }}/mapping"
|
||||
mkdir -p "$MAPPING_DIR"
|
||||
# 使用 find 代替 ls,避免参数列表过长
|
||||
find ${{ github.workspace }}/app/build/outputs/mapping/ -name "missing_rules.txt" -exec mv {} "$MAPPING_DIR/missing_rules.txt" \;
|
||||
|
||||
- name: Upload Missing Rules File To Artifact
|
||||
if: matrix.type == 'release'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: legado.${{ env.product }}.${{ env.type }}.mapping.missing_rules
|
||||
if-no-files-found: ignore
|
||||
path: ${{ github.workspace }}/mapping/missing_rules.txt
|
||||
|
||||
- name: Check Build production
|
||||
run: |
|
||||
echo "检查 APK 输出目录"
|
||||
APK_PATH="$GITHUB_WORKSPACE/app/build/outputs/apk/${{ env.product }}/${{ env.type }}"
|
||||
# 检查是否有任何 APK 文件生成
|
||||
if [ -z "$(find "$APK_PATH" -name "*.apk" -print -quit)" ]; then
|
||||
echo "Build production not found! Check gradle logs."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
- name: Upload universal APK
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: universal-apk-${{ env.type }}
|
||||
if-no-files-found: error
|
||||
path: app/build/outputs/apk/${{ env.product }}/${{ env.type }}/*universal*.apk
|
||||
|
||||
- name: Upload arm64-v8a APK
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: arm64-v8a-apk-${{ env.type }}
|
||||
if-no-files-found: error
|
||||
path: app/build/outputs/apk/${{ env.product }}/${{ env.type }}/*arm64-v8a*.apk
|
||||
|
||||
- name: Upload armeabi-v7a APK
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: armeabi-v7a-apk-${{ env.type }}
|
||||
if-no-files-found: error
|
||||
path: app/build/outputs/apk/${{ env.product }}/${{ env.type }}/*armeabi-v7a*.apk
|
||||
|
||||
- name: Upload Mapping File To Artifact
|
||||
if: matrix.type == 'release'
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: legado.${{ env.product }}.${{ env.type }}.mapping
|
||||
if-no-files-found: ignore
|
||||
path: ${{ github.workspace }}/mapping/mapping.txt
|
||||
|
||||
create_release:
|
||||
needs: [ prepare, build ]
|
||||
# 仅在所有者的 push 到 main 分支时执行发布
|
||||
if: |
|
||||
github.event_name == 'push' &&
|
||||
github.ref == 'refs/heads/main' &&
|
||||
github.actor == 'HapeLee' &&
|
||||
(needs.prepare.outputs.build_number > 0 || needs.prepare.outputs.is_release == 'true') &&
|
||||
success()
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
# 1. 下载 build 任务上传的所有 APK
|
||||
- name: 下载所有 APK Artifacts
|
||||
uses: actions/download-artifact@v4
|
||||
with:
|
||||
pattern: '*-apk-*'
|
||||
path: artifacts
|
||||
merge-multiple: true
|
||||
if-no-artifact-found: error
|
||||
|
||||
# 2. 重命名逻辑
|
||||
- name: 整理并重命名 APK 文件
|
||||
id: organize_apks
|
||||
run: |
|
||||
VERSIONL="${{ needs.prepare.outputs.versionL }}"
|
||||
echo "正在整理 APK 文件..."
|
||||
if [ ! -d artifacts ]; then
|
||||
echo "::error::artifacts 目录不存在,说明上一步没有下载到 APK。"
|
||||
exit 1
|
||||
fi
|
||||
cd artifacts
|
||||
|
||||
shopt -s nullglob
|
||||
for f in *.apk; do
|
||||
out="legado-${VERSIONL}"
|
||||
if [[ "$f" == *noR8* || "$f" == *nor8* ]]; then
|
||||
out="${out}-noR8"
|
||||
fi
|
||||
|
||||
if [[ "$f" == *arm64-v8a* ]]; then
|
||||
out="${out}-arm64-v8a"
|
||||
elif [[ "$f" == *armeabi-v7a* ]]; then
|
||||
out="${out}-armeabi-v7a"
|
||||
fi
|
||||
|
||||
out="${out}.apk"
|
||||
mv "$f" "$out"
|
||||
done
|
||||
|
||||
ls -l
|
||||
|
||||
#
|
||||
# 获取 Commit 记录并格式化为 Release Body
|
||||
#
|
||||
- name: 获取 Commit 记录
|
||||
id: get_commits
|
||||
run: |
|
||||
LAST_TAG="${{ needs.prepare.outputs.latest_tag }}"
|
||||
IS_RELEASE="${{ needs.prepare.outputs.is_release }}"
|
||||
|
||||
COMMIT_LOG=$(git log "$LAST_TAG"..HEAD --pretty=format:"* %s" --no-merges)
|
||||
|
||||
if [ -z "$COMMIT_LOG" ]; then
|
||||
CONTENT="- 没有新的 Commit 记录."
|
||||
else
|
||||
CONTENT="$COMMIT_LOG"
|
||||
fi
|
||||
|
||||
{
|
||||
echo "release_body<<EOF"
|
||||
if [[ "$IS_RELEASE" != "true" ]]; then
|
||||
echo "**此版本为测试版,会存在不稳定问题。**"
|
||||
echo "**noR8 变体关闭了代码混淆与压缩,若有崩溃问题请使用它并收集和发送 Log,以便开发者定位问题代码。**"
|
||||
echo ""
|
||||
fi
|
||||
echo "### 更新内容"
|
||||
echo ""
|
||||
echo "$CONTENT"
|
||||
echo "EOF"
|
||||
} >> $GITHUB_OUTPUT
|
||||
|
||||
echo "Release Body 已生成。"
|
||||
|
||||
- name: 创建 Tag
|
||||
run: |
|
||||
# 检查 Tag 是否已存在,避免重复创建
|
||||
if ! git tag -l ${{ needs.prepare.outputs.versionL }} | grep -q ${{ needs.prepare.outputs.versionL }}; then
|
||||
git tag ${{ needs.prepare.outputs.versionL }}
|
||||
git push origin ${{ needs.prepare.outputs.versionL }}
|
||||
else
|
||||
echo "Tag ${{ needs.prepare.outputs.versionL }} 已存在,跳过创建。"
|
||||
fi
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
# 4. 创建 Release 并上传
|
||||
- name: 创建 GitHub Release 并上传文件
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
tag_name: ${{ needs.prepare.outputs.versionL }}
|
||||
name: ${{ needs.prepare.outputs.is_release == 'true' && format('Release {0}', needs.prepare.outputs.versionL) || format('Pre-release {0}', needs.prepare.outputs.versionL) }}
|
||||
body: ${{ steps.get_commits.outputs.release_body }}
|
||||
prerelease: ${{ needs.prepare.outputs.is_release != 'true' }}
|
||||
files: |
|
||||
artifacts/legado-${{ needs.prepare.outputs.versionL }}*.apk
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
Reference in New Issue
Block a user