Skip to content

Commit 338bc72

Browse files
committed
使用GitHub Action打包
1 parent f5cf8e7 commit 338bc72

File tree

7 files changed

+883
-119
lines changed

7 files changed

+883
-119
lines changed
Lines changed: 281 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,281 @@
1+
name: Build Android APK
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*'
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
inputs:
11+
version:
12+
description: 'Version to build (e.g., 1.0.5.3)'
13+
required: false
14+
default: ''
15+
release:
16+
description: 'Create a GitHub Release after build'
17+
required: false
18+
default: 'false'
19+
type: boolean
20+
sign_apk:
21+
description: 'Sign the APK with release key'
22+
required: false
23+
default: 'true'
24+
type: boolean
25+
26+
env:
27+
APP_NAME: 柠檬Push
28+
ENGLISH_NAME: lemon_push
29+
PACKAGE_NAME: net.lemontree.push
30+
31+
jobs:
32+
build-android:
33+
runs-on: ubuntu-latest
34+
35+
steps:
36+
- name: Checkout code
37+
uses: actions/checkout@v3
38+
39+
- name: Set up JDK 11
40+
uses: actions/setup-java@v3
41+
with:
42+
java-version: '11'
43+
distribution: 'temurin'
44+
45+
- name: Setup Android SDK
46+
uses: android-actions/setup-android@v2
47+
48+
- name: Set version from tag or input
49+
id: version
50+
run: |
51+
if [[ "${{ github.ref_type }}" == "tag" ]]; then
52+
VERSION=${GITHUB_REF#refs/tags/v}
53+
echo "version=$VERSION" >> $GITHUB_OUTPUT
54+
echo "is_tag=true" >> $GITHUB_OUTPUT
55+
elif [[ -n "${{ github.event.inputs.version }}" ]]; then
56+
VERSION="${{ github.event.inputs.version }}"
57+
echo "version=$VERSION" >> $GITHUB_OUTPUT
58+
echo "is_tag=false" >> $GITHUB_OUTPUT
59+
else
60+
# Get version from app/build.gradle
61+
VERSION=$(grep "versionName" android/app/build.gradle | sed 's/.*"\(.*\)".*/\1/')
62+
echo "version=$VERSION" >> $GITHUB_OUTPUT
63+
echo "is_tag=false" >> $GITHUB_OUTPUT
64+
fi
65+
echo "Building version: $VERSION"
66+
shell: bash
67+
68+
- name: Cache Gradle packages
69+
uses: actions/cache@v3
70+
with:
71+
path: |
72+
~/.gradle/caches
73+
~/.gradle/wrapper
74+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
75+
restore-keys: |
76+
${{ runner.os }}-gradle-
77+
78+
- name: Grant execute permission for gradlew
79+
run: chmod +x android/gradlew
80+
shell: bash
81+
82+
- name: Update version in build.gradle (if needed)
83+
if: steps.version.outputs.is_tag == 'true' || github.event.inputs.version != ''
84+
run: |
85+
cd android
86+
# Update versionName and versionCode in app/build.gradle
87+
VERSION="${{ steps.version.outputs.version }}"
88+
89+
# Extract version components (assuming format like 1.0.5.3)
90+
IFS='.' read -ra VERSION_PARTS <<< "$VERSION"
91+
MAJOR=${VERSION_PARTS[0]}
92+
MINOR=${VERSION_PARTS[1]}
93+
PATCH=${VERSION_PARTS[2]}
94+
BUILD=${VERSION_PARTS[3]}
95+
96+
# Calculate versionCode (e.g., 10503 for 1.0.5.3)
97+
VERSION_CODE=$((MAJOR * 1000000 + MINOR * 10000 + PATCH * 100 + BUILD))
98+
99+
# Update build.gradle
100+
sed -i "s/versionCode .*/versionCode $VERSION_CODE/" app/build.gradle
101+
sed -i "s/versionName .*/versionName \"$VERSION\"/" app/build.gradle
102+
103+
echo "Updated versionCode to $VERSION_CODE and versionName to $VERSION"
104+
shell: bash
105+
106+
- name: Setup signing configuration
107+
if: github.event.inputs.sign_apk == 'true' || github.event.inputs.sign_apk == ''
108+
run: |
109+
cd android
110+
111+
# Create signing configuration in app/build.gradle
112+
cat >> app/build.gradle << 'EOF'
113+
114+
// Signing configuration added by GitHub Actions
115+
if (project.hasProperty('RELEASE_STORE_FILE')) {
116+
signingConfigs {
117+
release {
118+
storeFile file(RELEASE_STORE_FILE)
119+
storePassword RELEASE_STORE_PASSWORD
120+
keyAlias RELEASE_KEY_ALIAS
121+
keyPassword RELEASE_KEY_PASSWORD
122+
}
123+
}
124+
125+
buildTypes {
126+
release {
127+
signingConfig signingConfigs.release
128+
}
129+
}
130+
}
131+
EOF
132+
133+
echo "Added signing configuration to build.gradle"
134+
shell: bash
135+
136+
- name: Build Debug APK
137+
run: |
138+
cd android
139+
./gradlew assembleDebug
140+
shell: bash
141+
142+
- name: Build Release APK
143+
run: |
144+
cd android
145+
# If signing is enabled and secrets are available, use them
146+
if [[ "${{ github.event.inputs.sign_apk }}" == "true" || "${{ github.event.inputs.sign_apk }}" == "" ]]; then
147+
if [[ -n "${{ secrets.RELEASE_STORE_FILE }}" ]]; then
148+
echo "Building signed release APK..."
149+
echo "${{ secrets.RELEASE_STORE_FILE }}" | base64 -d > app/keystore.jks
150+
./gradlew assembleRelease -PRELEASE_STORE_FILE=app/keystore.jks \
151+
-PRELEASE_STORE_PASSWORD="${{ secrets.RELEASE_STORE_PASSWORD }}" \
152+
-PRELEASE_KEY_ALIAS="${{ secrets.RELEASE_KEY_ALIAS }}" \
153+
-PRELEASE_KEY_PASSWORD="${{ secrets.RELEASE_KEY_PASSWORD }}"
154+
else
155+
echo "Warning: No signing secrets found, building unsigned release APK..."
156+
./gradlew assembleRelease
157+
fi
158+
else
159+
echo "Building unsigned release APK..."
160+
./gradlew assembleRelease
161+
fi
162+
shell: bash
163+
164+
- name: Prepare APK artifacts
165+
run: |
166+
mkdir -p release
167+
# Copy debug APK
168+
cp android/app/build/outputs/apk/debug/app-debug.apk "release/${{ env.ENGLISH_NAME }}_v${{ steps.version.outputs.version }}_debug.apk"
169+
# Copy release APK
170+
cp android/app/build/outputs/apk/release/app-release.apk "release/${{ env.ENGLISH_NAME }}_v${{ steps.version.outputs.version }}_release.apk"
171+
172+
# Create checksums
173+
cd release
174+
sha256sum *.apk > checksums.txt
175+
176+
echo "APK artifacts prepared:"
177+
ls -la
178+
shell: bash
179+
180+
- name: Upload Debug APK
181+
uses: actions/upload-artifact@v3
182+
with:
183+
name: android-debug-apk
184+
path: release/${{ env.ENGLISH_NAME }}_v${{ steps.version.outputs.version }}_debug.apk
185+
retention-days: 30
186+
187+
- name: Upload Release APK
188+
uses: actions/upload-artifact@v3
189+
with:
190+
name: android-release-apk
191+
path: release/${{ env.ENGLISH_NAME }}_v${{ steps.version.outputs.version }}_release.apk
192+
retention-days: 90
193+
194+
- name: Upload Checksums
195+
uses: actions/upload-artifact@v3
196+
with:
197+
name: android-checksums
198+
path: release/checksums.txt
199+
retention-days: 90
200+
201+
create-release:
202+
needs: build-android
203+
runs-on: ubuntu-latest
204+
if: (startsWith(github.ref, 'refs/tags/') || github.event.inputs.release == 'true') && github.event_name != 'pull_request'
205+
206+
steps:
207+
- name: Set version from tag or input
208+
id: version
209+
run: |
210+
if [[ "${{ github.ref_type }}" == "tag" ]]; then
211+
VERSION=${GITHUB_REF#refs/tags/v}
212+
elif [[ -n "${{ github.event.inputs.version }}" ]]; then
213+
VERSION="${{ github.event.inputs.version }}"
214+
else
215+
# Get version from app/build.gradle
216+
VERSION=$(grep "versionName" android/app/build.gradle | sed 's/.*"\(.*\)".*/\1/')
217+
fi
218+
echo "version=$VERSION" >> $GITHUB_OUTPUT
219+
echo "Releasing version: $VERSION"
220+
shell: bash
221+
222+
- name: Download Release APK
223+
uses: actions/download-artifact@v3
224+
with:
225+
name: android-release-apk
226+
path: artifacts
227+
228+
- name: Download Checksums
229+
uses: actions/download-artifact@v3
230+
with:
231+
name: android-checksums
232+
path: artifacts
233+
234+
- name: Create Release
235+
uses: softprops/action-gh-release@v1
236+
with:
237+
name: ${{ env.APP_NAME }} Android v${{ steps.version.outputs.version }}
238+
body: |
239+
## ${{ env.APP_NAME }} Android v${{ steps.version.outputs.version }}
240+
241+
### 下载链接:
242+
- Android Release APK: lemon_push_v${{ steps.version.outputs.version }}_release.apk
243+
- Android Debug APK: lemon_push_v${{ steps.version.outputs.version }}_debug.apk
244+
- 校验和文件: checksums.txt
245+
246+
### 安装说明:
247+
#### Android:
248+
1. 下载 lemon_push_v${{ steps.version.outputs.version }}_release.apk
249+
2. 在 Android 设备上启用"未知来源"安装
250+
3. 点击 APK 文件进行安装
251+
252+
### 功能说明:
253+
- 剪贴板同步功能
254+
- 文本分词处理
255+
- 自动操作模式
256+
- 网络扫描和连接
257+
258+
### 系统要求:
259+
- Android 5.0 (API Level 21) 或更高版本
260+
- 建议在 Android 7.0 或更高版本上运行以获得最佳体验
261+
262+
### 更新日志:
263+
请查看项目提交记录了解详细更新内容。
264+
files: |
265+
artifacts/${{ env.ENGLISH_NAME }}_v${{ steps.version.outputs.version }}_release.apk
266+
artifacts/checksums.txt
267+
draft: false
268+
prerelease: false
269+
env:
270+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
271+
272+
- name: Upload Debug APK to Release
273+
if: startsWith(github.ref, 'refs/tags/')
274+
uses: actions/upload-release-asset@v1
275+
env:
276+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
277+
with:
278+
upload_url: ${{ steps.create_release.outputs.upload_url }}
279+
asset_path: artifacts/${{ env.ENGLISH_NAME }}_v${{ steps.version.outputs.version }}_debug.apk
280+
asset_name: lemon_push_v${{ steps.version.outputs.version }}_debug.apk
281+
asset_content_type: application/vnd.android.package-archive

0 commit comments

Comments
 (0)