test.yml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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: 完整版本号,例如 3.26.3-beta.1
  32. versionL: ${{ steps.set-ver.outputs.full_version_name }}
  33. # version: 基础版本号,例如 3.26.3
  34. version: ${{ steps.set-ver.outputs.current_version }}
  35. # build_number: 增量构建号,例如 1
  36. build_number: ${{ steps.set-ver.outputs.build_number }}
  37. # latest_tag: 最近一个用于版本递增的 tag,例如 v3.26.2-beta.5
  38. latest_tag: ${{ steps.set-ver.outputs.last_tag }}
  39. steps:
  40. - uses: actions/checkout@v4
  41. with:
  42. # 确保拉取所有标签,用于版本号计算
  43. fetch-depth: 0
  44. - name: 计算版本号和构建号
  45. id: set-ver
  46. run: |
  47. # 1. 读取 version.properties 文件获取基础版本号
  48. MAJOR=$(grep 'VERSION_MAJOR' version.properties | cut -d'=' -f2)
  49. MINOR=$(grep 'VERSION_MINOR' version.properties | cut -d'=' -f2)
  50. PATCH=$(grep 'VERSION_PATCH' version.properties | cut -d'=' -f2)
  51. CURRENT_VERSION="$MAJOR.$MINOR.$PATCH"
  52. # 2. 尝试获取最近一次成功的构建号 (beta build number)
  53. # 查找最近的、格式为 vX.Y.Z-beta.N 的标签,且 X.Y.Z 部分与当前版本匹配
  54. # 只有在推送到 main 分支时才进行自增计算
  55. if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then
  56. LAST_TAG=$(git tag --list "v*.*.*-beta.*" --sort=-committerdate | head -n 1)
  57. # 查找匹配当前 CURRENT_VERSION 的上一个 TAG
  58. LAST_MATCHING_TAG=$(git tag --list "v$CURRENT_VERSION-beta.*" --sort=-committerdate | head -n 1)
  59. if [[ $LAST_MATCHING_TAG =~ v[0-9]+\.[0-9]+\.[0-9]+-beta\.([0-9]+) ]]; then
  60. # 如果找到匹配当前 PATCH 版本的 beta 标签,则递增
  61. LAST_BUILD_NUM="${BASH_REMATCH[1]}"
  62. NEW_BUILD_NUM=$((LAST_BUILD_NUM + 1))
  63. LAST_TAG_FOR_LOG="$LAST_MATCHING_TAG"
  64. echo "版本 $CURRENT_VERSION 未变更,构建号自增:$LAST_BUILD_NUM -> $NEW_BUILD_NUM"
  65. else
  66. # 如果找不到匹配的标签 (可能是 PATCH 刚修改,或首次构建),则重置为 1
  67. NEW_BUILD_NUM=1
  68. # 如果是 PATCH 首次构建,则使用仓库中最近的那个 tag 作为日志起点
  69. LAST_TAG_FOR_LOG="$LAST_TAG"
  70. echo "版本 $CURRENT_VERSION 为新版本或首次构建,构建号重置为 $NEW_BUILD_NUM"
  71. fi
  72. else
  73. # 对于 PR 或其他分支,使用一个固定的构建号 0
  74. NEW_BUILD_NUM=0
  75. LAST_TAG_FOR_LOG=$(git tag --sort=-committerdate | head -n 1)
  76. echo "非 main 分支推送,构建号设置为 $NEW_BUILD_NUM"
  77. fi
  78. # 3. 构造最终版本信息
  79. VERSION_SUFFIX_NAME="-beta.$NEW_BUILD_NUM"
  80. FULL_VERSION_NAME="${CURRENT_VERSION}${VERSION_SUFFIX_NAME}"
  81. # 4. 设置输出供后续作业使用
  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. build:
  87. needs: prepare
  88. strategy:
  89. matrix:
  90. product: [ app ]
  91. type: [ release ]
  92. fail-fast: false
  93. runs-on: ubuntu-latest
  94. env:
  95. product: ${{ matrix.product }}
  96. type: ${{ matrix.type }}
  97. # 使用 prepare job 的输出
  98. VERSION: ${{ needs.prepare.outputs.version }}
  99. VERSIONL: ${{ needs.prepare.outputs.versionL }}
  100. BUILD_NUMBER: ${{ needs.prepare.outputs.build_number }}
  101. steps:
  102. - uses: actions/checkout@v5
  103. with:
  104. fetch-depth: 0
  105. - name: Set up JDK 17
  106. uses: actions/setup-java@v5
  107. with:
  108. distribution: 'temurin'
  109. java-version: 17
  110. - name: Clear 18PlusList.txt
  111. run: |
  112. echo "清空18PlusList.txt"
  113. echo "">$GITHUB_WORKSPACE/app/src/main/assets/18PlusList.txt
  114. - name: Release Apk Sign
  115. run: |
  116. echo "${{ secrets.SIGNING_KEY }}" | base64 -d > $GITHUB_WORKSPACE/app/my-release-key.jks
  117. echo "RELEASE_STORE_FILE=my-release-key.jks" >> $GITHUB_WORKSPACE/gradle.properties
  118. echo "RELEASE_STORE_PASSWORD=${{ secrets.KEYSTORE_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
  119. echo "RELEASE_KEY_ALIAS=${{ secrets.KEY_ALIAS }}" >> $GITHUB_WORKSPACE/gradle.properties
  120. echo "RELEASE_KEY_PASSWORD=${{ secrets.KEY_PASSWORD }}" >> $GITHUB_WORKSPACE/gradle.properties
  121. - name: Set up Gradle
  122. uses: gradle/actions/setup-gradle@v4
  123. - name: 统一版本号
  124. run: |
  125. # 1. 临时修改 version.properties 中的 VERSION_SUFFIX,供 build.gradle 读取 versionName
  126. echo "VERSION_SUFFIX=-beta.${{ env.BUILD_NUMBER }}" >> $GITHUB_WORKSPACE/version.properties
  127. # 2. 覆盖 build.gradle 中复杂的 versionCode 计算逻辑
  128. # 将 versionCode 设为 10000 + BUILD_NUMBER,忽略 gitCommits
  129. # 替换 app/build.gradle 中 versionCode 所在行
  130. sed -i "s/versionCode 10000 + gitCommits/versionCode 10000 + ${{ env.BUILD_NUMBER }}/" $GITHUB_WORKSPACE/app/build.gradle
  131. echo "versionName 已设置为 ${{ env.VERSIONL }}"
  132. echo "versionCode 已设置为 10000 + ${{ env.BUILD_NUMBER }}"
  133. - name: Build With Gradle
  134. run: |
  135. echo "开始${{ env.product }}${{ env.type }}构建"
  136. chmod +x gradlew
  137. # Gradle 现在会读取修改后的 version.properties 来获取 versionName
  138. ./gradlew assemble${{ env.product }}Release
  139. - name: Move Missing Rules Files
  140. run: |
  141. echo "移动missing_rules.txt文件"
  142. mkdir -p ${{ github.workspace }}/mapping/
  143. for file in `ls ${{ github.workspace }}/app/build/outputs/mapping/*/missing_rules.txt`; do
  144. mv "$file" ${{ github.workspace }}/mapping/missing_rules.txt
  145. done
  146. - name: Upload Missing Rules File To Artifact
  147. uses: actions/upload-artifact@v4
  148. with:
  149. name: legado.${{ env.product }}.${{ env.type }}.mapping.missing_rules
  150. if-no-files-found: ignore
  151. path: ${{ github.workspace }}/mapping/missing_rules.txt
  152. - name: Check Build production
  153. run: |
  154. echo "查看 APK 输出目录"
  155. ls -R $GITHUB_WORKSPACE/app/build/outputs/apk/
  156. if ! ls $GITHUB_WORKSPACE/app/build/outputs/apk/*/*/*.apk 1> /dev/null 2>&1; then
  157. echo "Build production not found! Check gradle logs."
  158. exit 1
  159. fi
  160. - name: Upload universal APK
  161. uses: actions/upload-artifact@v4
  162. with:
  163. name: universal-apk
  164. path: app/build/outputs/apk/app/release/legado_app_*[0-9].apk
  165. - name: Upload arm64-v8a APK
  166. uses: actions/upload-artifact@v4
  167. with:
  168. name: arm64-v8a-apk
  169. path: app/build/outputs/apk/app/release/*arm64-v8a.apk
  170. - name: Upload armeabi-v7a APK
  171. uses: actions/upload-artifact@v4
  172. with:
  173. name: armeabi-v7a-apk
  174. path: app/build/outputs/apk/app/release/*armeabi-v7a.apk
  175. - name: Upload Mapping File To Artifact
  176. uses: actions/upload-artifact@v4
  177. with:
  178. name: legado.${{ env.product }}.${{ env.type }}.mapping
  179. if-no-files-found: ignore
  180. path: ${{ github.workspace }}/mapping/mapping.txt
  181. test_Branch:
  182. needs: [ prepare, build ]
  183. runs-on: ubuntu-latest
  184. if: ${{ github.event_name != 'pull_request' && github.actor == 'gedoor' }}
  185. steps:
  186. - uses: actions/checkout@v5
  187. - uses: actions/download-artifact@v5
  188. with:
  189. path: apk/
  190. - working-directory: apk/
  191. run: mv */*.apk . ;rm -rf */
  192. - name: Push To "test" Branch
  193. run: |
  194. cd $GITHUB_WORKSPACE/apk/
  195. git init
  196. git checkout -b test
  197. git config --global user.name "github-actions[bot]"
  198. git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
  199. git remote add origin "https://${{ github.actor }}:${{ secrets.ACTIONS_TOKEN }}@github.com/${{ github.actor }}/release"
  200. git add *.apk
  201. git commit -m "${{ needs.prepare.outputs.versionL }}"
  202. git push -f -u origin test
  203. create_pre_release:
  204. needs: [ prepare, build ]
  205. if: |
  206. github.event_name == 'push' &&
  207. github.ref == 'refs/heads/main' &&
  208. needs.prepare.outputs.build_number > 0 &&
  209. success()
  210. runs-on: ubuntu-latest
  211. steps:
  212. - uses: actions/checkout@v4
  213. with:
  214. fetch-depth: 0
  215. - name: 获取所有 Commit 消息作为 Release Body
  216. id: get_commits
  217. run: |
  218. # 获取从上一个 tag 到当前 commit 之间的所有 commit 消息
  219. # 如果 latest_tag 为空,则从第一个 commit 开始获取
  220. LATEST_TAG="${{ needs.prepare.outputs.latest_tag }}"
  221. if [ -z "$LATEST_TAG" ]; then
  222. # 如果没有找到任何 tag,则从第一个 commit 开始
  223. COMMIT_LOG=$(git log --pretty=format:'* %s (%an)' --reverse)
  224. else
  225. # 从最近的 tag 到当前 HEAD
  226. # 使用 ^$LATEST_TAG 来排除 LATEST_TAG 本身的 commit
  227. COMMIT_LOG=$(git log $LATEST_TAG..HEAD --pretty=format:'* %s (%an)')
  228. fi
  229. # 检查是否获取到日志
  230. if [ -z "$COMMIT_LOG" ]; then
  231. RELEASE_BODY="本次发布无新增 Commit。"
  232. else
  233. RELEASE_BODY="# 本次更新内容\n\n$COMMIT_LOG"
  234. fi
  235. # 输出给下一步使用
  236. echo "release_body<<EOF" >> $GITHUB_OUTPUT
  237. echo "$RELEASE_BODY" >> $GITHUB_OUTPUT
  238. echo "EOF" >> $GITHUB_OUTPUT
  239. - name:
  240. 创建 Pre-Release Tag: v${{ needs.prepare.outputs.versionL }}
  241. # 创建一个 Tag,供下一轮计算版本号时使用
  242. run: |
  243. git tag v${{ needs.prepare.outputs.versionL }}
  244. git push origin v${{ needs.prepare.outputs.versionL }}
  245. env:
  246. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  247. - name: 创建 Pre-Release
  248. id: create_release
  249. uses: actions/create-release@v1
  250. env:
  251. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  252. with:
  253. # Tag 名称:3.26.3-beta.1
  254. tag_name: ${{ needs.prepare.outputs.versionL }}
  255. # Release 标题:3.26.3-beta.1
  256. release_name: ${{ needs.prepare.outputs.versionL }}
  257. # Release 内容为所有 Commit 消息
  258. body: ${{ steps.get_commits.outputs.release_body }}
  259. draft: false
  260. # 标记为预发布版本
  261. prerelease: true
  262. - name: 下载所有 APK Artifacts
  263. uses: actions/download-artifact@v4
  264. with:
  265. pattern: '*-apk'
  266. path: artifacts
  267. merge-multiple: true
  268. - name: 上传 Release Assets
  269. uses: actions/upload-release-asset@v1
  270. env:
  271. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  272. with:
  273. upload_url: ${{ steps.create_release.outputs.upload_url }}
  274. asset_path: artifacts/*.apk
  275. # 重新命名上传的 APK 文件,使其包含版本号
  276. asset_name: legado-${{ needs.prepare.outputs.versionL }}.apk
  277. asset_content_type: application/zip