release.yml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. name: Release APK
  2. on:
  3. workflow_dispatch:
  4. inputs:
  5. build_run_id:
  6. description: '成功的 Auto Build workflow run ID'
  7. required: true
  8. type: string
  9. permissions:
  10. actions: read
  11. contents: write
  12. jobs:
  13. release:
  14. # 与原自动发布权限保持一致;请在仓库 Settings 中按需改成你的 GitHub 用户名。
  15. if: github.actor == 'HapeLee'
  16. runs-on: ubuntu-latest
  17. steps:
  18. - name: Validate the selected build
  19. id: build
  20. env:
  21. GH_TOKEN: ${{ github.token }}
  22. RUN_ID: ${{ inputs.build_run_id }}
  23. run: |
  24. set -euo pipefail
  25. api="https://api.github.com/repos/${GITHUB_REPOSITORY}"
  26. run=$(curl --fail-with-body -sS \
  27. -H "Authorization: Bearer ${GH_TOKEN}" \
  28. -H "Accept: application/vnd.github+json" \
  29. "${api}/actions/runs/${RUN_ID}")
  30. test "$(jq -r '.name' <<<"${run}")" = "Auto Build"
  31. test "$(jq -r '.conclusion' <<<"${run}")" = success
  32. test "$(jq -r '.head_branch' <<<"${run}")" = main
  33. echo "sha=$(jq -r '.head_sha' <<<"${run}")" >> "$GITHUB_OUTPUT"
  34. - name: Check out the built commit
  35. uses: actions/checkout@v5
  36. with:
  37. ref: ${{ steps.build.outputs.sha }}
  38. fetch-depth: 0
  39. - name: Download artifacts from the selected build
  40. env:
  41. GH_TOKEN: ${{ github.token }}
  42. RUN_ID: ${{ inputs.build_run_id }}
  43. run: |
  44. set -euo pipefail
  45. api="https://api.github.com/repos/${GITHUB_REPOSITORY}"
  46. mkdir artifacts
  47. curl --fail-with-body -sS \
  48. -H "Authorization: Bearer ${GH_TOKEN}" \
  49. -H "Accept: application/vnd.github+json" \
  50. "${api}/actions/runs/${RUN_ID}/artifacts?per_page=100" > artifacts.json
  51. jq -r '.artifacts[] | select(.expired | not) | select(
  52. .name == "universal-apk-release" or
  53. .name == "arm64-v8a-apk-release" or
  54. .name == "armeabi-v7a-apk-release" or
  55. .name == "legado.app.release.mapping" or
  56. .name == "release-metadata"
  57. ) | .archive_download_url' artifacts.json | while read -r url; do
  58. curl --fail-with-body -sSL \
  59. -H "Authorization: Bearer ${GH_TOKEN}" \
  60. -H "Accept: application/vnd.github+json" \
  61. "$url" -o artifact.zip
  62. unzip -o artifact.zip -d artifacts
  63. done
  64. test -f artifacts/release-metadata.env
  65. - name: Prepare release files and notes
  66. id: release
  67. run: |
  68. set -euo pipefail
  69. source artifacts/release-metadata.env
  70. test -n "$VERSIONL"
  71. mkdir release
  72. for apk in artifacts/*.apk; do
  73. case "$apk" in
  74. *arm64-v8a*) suffix=-arm64-v8a ;;
  75. *armeabi-v7a*) suffix=-armeabi-v7a ;;
  76. *) suffix= ;;
  77. esac
  78. mv "$apk" "release/legado-${VERSIONL}${suffix}.apk"
  79. done
  80. if [ -f artifacts/mapping.txt ]; then mv artifacts/mapping.txt release/; fi
  81. if [ -n "$LATEST_TAG" ] && git rev-parse -q --verify "refs/tags/$LATEST_TAG" >/dev/null; then
  82. notes=$(git log "$LATEST_TAG"..HEAD --pretty=format:'* %s' --no-merges)
  83. else
  84. notes=$(git log -1 --pretty=format:'* %s')
  85. fi
  86. [ -n "$notes" ] || notes='- 没有新的 Commit 记录。'
  87. {
  88. echo 'custom_body<<EOF'
  89. if [ "$IS_RELEASE" != true ]; then
  90. echo '**此版本为测试版,会存在不稳定问题。**'
  91. echo '### [发布页](https://t.me/materado)'
  92. echo '### [反馈群组](https://t.me/+Yn1_v5PZqddmMjQ1)'
  93. echo
  94. fi
  95. echo '### 具体更新内容'
  96. echo
  97. echo "$notes"
  98. echo EOF
  99. } >> "$GITHUB_OUTPUT"
  100. echo "version=$VERSIONL" >> "$GITHUB_OUTPUT"
  101. echo "previous_tag=$LATEST_TAG" >> "$GITHUB_OUTPUT"
  102. echo "prerelease=$([ "$IS_RELEASE" = true ] && echo false || echo true)" >> "$GITHUB_OUTPUT"
  103. - name: Create tag at the built commit
  104. env:
  105. VERSION: ${{ steps.release.outputs.version }}
  106. run: |
  107. if git rev-parse -q --verify "refs/tags/$VERSION" >/dev/null; then
  108. test "$(git rev-list -n 1 "$VERSION")" = "${{ steps.build.outputs.sha }}"
  109. else
  110. git tag "$VERSION" "${{ steps.build.outputs.sha }}"
  111. git push origin "$VERSION"
  112. fi
  113. - name: Generate GitHub release notes
  114. id: notes
  115. env:
  116. GH_TOKEN: ${{ github.token }}
  117. VERSION: ${{ steps.release.outputs.version }}
  118. SHA: ${{ steps.build.outputs.sha }}
  119. PREVIOUS_TAG: ${{ steps.release.outputs.previous_tag }}
  120. CUSTOM_BODY: ${{ steps.release.outputs.custom_body }}
  121. run: |
  122. set -euo pipefail
  123. payload=$(jq -n \
  124. --arg tag_name "$VERSION" \
  125. --arg target_commitish "$SHA" \
  126. --arg previous_tag_name "$PREVIOUS_TAG" \
  127. '{tag_name: $tag_name, target_commitish: $target_commitish} +
  128. (if $previous_tag_name == "" then {} else {previous_tag_name: $previous_tag_name} end)')
  129. generated=$(curl --fail-with-body -sS -X POST \
  130. -H "Authorization: Bearer ${GH_TOKEN}" \
  131. -H "Accept: application/vnd.github+json" \
  132. "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/generate-notes" \
  133. -d "$payload" | jq -r .body)
  134. {
  135. echo 'body<<EOF'
  136. echo "$generated"
  137. echo
  138. echo "$CUSTOM_BODY"
  139. echo EOF
  140. } >> "$GITHUB_OUTPUT"
  141. - name: Create GitHub release
  142. uses: softprops/action-gh-release@v2
  143. with:
  144. tag_name: ${{ steps.release.outputs.version }}
  145. name: ${{ steps.release.outputs.prerelease == 'true' && format('Pre-release {0}', steps.release.outputs.version) || format('Release {0}', steps.release.outputs.version) }}
  146. body: ${{ steps.notes.outputs.body }}
  147. prerelease: ${{ steps.release.outputs.prerelease }}
  148. files: release/*