test.yml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. latest_tag: ${{ steps.set-ver.outputs.last_tag }}
  35. is_release: ${{ steps.set-ver.outputs.is_release }}
  36. steps:
  37. - uses: actions/checkout@v4
  38. with:
  39. fetch-depth: 0
  40. - name: 计算版本号和构建号
  41. id: set-ver
  42. run: |
  43. PROPERTIES_FILE="./app/version.properties"
  44. if [[ ! -f "$PROPERTIES_FILE" ]]; then
  45. echo "错误:version.properties 文件不存在于 $PROPERTIES_FILE"
  46. exit 1
  47. fi
  48. # --- 1. 读取基础版本和发布标志 ---
  49. MAJOR=$(cat "$PROPERTIES_FILE" | grep 'VERSION_MAJOR' | cut -d'=' -f2 | tr -d '[:space:]')
  50. MINOR=$(cat "$PROPERTIES_FILE" | grep 'VERSION_MINOR' | cut -d'=' -f2 | tr -d '[:space:]')
  51. PATCH=$(cat "$PROPERTIES_FILE" | grep 'VERSION_PATCH' | cut -d'=' -f2 | tr -d '[:space:]')
  52. RELEASE_FLAG=$(cat "$PROPERTIES_FILE" | grep 'VERSION_SUFFIX' | cut -d'=' -f2 | tr -d '[:space:]')
  53. CURRENT_VERSION="$MAJOR.$MINOR.$PATCH"
  54. NEW_BUILD_NUM=0
  55. VERSION_SUFFIX_NAME=""
  56. IS_RELEASE="false" # 默认为 Pre-release
  57. # 2. 确定版本类型和后缀
  58. if [[ "${RELEASE_FLAG}" == "0" ]]; then
  59. # 标记为正式版 (VERSION_SUFFIX=0)
  60. VERSION_SUFFIX_NAME=""
  61. IS_RELEASE="true"
  62. echo "版本类型: 正式版 (Release), 版本名: $CURRENT_VERSION"
  63. # 找到最近的正式版本 tag 作为日志起点
  64. LAST_TAG_FOR_LOG=$(git tag --list "$MAJOR.$MINOR.*" --sort=-committerdate | grep -v 'beta' | head -n 2 | tail -n 1)
  65. if [ -z "$LAST_TAG_FOR_LOG" ]; then
  66. # 如果找不到上一个正式版,则从最近的非本次构建的 tag 开始
  67. LAST_TAG_FOR_LOG=$(git tag --sort=-committerdate | head -n 2 | tail -n 1)
  68. fi
  69. elif [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
  70. # 标记为测试版 (VERSION_SUFFIX=1 或其他/空),且在 main 分支推送
  71. # 查找匹配当前 CURRENT_VERSION 的上一个 BETA TAG (e.g., 3.26.4-beta.X)
  72. LAST_MATCHING_TAG=$(git tag --list "$CURRENT_VERSION-beta.*" --sort=-committerdate | head -n 1)
  73. if [[ $LAST_MATCHING_TAG =~ [0-9]+\.[0-9]+\.[0-9]+-beta\.([0-9]+) ]]; then
  74. LAST_BUILD_NUM="${BASH_REMATCH[1]}"
  75. NEW_BUILD_NUM=$((LAST_BUILD_NUM + 1))
  76. else
  77. NEW_BUILD_NUM=1
  78. fi
  79. VERSION_SUFFIX_NAME="-beta.$NEW_BUILD_NUM"
  80. # 日志从最近的任意 TAG 开始 (包括上一个 beta.x tag)
  81. LAST_ANY_TAG=$(git tag --sort=-committerdate | head -n 1)
  82. LAST_TAG_FOR_LOG="${LAST_ANY_TAG#v}"
  83. echo "版本类型: 预发布版 (Pre-release), 构建号: $NEW_BUILD_NUM"
  84. else
  85. # 其它情况 (PR, 非 main 分支推送等),构建号设为 0
  86. NEW_BUILD_NUM=0
  87. LAST_ANY_TAG=$(git tag --sort=-committerdate | head -n 1)
  88. LAST_TAG_FOR_LOG="${LAST_ANY_TAG#v}"
  89. echo "非 main 分支推送/PR,构建号设置为 $NEW_BUILD_NUM"
  90. fi
  91. # 3. 构造最终版本信息
  92. FULL_VERSION_NAME="${CURRENT_VERSION}${VERSION_SUFFIX_NAME}"
  93. # 4. 设置输出供后续作业使用
  94. echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
  95. echo "full_version_name=$FULL_VERSION_NAME" >> $GITHUB_OUTPUT
  96. echo "build_number=$NEW_BUILD_NUM" >> $GITHUB_OUTPUT
  97. echo "last_tag=$LAST_TAG_FOR_LOG" >> $GITHUB_OUTPUT
  98. echo "is_release=$IS_RELEASE" >> $GITHUB_OUTPUT # 新的输出标志
  99. build:
  100. needs: prepare
  101. strategy:
  102. matrix:
  103. product: [ app ]
  104. type: [ release ]
  105. fail-fast: false
  106. runs-on: ubuntu-latest
  107. env:
  108. product: ${{ matrix.product }}
  109. type: ${{ matrix.type }}
  110. # 使用 prepare job 的输出
  111. VERSION: ${{ needs.prepare.outputs.version }}
  112. VERSIONL: ${{ needs.prepare.outputs.versionL }}
  113. BUILD_NUMBER: ${{ needs.prepare.outputs.build_number }}
  114. steps:
  115. - uses: actions/checkout@v5
  116. with:
  117. fetch-depth: 0
  118. - name: Set up JDK 17
  119. uses: actions/setup-java@v5
  120. with:
  121. distribution: 'temurin'
  122. java-version: 17
  123. - name: Clear 18PlusList.txt
  124. run: |
  125. echo "清空18PlusList.txt"
  126. echo "">$GITHUB_WORKSPACE/app/src/main/assets/18PlusList.txt
  127. - name: Release Apk Sign
  128. run: |
  129. echo "${{ secrets.SIGNING_KEY }}" | base64 -d > $GITHUB_WORKSPACE/app/my-release-key.jks
  130. echo "RELEASE_STORE_FILE=my-release-key.jks" >> $GITHUB_WORKSPACE/gradle.properties
  131. echo "RELEASE_STORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
  132. echo "RELEASE_KEY_ALIAS=${{ secrets.KEY_ALIAS }}" >> $GITHUB_WORKSPACE/gradle.properties
  133. echo "RELEASE_KEY_PASSWORD=${{ secrets.KEY_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
  134. - name: Set up Gradle
  135. uses: gradle/actions/setup-gradle@v4
  136. - name: 统一版本号
  137. run: |
  138. VERSIONL="${{ env.VERSIONL }}"
  139. BUILD_NUMBER="${{ env.BUILD_NUMBER }}"
  140. echo "开始修改 build.gradle 中的版本号..."
  141. sed -i "s/ext.versionName = .*/ext.versionName = \"$VERSIONL\"/" $GITHUB_WORKSPACE/app/build.gradle
  142. - name: Build With Gradle
  143. run: |
  144. echo "开始${{ env.product }}${{ env.type }}构建"
  145. chmod +x gradlew
  146. ./gradlew assemble${{ env.product }}Release
  147. - name: Move Missing Rules Files
  148. run: |
  149. echo "移动missing_rules.txt文件"
  150. MAPPING_DIR="${{ github.workspace }}/mapping"
  151. mkdir -p "$MAPPING_DIR"
  152. # 使用 find 代替 ls,避免参数列表过长
  153. find ${{ github.workspace }}/app/build/outputs/mapping/ -name "missing_rules.txt" -exec mv {} "$MAPPING_DIR/missing_rules.txt" \;
  154. - name: Upload Missing Rules File To Artifact
  155. uses: actions/upload-artifact@v4
  156. with:
  157. name: legado.${{ env.product }}.${{ env.type }}.mapping.missing_rules
  158. if-no-files-found: ignore
  159. path: ${{ github.workspace }}/mapping/missing_rules.txt
  160. - name: Check Build production
  161. run: |
  162. echo "检查 APK 输出目录"
  163. APK_PATH="$GITHUB_WORKSPACE/app/build/outputs/apk"
  164. # 检查是否有任何 APK 文件生成
  165. if [ -z "$(find "$APK_PATH" -name "*.apk" -print -quit)" ]; then
  166. echo "Build production not found! Check gradle logs."
  167. exit 1
  168. fi
  169. - name: Upload universal APK
  170. uses: actions/upload-artifact@v4
  171. with:
  172. name: universal-apk
  173. path: app/build/outputs/apk/app/release/legado_app_*[0-9].apk
  174. - name: Upload arm64-v8a APK
  175. uses: actions/upload-artifact@v4
  176. with:
  177. name: arm64-v8a-apk
  178. path: app/build/outputs/apk/app/release/*arm64-v8a.apk
  179. - name: Upload armeabi-v7a APK
  180. uses: actions/upload-artifact@v4
  181. with:
  182. name: armeabi-v7a-apk
  183. path: app/build/outputs/apk/app/release/*armeabi-v7a.apk
  184. - name: Upload Mapping File To Artifact
  185. uses: actions/upload-artifact@v4
  186. with:
  187. name: legado.${{ env.product }}.${{ env.type }}.mapping
  188. if-no-files-found: ignore
  189. path: ${{ github.workspace }}/mapping/mapping.txt
  190. test_Branch:
  191. needs: [ prepare, build ]
  192. runs-on: ubuntu-latest
  193. if: ${{ github.event_name != 'pull_request' && github.actor == 'gedoor' }}
  194. steps:
  195. - uses: actions/checkout@v5
  196. - uses: actions/download-artifact@v5
  197. with:
  198. path: apk/
  199. - working-directory: apk/
  200. run: |
  201. # 使用 find 安全地移动文件
  202. find . -type f -name "*.apk" -exec mv {} . \;
  203. # 清理子目录
  204. find . -type d -mindepth 1 -exec rm -rf {} +
  205. - name: Push To "test" Branch
  206. run: |
  207. cd $GITHUB_WORKSPACE/apk/
  208. git init
  209. git checkout -b test
  210. git config --global user.name "github-actions[bot]"
  211. git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
  212. git remote add origin "https://${{ github.actor }}:${{ secrets.ACTIONS_TOKEN }}@github.com/${{ github.actor }}/release"
  213. git add *.apk
  214. git commit -m "${{ needs.prepare.outputs.versionL }}"
  215. git push -f -u origin test
  216. create_release:
  217. needs: [ prepare, build ]
  218. # 触发条件:只有在 main 分支推送,且构建号大于 0(针对 beta 版) 或 明确标记为正式版时才执行
  219. if: |
  220. github.event_name == 'push' &&
  221. github.ref == 'refs/heads/main' &&
  222. (needs.prepare.outputs.build_number > 0 || needs.prepare.outputs.is_release == 'true') &&
  223. success()
  224. runs-on: ubuntu-latest
  225. steps:
  226. - uses: actions/checkout@v4
  227. with:
  228. fetch-depth: 0
  229. #
  230. # 获取 Commit 记录并格式化为 Release Body
  231. #
  232. - name: 获取 Commit 记录
  233. id: get_commits
  234. run: |
  235. # prepare Job 中计算出的日志起点 Tag (LAST_TAG_FOR_LOG)
  236. LAST_TAG="${{ needs.prepare.outputs.latest_tag }}"
  237. # 获取从上一个 Tag 到当前 HEAD 的所有 Commit 消息
  238. # --no-merges: 排除合并提交
  239. COMMIT_LOG=$(git log "$LAST_TAG"..HEAD --pretty=format:"* %s" --no-merges)
  240. if [ -z "$COMMIT_LOG" ]; then
  241. RELEASE_BODY=$(cat <<EOF
  242. ### 更新内容
  243. - 没有新的 Commit 记录.
  244. EOF
  245. )
  246. else
  247. RELEASE_BODY=$(cat <<EOF
  248. ### 更新内容
  249. $COMMIT_LOG
  250. EOF
  251. )
  252. fi
  253. # 设置 step 输出
  254. echo "release_body<<EOF" >> $GITHUB_OUTPUT
  255. echo "$RELEASE_BODY" >> $GITHUB_OUTPUT
  256. echo "EOF" >> $GITHUB_OUTPUT
  257. echo "生成的 Release Body:"
  258. echo "$RELEASE_BODY"
  259. - name: 创建 Tag
  260. run: |
  261. # 检查 Tag 是否已存在,避免重复创建
  262. if ! git tag -l ${{ needs.prepare.outputs.versionL }} | grep -q ${{ needs.prepare.outputs.versionL }}; then
  263. git tag ${{ needs.prepare.outputs.versionL }}
  264. git push origin ${{ needs.prepare.outputs.versionL }}
  265. else
  266. echo "Tag ${{ needs.prepare.outputs.versionL }} 已存在,跳过创建。"
  267. fi
  268. env:
  269. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  270. - name: 创建 GitHub Release
  271. id: create_release
  272. uses: actions/create-release@v1
  273. env:
  274. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  275. with:
  276. tag_name: ${{ needs.prepare.outputs.versionL }}
  277. release_name: |
  278. ${{ needs.prepare.outputs.is_release == 'true' && format('Release {0}', needs.prepare.outputs.versionL) || format('Pre-release {0}', needs.prepare.outputs.versionL) }}
  279. body: ${{ steps.get_commits.outputs.release_body }} # <--- 使用新步骤的输出
  280. draft: false
  281. # 根据 prepare 任务的输出设置 prerelease 标志
  282. prerelease: ${{ needs.prepare.outputs.is_release == 'true' && 'false' || 'true' }}
  283. - name: 下载所有 APK Artifacts
  284. uses: actions/download-artifact@v4
  285. with:
  286. pattern: '*-apk'
  287. path: artifacts
  288. merge-multiple: true
  289. - name: 整理并重命名 APK 文件
  290. id: organize_apks
  291. run: |
  292. VERSIONL="${{ needs.prepare.outputs.versionL }}"
  293. echo "正在查找并重命名 APK 文件..."
  294. # 查找 Universal APK (文件名不含 ABI)
  295. find artifacts -maxdepth 1 -type f -name '*app_*.apk' \
  296. ! -name '*arm64-v8a.apk' \
  297. ! -name '*armeabi-v7a.apk' \
  298. -exec mv {} "artifacts/legado-${VERSIONL}.apk" \;
  299. # 查找 arm64-v8a APK
  300. find artifacts -maxdepth 1 -type f -name '*arm64-v8a.apk' \
  301. -exec mv {} "artifacts/legado-${VERSIONL}-arm64-v8a.apk" \;
  302. # 查找 armeabi-v7a APK
  303. find artifacts -maxdepth 1 -type f -name '*armeabi-v7a.apk' \
  304. -exec mv {} "artifacts/legado-${VERSIONL}-armeabi-v7a.apk" \;
  305. # 检查文件是否成功找到并重命名,如果找不到则退出
  306. if [ ! -f "artifacts/legado-${VERSIONL}.apk" ]; then
  307. echo "错误:未找到 Universal APK。请检查 build 作业的命名规则。"
  308. # 允许继续,但发出警告,因为其他 ABI 仍可能存在
  309. # exit 1 # 移除硬性退出,以防万一
  310. fi
  311. echo "文件重命名完成,准备上传:"
  312. ls -l artifacts/
  313. - name: 上传 Universal APK
  314. uses: actions/upload-release-asset@v1
  315. env:
  316. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  317. with:
  318. upload_url: ${{ steps.create_release.outputs.upload_url }}
  319. asset_path: artifacts/legado-${{ needs.prepare.outputs.versionL }}.apk
  320. asset_name: legado-${{ needs.prepare.outputs.versionL }}.apk
  321. asset_content_type: application/vnd.android.package-archive # APK MIME 类型
  322. - name: 上传 arm64-v8a APK
  323. uses: actions/upload-release-asset@v1
  324. env:
  325. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  326. with:
  327. upload_url: ${{ steps.create_release.outputs.upload_url }}
  328. asset_path: artifacts/legado-${{ needs.prepare.outputs.versionL }}-arm64-v8a.apk
  329. asset_name: legado-${{ needs.prepare.outputs.versionL }}-arm64-v8a.apk
  330. asset_content_type: application/vnd.android.package-archive
  331. - name: 上传 armeabi-v7a APK
  332. uses: actions/upload-release-asset@v1
  333. env:
  334. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  335. with:
  336. upload_url: ${{ steps.create_release.outputs.upload_url }}
  337. asset_path: artifacts/legado-${{ needs.prepare.outputs.versionL }}-armeabi-v7a.apk
  338. asset_name: legado-${{ needs.prepare.outputs.versionL }}-armeabi-v7a.apk
  339. asset_content_type: application/vnd.android.package-archive