release.yml 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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: Download artifacts from 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 '.conclusion' <<<"${run}")" = success
  31. test "$(jq -r '.head_branch' <<<"${run}")" = main
  32. echo "sha=$(jq -r '.head_sha' <<<"${run}")" >> "$GITHUB_OUTPUT"
  33. mkdir artifacts
  34. curl --fail-with-body -sS \
  35. -H "Authorization: Bearer ${GH_TOKEN}" \
  36. -H "Accept: application/vnd.github+json" \
  37. "${api}/actions/runs/${RUN_ID}/artifacts?per_page=100" > artifacts.json
  38. jq -r '.artifacts[] | select(.expired | not) | select(
  39. .name == "universal-apk-release" or
  40. .name == "arm64-v8a-apk-release" or
  41. .name == "armeabi-v7a-apk-release" or
  42. .name == "legado.app.release.mapping" or
  43. .name == "release-metadata"
  44. ) | .archive_download_url' artifacts.json | while read -r url; do
  45. curl --fail-with-body -sSL \
  46. -H "Authorization: Bearer ${GH_TOKEN}" \
  47. -H "Accept: application/vnd.github+json" \
  48. "$url" -o artifact.zip
  49. unzip -o artifact.zip -d artifacts
  50. done
  51. test -f artifacts/release-metadata.env
  52. - name: Check out the built commit
  53. uses: actions/checkout@v5
  54. with:
  55. ref: ${{ steps.build.outputs.sha }}
  56. fetch-depth: 0
  57. - name: Prepare release files and notes
  58. id: release
  59. run: |
  60. set -euo pipefail
  61. source artifacts/release-metadata.env
  62. test -n "$VERSIONL"
  63. mkdir release
  64. for apk in artifacts/*.apk; do
  65. case "$apk" in
  66. *arm64-v8a*) suffix=-arm64-v8a ;;
  67. *armeabi-v7a*) suffix=-armeabi-v7a ;;
  68. *) suffix= ;;
  69. esac
  70. mv "$apk" "release/legado-${VERSIONL}${suffix}.apk"
  71. done
  72. if [ -f artifacts/mapping.txt ]; then mv artifacts/mapping.txt release/; fi
  73. if [ -n "$LATEST_TAG" ] && git rev-parse -q --verify "refs/tags/$LATEST_TAG" >/dev/null; then
  74. notes=$(git log "$LATEST_TAG"..HEAD --pretty=format:'* %s' --no-merges)
  75. else
  76. notes=$(git log -1 --pretty=format:'* %s')
  77. fi
  78. [ -n "$notes" ] || notes='- 没有新的 Commit 记录。'
  79. {
  80. echo 'custom_body<<EOF'
  81. if [ "$IS_RELEASE" != true ]; then
  82. echo '**此版本为测试版,会存在不稳定问题。**'
  83. echo '### [发布页](https://t.me/materado)'
  84. echo '### [反馈群组](https://t.me/+Yn1_v5PZqddmMjQ1)'
  85. echo
  86. fi
  87. echo '### 具体更新内容'
  88. echo
  89. echo "$notes"
  90. echo EOF
  91. } >> "$GITHUB_OUTPUT"
  92. echo "version=$VERSIONL" >> "$GITHUB_OUTPUT"
  93. echo "previous_tag=$LATEST_TAG" >> "$GITHUB_OUTPUT"
  94. echo "prerelease=$([ "$IS_RELEASE" = true ] && echo false || echo true)" >> "$GITHUB_OUTPUT"
  95. - name: Create tag at the built commit
  96. env:
  97. VERSION: ${{ steps.release.outputs.version }}
  98. run: |
  99. if git rev-parse -q --verify "refs/tags/$VERSION" >/dev/null; then
  100. test "$(git rev-list -n 1 "$VERSION")" = "${{ steps.build.outputs.sha }}"
  101. else
  102. git tag "$VERSION" "${{ steps.build.outputs.sha }}"
  103. git push origin "$VERSION"
  104. - name: Generate GitHub release notes
  105. id: notes
  106. env:
  107. GH_TOKEN: ${{ github.token }}
  108. VERSION: ${{ steps.release.outputs.version }}
  109. SHA: ${{ steps.build.outputs.sha }}
  110. PREVIOUS_TAG: ${{ steps.release.outputs.previous_tag }}
  111. CUSTOM_BODY: ${{ steps.release.outputs.custom_body }}
  112. run: |
  113. set -euo pipefail
  114. payload=$(jq -n \
  115. --arg tag_name "$VERSION" \
  116. --arg target_commitish "$SHA" \
  117. --arg previous_tag_name "$PREVIOUS_TAG" \
  118. '{tag_name: $tag_name, target_commitish: $target_commitish} +
  119. (if $previous_tag_name == "" then {} else {previous_tag_name: $previous_tag_name} end)')
  120. generated=$(curl --fail-with-body -sS -X POST \
  121. -H "Authorization: Bearer ${GH_TOKEN}" \
  122. -H "Accept: application/vnd.github+json" \
  123. "https://api.github.com/repos/${GITHUB_REPOSITORY}/releases/generate-notes" \
  124. -d "$payload" | jq -r .body)
  125. {
  126. echo 'body<<EOF'
  127. echo "$generated"
  128. echo
  129. echo "$CUSTOM_BODY"
  130. echo EOF
  131. } >> "$GITHUB_OUTPUT"
  132. fi
  133. - name: Create GitHub release
  134. uses: softprops/action-gh-release@v2
  135. with:
  136. tag_name: ${{ steps.release.outputs.version }}
  137. name: ${{ steps.release.outputs.prerelease == 'true' && format('Pre-release {0}', steps.release.outputs.version) || format('Release {0}', steps.release.outputs.version) }}
  138. body: ${{ steps.notes.outputs.body }}
  139. prerelease: ${{ steps.release.outputs.prerelease }}
  140. files: release/*