Skip to content

Commit 4db04a1

Browse files
committed
👷 添加action配置文件
1 parent 2b5e130 commit 4db04a1

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
name: Build and Release Android APK
2+
3+
# 触发条件:当推送以v开头的tag时
4+
on:
5+
push:
6+
tags:
7+
- 'v*.*.*' # 匹配 v1.0.0, v2.1.3 等格式
8+
# 也可以手动触发
9+
workflow_dispatch:
10+
11+
jobs:
12+
build-and-release:
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
# 1. 检出代码
17+
- name: Checkout Repository
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0 # 获取完整历史,用于生成changelog
21+
22+
# 2. 设置JDK环境
23+
- name: Set up JDK 17
24+
uses: actions/setup-java@v4
25+
with:
26+
java-version: '17'
27+
distribution: 'temurin'
28+
29+
# 3. 设置Android SDK
30+
- name: Setup Android SDK
31+
uses: android-actions/setup-android@v3
32+
33+
# 4. 缓存Gradle依赖
34+
- name: Cache Gradle packages
35+
uses: actions/cache@v4
36+
with:
37+
path: |
38+
~/.gradle/caches
39+
~/.gradle/wrapper
40+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
41+
restore-keys: |
42+
${{ runner.os }}-gradle-
43+
44+
# 5. 赋予gradlew执行权限
45+
- name: Grant execute permission for gradlew
46+
run: chmod +x gradlew
47+
48+
# 6. 获取版本信息
49+
- name: Get version info
50+
id: version
51+
run: |
52+
VERSION=${GITHUB_REF#refs/tags/}
53+
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
54+
echo "VERSION_CODE=$(git rev-list --count HEAD)" >> $GITHUB_OUTPUT
55+
56+
# 7. 构建Debug APK
57+
- name: Build Debug APK
58+
run: ./gradlew assembleDebug
59+
60+
# 8. 构建Release APK
61+
- name: Build Release APK
62+
run: ./gradlew assembleRelease
63+
env:
64+
VERSION_NAME: ${{ steps.version.outputs.VERSION }}
65+
VERSION_CODE: ${{ steps.version.outputs.VERSION_CODE }}
66+
67+
# 9. 签名APK (如果配置了签名)
68+
- name: Sign APK
69+
if: ${{ env.KEYSTORE_FILE != '' }}
70+
run: |
71+
echo "${{ secrets.KEYSTORE_FILE }}" | base64 -d > keystore.jks
72+
jarsigner -verbose -sigalg SHA256withRSA -digestalg SHA-256 \
73+
-keystore keystore.jks \
74+
-storepass "${{ secrets.KEYSTORE_PASSWORD }}" \
75+
-keypass "${{ secrets.KEY_PASSWORD }}" \
76+
app/build/outputs/apk/release/app-release-unsigned.apk \
77+
"${{ secrets.KEY_ALIAS }}"
78+
env:
79+
KEYSTORE_FILE: ${{ secrets.KEYSTORE_FILE }}
80+
81+
# 10. 重命名APK文件
82+
- name: Rename APK files
83+
run: |
84+
VERSION=${{ steps.version.outputs.VERSION }}
85+
mv app/build/outputs/apk/debug/app-debug.apk app/build/outputs/apk/debug/MyApp-${VERSION}-debug.apk
86+
mv app/build/outputs/apk/release/app-release.apk app/build/outputs/apk/release/MyApp-${VERSION}-release.apk
87+
88+
# 11. 生成Changelog
89+
- name: Generate Changelog
90+
id: changelog
91+
run: |
92+
PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "")
93+
if [ -n "$PREVIOUS_TAG" ]; then
94+
CHANGELOG=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"- %s" --no-merges)
95+
else
96+
CHANGELOG=$(git log --pretty=format:"- %s" --no-merges -10)
97+
fi
98+
echo "CHANGELOG<<EOF" >> $GITHUB_OUTPUT
99+
echo "$CHANGELOG" >> $GITHUB_OUTPUT
100+
echo "EOF" >> $GITHUB_OUTPUT
101+
102+
# 12. 创建GitHub Release
103+
- name: Create GitHub Release
104+
id: create_release
105+
uses: actions/create-release@v1
106+
env:
107+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
108+
with:
109+
tag_name: ${{ steps.version.outputs.VERSION }}
110+
release_name: Release ${{ steps.version.outputs.VERSION }}
111+
body: |
112+
## 🚀 Release ${{ steps.version.outputs.VERSION }}
113+
114+
### 📱 Download
115+
- **Release APK**: For production use
116+
- **Debug APK**: For testing and debugging
117+
118+
### 📋 Changes
119+
${{ steps.changelog.outputs.CHANGELOG }}
120+
121+
### 📊 Build Info
122+
- Version Code: ${{ steps.version.outputs.VERSION_CODE }}
123+
- Build Date: ${{ github.event.head_commit.timestamp }}
124+
- Commit: ${{ github.sha }}
125+
draft: false
126+
prerelease: false
127+
128+
# 13. 上传Release APK
129+
- name: Upload Release APK
130+
uses: actions/upload-release-asset@v1
131+
env:
132+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
133+
with:
134+
upload_url: ${{ steps.create_release.outputs.upload_url }}
135+
asset_path: app/build/outputs/apk/release/MyApp-${{ steps.version.outputs.VERSION }}-release.apk
136+
asset_name: MyApp-${{ steps.version.outputs.VERSION }}-release.apk
137+
asset_content_type: application/vnd.android.package-archive
138+
139+
# 14. 上传Debug APK
140+
- name: Upload Debug APK
141+
uses: actions/upload-release-asset@v1
142+
env:
143+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
144+
with:
145+
upload_url: ${{ steps.create_release.outputs.upload_url }}
146+
asset_path: app/build/outputs/apk/debug/MyApp-${{ steps.version.outputs.VERSION }}-debug.apk
147+
asset_name: MyApp-${{ steps.version.outputs.VERSION }}-debug.apk
148+
asset_content_type: application/vnd.android.package-archive
149+
150+
# 15. 上传构建产物到Artifacts (备份)
151+
- name: Upload APK Artifacts
152+
uses: actions/upload-artifact@v4
153+
with:
154+
name: apk-files-${{ steps.version.outputs.VERSION }}
155+
path: |
156+
app/build/outputs/apk/release/*.apk
157+
app/build/outputs/apk/debug/*.apk
158+
retention-days: 30

0 commit comments

Comments
 (0)