-
Notifications
You must be signed in to change notification settings - Fork 4
293 lines (262 loc) · 11.8 KB
/
deploy-android.yml
File metadata and controls
293 lines (262 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
name: Deploy Android
on:
workflow_dispatch:
inputs:
environment:
description: '部署环境'
required: true
type: choice
options:
- staging
- production
version:
description: '版本号 (如 v1.2.0)'
required: true
type: string
concurrency:
group: deploy-android-${{ github.event.inputs.environment }}
cancel-in-progress: false
jobs:
build:
runs-on: ubuntu-latest
outputs:
apk_size: ${{ steps.verify.outputs.apk_size }}
steps:
- uses: actions/checkout@v4
- name: Setup JDK
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Setup Android SDK
uses: android-actions/setup-android@v3
- name: Decode keystore
run: echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 -d > android/app/release.keystore
- name: Build Release APK
run: |
cd android
./gradlew assembleRelease \
-Pandroid.injected.signing.store.file=app/release.keystore \
-Pandroid.injected.signing.store.password="${{ secrets.ANDROID_KEYSTORE_PASSWORD }}" \
-Pandroid.injected.signing.key.alias="${{ secrets.ANDROID_KEY_ALIAS }}" \
-Pandroid.injected.signing.key.password="${{ secrets.ANDROID_KEY_PASSWORD }}" \
-PversionName=${{ github.event.inputs.version }}
env:
BUILD_ENV: ${{ github.event.inputs.environment }}
- name: Build AAB (production only)
if: github.event.inputs.environment == 'production'
run: |
cd android
./gradlew bundleRelease \
-Pandroid.injected.signing.store.file=app/release.keystore \
-Pandroid.injected.signing.store.password="${{ secrets.ANDROID_KEYSTORE_PASSWORD }}" \
-Pandroid.injected.signing.key.alias="${{ secrets.ANDROID_KEY_ALIAS }}" \
-Pandroid.injected.signing.key.password="${{ secrets.ANDROID_KEY_PASSWORD }}" \
-PversionName=${{ github.event.inputs.version }}
- name: Verify build output
id: verify
run: |
APK_PATH=$(find android/app/build/outputs/apk/release -name "*.apk" | head -1)
if [ -z "$APK_PATH" ]; then
echo "❌ APK not found"
exit 1
fi
APK_SIZE=$(du -h "$APK_PATH" | cut -f1)
echo "✅ APK built: ${APK_PATH} (${APK_SIZE})"
echo "apk_size=${APK_SIZE}" >> $GITHUB_OUTPUT
- name: Upload APK artifact
uses: actions/upload-artifact@v4
with:
name: android-apk-${{ github.event.inputs.version }}
path: android/app/build/outputs/apk/release/*.apk
retention-days: 30
- name: Upload AAB artifact
if: github.event.inputs.environment == 'production'
uses: actions/upload-artifact@v4
with:
name: android-aab-${{ github.event.inputs.version }}
path: android/app/build/outputs/bundle/release/*.aab
retention-days: 30
# ─── staging: 上传到内部分发平台, 输出下载链接 ───
deploy-staging:
if: github.event.inputs.environment == 'staging'
needs: build
runs-on: ubuntu-latest
environment: staging
outputs:
download_url: ${{ steps.distribute.outputs.download_url }}
steps:
- name: Download APK
uses: actions/download-artifact@v4
with:
name: android-apk-${{ github.event.inputs.version }}
path: apk/
- name: Upload to distribution platform
id: distribute
run: |
APK_PATH=$(find apk/ -name "*.apk" | head -1)
# ── 蒲公英 (pgyer) ──
if [ -n "${{ secrets.PGYER_API_KEY }}" ]; then
RESPONSE=$(curl -s -F "file=@${APK_PATH}" \
-F "_api_key=${{ secrets.PGYER_API_KEY }}" \
-F "buildUpdateDescription=${{ github.event.inputs.version }}" \
https://www.pgyer.com/apiv2/app/upload)
BUILD_KEY=$(echo $RESPONSE | jq -r '.data.buildKey // empty')
if [ -n "$BUILD_KEY" ]; then
DOWNLOAD_URL="https://www.pgyer.com/${BUILD_KEY}"
echo "✅ Uploaded to pgyer"
else
echo "❌ pgyer upload failed: $RESPONSE"
exit 1
fi
# ── fir.im ──
elif [ -n "${{ secrets.FIR_TOKEN }}" ]; then
RESPONSE=$(curl -s -F "file=@${APK_PATH}" \
-F "type=android" \
-F "bundle_id=${{ vars.ANDROID_PACKAGE_NAME }}" \
-F "api_token=${{ secrets.FIR_TOKEN }}" \
https://api.bq04.com/apps)
SHORT_URL=$(echo $RESPONSE | jq -r '.short // empty')
if [ -n "$SHORT_URL" ]; then
DOWNLOAD_URL="https://fir.im/${SHORT_URL}"
echo "✅ Uploaded to fir.im"
else
echo "❌ fir.im upload failed: $RESPONSE"
exit 1
fi
# ── 自建文件服务器 (SCP + Nginx 静态文件) ──
elif [ -n "${{ secrets.DEPLOY_SSH_KEY }}" ]; then
mkdir -p ~/.ssh
echo "${{ secrets.DEPLOY_SSH_KEY }}" > ~/.ssh/deploy_key
chmod 600 ~/.ssh/deploy_key
ssh-keyscan -p ${{ vars.DEPLOY_SSH_PORT || 22 }} ${{ vars.DEPLOY_HOST }} >> ~/.ssh/known_hosts 2>/dev/null
REMOTE_DIR="${{ vars.APK_SERVE_PATH }}/${{ github.event.inputs.version }}"
HOST="${{ vars.DEPLOY_HOST }}"
USER="${{ vars.DEPLOY_USER }}"
PORT="${{ vars.DEPLOY_SSH_PORT || 22 }}"
ssh -i ~/.ssh/deploy_key -p ${PORT} ${USER}@${HOST} "mkdir -p ${REMOTE_DIR}"
scp -i ~/.ssh/deploy_key -P ${PORT} ${APK_PATH} ${USER}@${HOST}:${REMOTE_DIR}/app-release.apk
DOWNLOAD_URL="${{ vars.APK_SERVE_URL }}/${{ github.event.inputs.version }}/app-release.apk"
echo "✅ Uploaded to file server"
rm -f ~/.ssh/deploy_key
else
echo "❌ No distribution platform configured"
exit 1
fi
echo "download_url=${DOWNLOAD_URL}" >> $GITHUB_OUTPUT
echo ""
echo "════════════════════════════════════════"
echo "✅ 分发完成!"
echo "📱 下载地址: ${DOWNLOAD_URL}"
echo "════════════════════════════════════════"
# ─── production: 上传到 Google Play ───
deploy-production:
if: github.event.inputs.environment == 'production'
needs: build
runs-on: ubuntu-latest
environment: production
outputs:
download_url: ${{ steps.upload.outputs.download_url }}
steps:
- name: Download AAB
uses: actions/download-artifact@v4
with:
name: android-aab-${{ github.event.inputs.version }}
path: aab/
- name: Upload to Google Play
id: upload
uses: r0adkll/upload-google-play@v1
with:
serviceAccountJsonPlainText: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT }}
packageName: ${{ vars.ANDROID_PACKAGE_NAME }}
releaseFiles: aab/*.aab
track: internal
status: completed
- name: Output download link
run: |
DOWNLOAD_URL="https://play.google.com/store/apps/details?id=${{ vars.ANDROID_PACKAGE_NAME }}"
echo "download_url=${DOWNLOAD_URL}" >> $GITHUB_OUTPUT
echo ""
echo "════════════════════════════════════════"
echo "✅ 已提交 Google Play!"
echo "📱 商店地址: ${DOWNLOAD_URL}"
echo "════════════════════════════════════════"
# ─── 通知 (附带下载地址) ───
notify:
needs: [build, deploy-staging, deploy-production]
if: always()
runs-on: ubuntu-latest
steps:
- name: Prepare content
id: content
run: |
ENV="${{ github.event.inputs.environment }}"
if [ "$ENV" == "staging" ]; then
DOWNLOAD_URL="${{ needs.deploy-staging.outputs.download_url }}"
RESULT="${{ needs.deploy-staging.result }}"
TARGET="内部分发"
else
DOWNLOAD_URL="${{ needs.deploy-production.outputs.download_url }}"
RESULT="${{ needs.deploy-production.result }}"
TARGET="Google Play"
fi
STATUS="✅ 成功"
if [ "$RESULT" != "success" ]; then
STATUS="❌ 失败"
DOWNLOAD_URL="部署失败, 请检查日志"
fi
echo "status=${STATUS}" >> $GITHUB_OUTPUT
echo "download_url=${DOWNLOAD_URL}" >> $GITHUB_OUTPUT
echo "target=${TARGET}" >> $GITHUB_OUTPUT
- name: Send DingTalk notification
if: vars.DINGTALK_WEBHOOK != ''
run: |
curl -s -X POST "${{ vars.DINGTALK_WEBHOOK }}" \
-H 'Content-Type: application/json' \
-d "{
\"msgtype\": \"markdown\",
\"markdown\": {
\"title\": \"Android 部署通知\",
\"text\": \"## 🤖 Android 部署通知\n\n- **目标**: ${{ steps.content.outputs.target }}\n- **版本**: ${{ github.event.inputs.version }}\n- **包大小**: ${{ needs.build.outputs.apk_size }}\n- **状态**: ${{ steps.content.outputs.status }}\n- **触发人**: ${{ github.actor }}\n\n**📱 下载地址**: ${{ steps.content.outputs.download_url }}\"
}
}"
- name: Send Feishu notification
if: vars.FEISHU_WEBHOOK != ''
run: |
curl -s -X POST "${{ vars.FEISHU_WEBHOOK }}" \
-H 'Content-Type: application/json' \
-d "{
\"msg_type\": \"interactive\",
\"card\": {
\"header\": { \"title\": { \"tag\": \"plain_text\", \"content\": \"🤖 Android 部署通知\" } },
\"elements\": [
{
\"tag\": \"div\",
\"text\": { \"tag\": \"lark_md\", \"content\": \"**目标**: ${{ steps.content.outputs.target }}\n**版本**: ${{ github.event.inputs.version }}\n**状态**: ${{ steps.content.outputs.status }}\n**触发人**: ${{ github.actor }}\" }
},
{
\"tag\": \"action\",
\"actions\": [{
\"tag\": \"button\",
\"text\": { \"tag\": \"plain_text\", \"content\": \"📱 下载 APK\" },
\"url\": \"${{ steps.content.outputs.download_url }}\",
\"type\": \"primary\"
}]
}
]
}
}"
- name: Output summary
run: |
echo ""
echo "══════════════════════════════════════════════════"
echo "📋 部署摘要"
echo "──────────────────────────────────────────────────"
echo " 平台: Android"
echo " 目标: ${{ steps.content.outputs.target }}"
echo " 版本: ${{ github.event.inputs.version }}"
echo " 包大小: ${{ needs.build.outputs.apk_size }}"
echo " 状态: ${{ steps.content.outputs.status }}"
echo ""
echo " 📱 下载地址: ${{ steps.content.outputs.download_url }}"
echo "══════════════════════════════════════════════════"