test.yml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. name: Test Build
  2. on:
  3. push:
  4. branches:
  5. - main
  6. paths:
  7. - '**'
  8. # - '!**/assets/**'
  9. # - '!**.md'
  10. - '!**/ISSUE_TEMPLATE/**'
  11. - '!**/modules/web/**'
  12. pull_request:
  13. paths-ignore:
  14. - '**/modules/web/**'
  15. workflow_run:
  16. workflows: [Build Web]
  17. branches: [master]
  18. types:
  19. - completed
  20. workflow_dispatch:
  21. permissions:
  22. contents: write
  23. concurrency:
  24. group: ${{ github.workflow }}-${{ github.ref }}
  25. cancel-in-progress: true
  26. jobs:
  27. prepare:
  28. runs-on: ubuntu-latest
  29. if: ${{ !startsWith(github.event.head_commit.message, 'Merge pull request') }}
  30. outputs:
  31. versionL: ${{ steps.set-ver.outputs.full_version_name }}
  32. version: ${{ steps.set-ver.outputs.current_version }}
  33. build_number: ${{ steps.set-ver.outputs.build_number }}
  34. commit_number: ${{ steps.set-ver.outputs.commit_number }}
  35. latest_tag: ${{ steps.set-ver.outputs.last_tag }}
  36. is_release: ${{ steps.set-ver.outputs.is_release }}
  37. steps:
  38. - uses: actions/checkout@v4
  39. with:
  40. fetch-depth: 0
  41. - name: 计算版本号和构建号
  42. id: set-ver
  43. run: |
  44. COMMIT_COUNT=$(git rev-list --count HEAD)
  45. echo "Commit 总数 (Build Number): $COMMIT_COUNT"
  46. PROPERTIES_FILE="./app/version.properties"
  47. if [[ ! -f "$PROPERTIES_FILE" ]]; then
  48. echo "错误:version.properties 文件不存在于 $PROPERTIES_FILE"
  49. exit 1
  50. fi
  51. # --- 1. 读取基础版本和发布标志 ---
  52. MAJOR=$(cat "$PROPERTIES_FILE" | grep 'VERSION_MAJOR' | cut -d'=' -f2 | tr -d '[:space:]')
  53. MINOR=$(cat "$PROPERTIES_FILE" | grep 'VERSION_MINOR' | cut -d'=' -f2 | tr -d '[:space:]')
  54. PATCH=$(cat "$PROPERTIES_FILE" | grep 'VERSION_PATCH' | cut -d'=' -f2 | tr -d '[:space:]')
  55. RELEASE_FLAG=$(cat "$PROPERTIES_FILE" | grep 'VERSION_SUFFIX' | cut -d'=' -f2 | tr -d '[:space:]')
  56. CURRENT_VERSION="$MAJOR.$MINOR.$PATCH"
  57. NEW_BUILD_NUM=0
  58. VERSION_SUFFIX_NAME=""
  59. IS_RELEASE="false" # 默认为 Pre-release
  60. # 2. 确定版本类型和后缀
  61. if [[ "${RELEASE_FLAG}" == "0" ]]; then
  62. # 标记为正式版 (VERSION_SUFFIX=0)
  63. VERSION_SUFFIX_NAME=""
  64. IS_RELEASE="true"
  65. echo "版本类型: 正式版 (Release), 版本名: $CURRENT_VERSION"
  66. # 找到最近的正式版本 tag 作为日志起点
  67. LAST_TAG_FOR_LOG=$(git tag --list "$MAJOR.$MINOR.*" --sort=-committerdate | grep -v 'beta' | head -n 2 | tail -n 1)
  68. if [ -z "$LAST_TAG_FOR_LOG" ]; then
  69. # 如果找不到上一个正式版,则从最近的非本次构建的 tag 开始
  70. LAST_TAG_FOR_LOG=$(git tag --sort=-committerdate | head -n 2 | tail -n 1)
  71. fi
  72. elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
  73. # 标记为测试版 (VERSION_SUFFIX=1 或其他/空),且在 main 分支推送
  74. # 查找匹配当前 CURRENT_VERSION 的上一个 BETA TAG (e.g., 3.26.4-beta.X)
  75. LAST_MATCHING_TAG=$(git tag --list "$CURRENT_VERSION-beta.*" --sort=-committerdate | head -n 1)
  76. if [[ $LAST_MATCHING_TAG =~ [0-9]+\.[0-9]+\.[0-9]+-beta\.([0-9]+) ]]; then
  77. LAST_BUILD_NUM="${BASH_REMATCH[1]}"
  78. NEW_BUILD_NUM=$((LAST_BUILD_NUM + 1))
  79. else
  80. NEW_BUILD_NUM=1
  81. fi
  82. VERSION_SUFFIX_NAME="-beta.$NEW_BUILD_NUM"
  83. # 日志从最近的任意 TAG 开始 (包括上一个 beta.x tag)
  84. LAST_ANY_TAG=$(git tag --sort=-committerdate | head -n 1)
  85. LAST_TAG_FOR_LOG="${LAST_ANY_TAG#v}"
  86. echo "版本类型: 预发布版 (Pre-release), 构建号: $NEW_BUILD_NUM"
  87. else
  88. # 其它情况 (PR, 非 main 分支推送等),构建号设为 0
  89. NEW_BUILD_NUM=0
  90. LAST_ANY_TAG=$(git tag --sort=-committerdate | head -n 1)
  91. LAST_TAG_FOR_LOG="${LAST_ANY_TAG#v}"
  92. echo "非 main 分支推送/PR,构建号设置为 $NEW_BUILD_NUM"
  93. fi
  94. # 3. 构造最终版本信息
  95. FULL_VERSION_NAME="${CURRENT_VERSION}${VERSION_SUFFIX_NAME}"
  96. # 4. 设置输出供后续作业使用
  97. echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
  98. echo "full_version_name=$FULL_VERSION_NAME" >> $GITHUB_OUTPUT
  99. echo "build_number=$NEW_BUILD_NUM" >> $GITHUB_OUTPUT
  100. echo "commit_number=$COMMIT_COUNT" >> $GITHUB_OUTPUT
  101. echo "last_tag=$LAST_TAG_FOR_LOG" >> $GITHUB_OUTPUT
  102. echo "is_release=$IS_RELEASE" >> $GITHUB_OUTPUT # 新的输出标志
  103. build:
  104. needs: prepare
  105. strategy:
  106. matrix:
  107. product: [ app ]
  108. # 如果是正式版,只构建 release;如果是测试版,构建 release 和 noR8
  109. type: ${{ (needs.prepare.outputs.is_release == 'true') && fromJSON('["release"]') || fromJSON('["release", "noR8"]') }}
  110. fail-fast: false
  111. runs-on: ubuntu-latest
  112. env:
  113. product: ${{ matrix.product }}
  114. type: ${{ matrix.type }}
  115. # 使用 prepare job 的输出
  116. VERSION: ${{ needs.prepare.outputs.version }}
  117. VERSIONL: ${{ needs.prepare.outputs.versionL }}
  118. COMMIT_NUMBER_VAL: ${{ needs.prepare.outputs.commit_number }}
  119. steps:
  120. - uses: actions/checkout@v5
  121. with:
  122. fetch-depth: 0
  123. - name: Set up JDK 17
  124. uses: actions/setup-java@v5
  125. with:
  126. distribution: 'temurin'
  127. java-version: 17
  128. - name: Clear 18PlusList.txt
  129. run: |
  130. echo "清空18PlusList.txt"
  131. echo "">$GITHUB_WORKSPACE/app/src/main/assets/18PlusList.txt
  132. - name: Release Apk Sign
  133. # 仅在所有者的 PR 或 push 时进行签名
  134. if: ${{ github.actor == 'HapeLee' }}
  135. run: |
  136. echo "${{ secrets.SIGNING_KEY }}" | base64 -d > $GITHUB_WORKSPACE/app/my-release-key.jks
  137. echo "RELEASE_STORE_FILE=my-release-key.jks" >> $GITHUB_WORKSPACE/gradle.properties
  138. echo "RELEASE_STORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
  139. echo "RELEASE_KEY_ALIAS=${{ secrets.KEY_ALIAS }}" >> $GITHUB_WORKSPACE/gradle.properties
  140. echo "RELEASE_KEY_PASSWORD=${{ secrets.KEY_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
  141. - name: Set up Gradle
  142. uses: gradle/actions/setup-gradle@v4
  143. - name: Build With Gradle
  144. run: |
  145. echo "开始${{ env.product }}${{ env.type }}构建"
  146. echo "VersionName: $APP_VERSION_NAME"
  147. echo "VersionCode (Commit Number): $COMMIT_NUMBER"
  148. chmod +x gradlew
  149. # 将 type 首字母大写以匹配 Gradle Task
  150. TYPE_CAP=$(echo ${{ env.type }} | awk '{print toupper(substr($0,1,1))substr($0,2)}')
  151. ./gradlew assemble${{ env.product }}${TYPE_CAP}
  152. env:
  153. COMMIT_NUMBER: ${{ env.COMMIT_NUMBER_VAL }}
  154. APP_VERSION_NAME: ${{ env.VERSIONL }}
  155. - name: Move Missing Rules Files
  156. if: matrix.type == 'release'
  157. run: |
  158. echo "移动missing_rules.txt文件"
  159. MAPPING_DIR="${{ github.workspace }}/mapping"
  160. mkdir -p "$MAPPING_DIR"
  161. # 使用 find 代替 ls,避免参数列表过长
  162. find ${{ github.workspace }}/app/build/outputs/mapping/ -name "missing_rules.txt" -exec mv {} "$MAPPING_DIR/missing_rules.txt" \;
  163. - name: Upload Missing Rules File To Artifact
  164. if: matrix.type == 'release'
  165. uses: actions/upload-artifact@v4
  166. with:
  167. name: legado.${{ env.product }}.${{ env.type }}.mapping.missing_rules
  168. if-no-files-found: ignore
  169. path: ${{ github.workspace }}/mapping/missing_rules.txt
  170. - name: Check Build production
  171. run: |
  172. echo "检查 APK 输出目录"
  173. APK_PATH="$GITHUB_WORKSPACE/app/build/outputs/apk/${{ env.product }}/${{ env.type }}"
  174. # 检查是否有任何 APK 文件生成
  175. if [ -z "$(find "$APK_PATH" -name "*.apk" -print -quit)" ]; then
  176. echo "Build production not found! Check gradle logs."
  177. exit 1
  178. fi
  179. - name: Upload universal APK
  180. uses: actions/upload-artifact@v4
  181. with:
  182. name: universal-apk-${{ env.type }}
  183. path: app/build/outputs/apk/${{ env.product }}/${{ env.type }}/legado_${{ env.product }}_*[0-9]*.apk
  184. - name: Upload arm64-v8a APK
  185. uses: actions/upload-artifact@v4
  186. with:
  187. name: arm64-v8a-apk-${{ env.type }}
  188. path: app/build/outputs/apk/${{ env.product }}/${{ env.type }}/*arm64-v8a.apk
  189. - name: Upload armeabi-v7a APK
  190. uses: actions/upload-artifact@v4
  191. with:
  192. name: armeabi-v7a-apk-${{ env.type }}
  193. path: app/build/outputs/apk/${{ env.product }}/${{ env.type }}/*armeabi-v7a.apk
  194. - name: Upload Mapping File To Artifact
  195. if: matrix.type == 'release'
  196. uses: actions/upload-artifact@v4
  197. with:
  198. name: legado.${{ env.product }}.${{ env.type }}.mapping
  199. if-no-files-found: ignore
  200. path: ${{ github.workspace }}/mapping/mapping.txt
  201. test_Branch:
  202. needs: [ prepare, build ]
  203. runs-on: ubuntu-latest
  204. # 仅在所有者的非 PR 事件时执行(即所有者 push 到 main 分支时)
  205. if: ${{ github.event_name != 'pull_request' && github.actor == 'HapeLee' }}
  206. steps:
  207. - uses: actions/checkout@v5
  208. - uses: actions/download-artifact@v5
  209. with:
  210. pattern: '*-apk-release'
  211. path: apk/
  212. merge-multiple: true
  213. - working-directory: apk/
  214. run: |
  215. # 使用 find 安全地移动文件
  216. find . -type f -name "*.apk" -exec mv {} . \;
  217. # 清理子目录
  218. find . -type d -mindepth 1 -exec rm -rf {} +
  219. - name: Push To "test" Branch
  220. run: |
  221. cd $GITHUB_WORKSPACE/apk/
  222. git init
  223. git checkout -b test
  224. git config --global user.name "github-actions[bot]"
  225. git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
  226. git remote add origin "https://${{ github.actor }}:${{ secrets.ACTIONS_TOKEN }}@github.com/${{ github.actor }}/release"
  227. git add *.apk
  228. git commit -m "${{ needs.prepare.outputs.versionL }}"
  229. git push -f -u origin test
  230. create_release:
  231. needs: [ prepare, build ]
  232. # 仅在所有者的 push 到 main 分支时执行发布
  233. if: |
  234. github.event_name == 'push' &&
  235. github.ref == 'refs/heads/main' &&
  236. github.actor == 'HapeLee' &&
  237. (needs.prepare.outputs.build_number > 0 || needs.prepare.outputs.is_release == 'true') &&
  238. success()
  239. runs-on: ubuntu-latest
  240. steps:
  241. - uses: actions/checkout@v4
  242. with:
  243. fetch-depth: 0
  244. # 1. 下载 build 任务上传的所有 APK
  245. - name: 下载所有 APK Artifacts
  246. uses: actions/download-artifact@v4
  247. with:
  248. pattern: '*-apk-*'
  249. path: artifacts
  250. merge-multiple: true
  251. # 2. 重命名逻辑
  252. - name: 整理并重命名 APK 文件
  253. id: organize_apks
  254. run: |
  255. VERSIONL="${{ needs.prepare.outputs.versionL }}"
  256. echo "正在整理 APK 文件..."
  257. cd artifacts
  258. # 重命名 release 版本
  259. for f in *app_${VERSIONL}.apk; do [ -f "$f" ] && mv "$f" "legado-${VERSIONL}.apk"; done
  260. for f in *app_${VERSIONL}_arm64-v8a.apk; do [ -f "$f" ] && mv "$f" "legado-${VERSIONL}-arm64-v8a.apk"; done
  261. for f in *app_${VERSIONL}_armeabi-v7a.apk; do [ -f "$f" ] && mv "$f" "legado-${VERSIONL}-armeabi-v7a.apk"; done
  262. # 重命名 noR8 版本 (如果存在)
  263. for f in *app_${VERSIONL}-noR8.apk; do [ -f "$f" ] && mv "$f" "legado-${VERSIONL}-noR8.apk"; done
  264. for f in *app_${VERSIONL}-noR8_arm64-v8a.apk; do [ -f "$f" ] && mv "$f" "legado-${VERSIONL}-noR8-arm64-v8a.apk"; done
  265. for f in *app_${VERSIONL}-noR8_armeabi-v7a.apk; do [ -f "$f" ] && mv "$f" "legado-${VERSIONL}-noR8-armeabi-v7a.apk"; done
  266. ls -l
  267. #
  268. # 获取 Commit 记录并格式化为 Release Body
  269. #
  270. - name: 获取 Commit 记录
  271. id: get_commits
  272. run: |
  273. LAST_TAG="${{ needs.prepare.outputs.latest_tag }}"
  274. IS_RELEASE="${{ needs.prepare.outputs.is_release }}"
  275. COMMIT_LOG=$(git log "$LAST_TAG"..HEAD --pretty=format:"* %s" --no-merges)
  276. if [ -z "$COMMIT_LOG" ]; then
  277. CONTENT="- 没有新的 Commit 记录."
  278. else
  279. CONTENT="$COMMIT_LOG"
  280. fi
  281. {
  282. echo "release_body<<EOF"
  283. if [[ "$IS_RELEASE" != "true" ]]; then
  284. echo "**此版本为测试版,会存在不稳定问题。**"
  285. echo "**noR8 变体关闭了代码混淆与压缩,若有崩溃问题请使用它并收集和发送 Log,以便开发者定位问题代码。**"
  286. echo ""
  287. fi
  288. echo "### 更新内容"
  289. echo ""
  290. echo "$CONTENT"
  291. echo "EOF"
  292. } >> $GITHUB_OUTPUT
  293. echo "Release Body 已生成。"
  294. - name: 创建 Tag
  295. run: |
  296. # 检查 Tag 是否已存在,避免重复创建
  297. if ! git tag -l ${{ needs.prepare.outputs.versionL }} | grep -q ${{ needs.prepare.outputs.versionL }}; then
  298. git tag ${{ needs.prepare.outputs.versionL }}
  299. git push origin ${{ needs.prepare.outputs.versionL }}
  300. else
  301. echo "Tag ${{ needs.prepare.outputs.versionL }} 已存在,跳过创建。"
  302. fi
  303. env:
  304. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  305. # 4. 创建 Release 并上传
  306. - name: 创建 GitHub Release 并上传文件
  307. uses: softprops/action-gh-release@v2
  308. with:
  309. tag_name: ${{ needs.prepare.outputs.versionL }}
  310. name: ${{ needs.prepare.outputs.is_release == 'true' && format('Release {0}', needs.prepare.outputs.versionL) || format('Pre-release {0}', needs.prepare.outputs.versionL) }}
  311. body: ${{ steps.get_commits.outputs.release_body }}
  312. prerelease: ${{ needs.prepare.outputs.is_release != 'true' }}
  313. files: |
  314. artifacts/legado-${{ needs.prepare.outputs.versionL }}*.apk
  315. env:
  316. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}