test.yml 14 KB

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