forked from AceDataCloud/Nexior
-
Notifications
You must be signed in to change notification settings - Fork 0
356 lines (322 loc) · 13.6 KB
/
Copy pathbuild-android.yaml
File metadata and controls
356 lines (322 loc) · 13.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
name: Build Android
on:
push:
tags:
- 'android-v*'
workflow_dispatch:
inputs:
track:
description: 'Initial Play Store track to upload to (default: beta = Open testing)'
required: true
default: 'beta'
type: choice
options:
- internal
- alpha
- beta
- production
promote_to_production:
description: 'Auto-promote the same versionCode to Production after the initial upload succeeds.'
required: false
default: true
type: boolean
rollout_percent:
description: 'Production staged rollout fraction (0.0–1.0). 1.0 = full release.'
required: false
default: '1.0'
type: string
force:
description: 'Build even if this versionCode is already uploaded to Play (default: skip duplicates).'
required: false
default: false
type: boolean
permissions:
contents: read
env:
NODE_VERSION: '22'
JAVA_VERSION: '21'
jobs:
preflight:
name: Preflight (skip if Play already has this versionCode)
runs-on: ubuntu-latest
outputs:
should-build: ${{ steps.check.outputs.should-build }}
version-name: ${{ steps.version.outputs.name }}
version-code: ${{ steps.version.outputs.code }}
track: ${{ steps.track.outputs.track }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Determine version from package.json
id: version
run: |
VERSION=$(node -p "require('./package.json').version")
IFS='.' read -r major minor patch <<< "$VERSION"
CODE=$((major * 10000 + minor * 100 + patch))
echo "name=$VERSION" >> "$GITHUB_OUTPUT"
echo "code=$CODE" >> "$GITHUB_OUTPUT"
echo "Version: $VERSION Code: $CODE"
- name: Determine Play Store track
id: track
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "track=${{ inputs.track }}" >> "$GITHUB_OUTPUT"
else
# Tag push (android-v*) defaults to Open testing (beta) and then promotes to production.
echo "track=beta" >> "$GITHUB_OUTPUT"
fi
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.12'
- name: Check Play Console for existing AAB/APK
id: check
env:
FORCE: ${{ inputs.force }}
SA_JSON: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_JSON }}
PACKAGE_NAME: com.acedatacloud.nexior
VERSION_CODE: ${{ steps.version.outputs.code }}
# Opens a Play Console edit, lists all previously uploaded bundles/apks,
# and aborts the edit. If the target versionCode already appears, we set
# should-build=false so the heavy build job is skipped. Fail-open on any
# API failure so a flaky preflight never blocks a release.
run: |
if [ "$FORCE" = "true" ]; then
echo "::notice::force=true → skipping Play duplicate check."
echo "should-build=true" >> "$GITHUB_OUTPUT"
exit 0
fi
python3 -m pip install --quiet 'pyjwt[crypto]==2.10.1' requests
python3 <<'PY'
import json, os, time, requests, jwt
try:
sa = json.loads(os.environ['SA_JSON'])
package = os.environ['PACKAGE_NAME']
wanted = int(os.environ['VERSION_CODE'])
# Exchange the service-account JWT for an access token.
now = int(time.time())
assertion = jwt.encode(
{
'iss': sa['client_email'],
'scope': 'https://www.googleapis.com/auth/androidpublisher',
'aud': 'https://oauth2.googleapis.com/token',
'iat': now, 'exp': now + 3600,
},
sa['private_key'], algorithm='RS256',
)
tok = requests.post(
'https://oauth2.googleapis.com/token',
data={
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': assertion,
}, timeout=30,
)
tok.raise_for_status()
access = tok.json()['access_token']
h = {'Authorization': f'Bearer {access}'}
base = f'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{package}'
edit = requests.post(f'{base}/edits', headers=h, json={}, timeout=30)
edit.raise_for_status()
edit_id = edit.json()['id']
try:
codes = set()
for kind in ('bundles', 'apks'):
r = requests.get(f'{base}/edits/{edit_id}/{kind}', headers=h, timeout=30)
if r.status_code == 200:
for item in r.json().get(kind, []):
codes.add(int(item['versionCode']))
finally:
requests.delete(f'{base}/edits/{edit_id}', headers=h, timeout=30)
if wanted in codes:
print(f"::notice::Play already has versionCode {wanted} for {package} (uploaded: {sorted(codes)[-10:]}) — skipping.")
should = 'false'
else:
print(f"versionCode {wanted} not yet uploaded (last 10 uploaded: {sorted(codes)[-10:]}) — proceeding.")
should = 'true'
except Exception as e:
print(f"::warning::Play preflight check failed ({e!r}) — letting build proceed.")
should = 'true'
with open(os.environ['GITHUB_OUTPUT'], 'a') as f:
f.write(f'should-build={should}\n')
print(f'should-build={should}')
PY
build:
name: Build & Upload to Play Store
needs: preflight
if: needs.preflight.outputs.should-build == 'true'
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: ${{ env.NODE_VERSION }}
cache: npm
- name: Set up JDK
uses: actions/setup-java@v5
with:
distribution: temurin
java-version: ${{ env.JAVA_VERSION }}
cache: gradle
- name: Install Node dependencies
run: npm ci
- name: Build web assets (Android surface)
env:
VITE_STRIPE_PUBLISHABLE_KEY: ${{ secrets.VITE_STRIPE_PUBLISHABLE_KEY }}
run: npm run build:android
- name: Sync Android project
run: npx cap sync android
- name: Decode keystore
run: |
echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 -d > android/app/release.keystore
shell: bash
- name: Set version in build.gradle
env:
VERSION_NAME: ${{ needs.preflight.outputs.version-name }}
VERSION_CODE: ${{ needs.preflight.outputs.version-code }}
run: |
sed -i "s/versionCode [0-9]*/versionCode $VERSION_CODE/" android/app/build.gradle
sed -i "s/versionName \"[^\"]*\"/versionName \"$VERSION_NAME\"/" android/app/build.gradle
shell: bash
- name: Build release AAB
working-directory: android
run: |
chmod +x gradlew
./gradlew bundleRelease \
-PRELEASE_STORE_FILE=release.keystore \
-PRELEASE_STORE_PASSWORD='${{ secrets.ANDROID_KEYSTORE_PASSWORD }}' \
-PRELEASE_KEY_ALIAS='${{ secrets.ANDROID_KEY_ALIAS }}' \
-PRELEASE_KEY_PASSWORD='${{ secrets.ANDROID_KEY_PASSWORD }}'
- name: Build release APK (for GitHub artifacts)
working-directory: android
run: |
./gradlew assembleRelease \
-PRELEASE_STORE_FILE=release.keystore \
-PRELEASE_STORE_PASSWORD='${{ secrets.ANDROID_KEYSTORE_PASSWORD }}' \
-PRELEASE_KEY_ALIAS='${{ secrets.ANDROID_KEY_ALIAS }}' \
-PRELEASE_KEY_PASSWORD='${{ secrets.ANDROID_KEY_PASSWORD }}'
- name: Upload APK artifact
uses: actions/upload-artifact@v7
with:
name: nexior-${{ needs.preflight.outputs.version-name }}.apk
path: android/app/build/outputs/apk/release/app-release.apk
- name: Upload AAB artifact
uses: actions/upload-artifact@v7
with:
name: nexior-${{ needs.preflight.outputs.version-name }}.aab
path: android/app/build/outputs/bundle/release/app-release.aab
- name: Upload to Play Store
uses: r0adkll/upload-google-play@v1
with:
serviceAccountJsonPlainText: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_JSON }}
packageName: com.acedatacloud.nexior
releaseFiles: android/app/build/outputs/bundle/release/app-release.aab
tracks: ${{ needs.preflight.outputs.track }}
status: completed
promote:
name: Promote to Production
needs: [preflight, build]
# Run when preflight finished AND either the build job uploaded a fresh AAB
# or it was skipped because Play already had this versionCode (so the
# promote step can still push it from beta → production).
if: |
always()
&& needs.preflight.result == 'success'
&& (needs.build.result == 'success' || needs.build.result == 'skipped')
&& needs.preflight.outputs.track != 'production'
&& (github.event_name != 'workflow_dispatch' || inputs.promote_to_production)
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.12'
- name: Promote release to Production track
env:
SA_JSON: ${{ secrets.GOOGLE_PLAY_SERVICE_ACCOUNT_JSON }}
PACKAGE_NAME: com.acedatacloud.nexior
VERSION_CODE: ${{ needs.preflight.outputs.version-code }}
VERSION_NAME: ${{ needs.preflight.outputs.version-name }}
ROLLOUT: ${{ github.event_name == 'workflow_dispatch' && inputs.rollout_percent || '1.0' }}
# Creates a Play Console edit, points the production track at the same versionCode
# uploaded in the previous job, and commits. Uses staged rollout when ROLLOUT < 1.
run: |
python3 -m pip install --quiet 'pyjwt[crypto]==2.10.1' requests
python3 <<'PY'
import json, os, time, requests, jwt
sa = json.loads(os.environ['SA_JSON'])
package = os.environ['PACKAGE_NAME']
wanted = int(os.environ['VERSION_CODE'])
version_name = os.environ.get('VERSION_NAME') or str(wanted)
try:
rollout = float(os.environ.get('ROLLOUT', '1.0'))
except ValueError:
rollout = 1.0
rollout = max(0.0, min(1.0, rollout))
now = int(time.time())
assertion = jwt.encode(
{
'iss': sa['client_email'],
'scope': 'https://www.googleapis.com/auth/androidpublisher',
'aud': 'https://oauth2.googleapis.com/token',
'iat': now, 'exp': now + 3600,
},
sa['private_key'], algorithm='RS256',
)
tok = requests.post(
'https://oauth2.googleapis.com/token',
data={
'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion': assertion,
}, timeout=30,
)
tok.raise_for_status()
access = tok.json()['access_token']
h = {'Authorization': f'Bearer {access}', 'Content-Type': 'application/json'}
base = f'https://androidpublisher.googleapis.com/androidpublisher/v3/applications/{package}'
edit = requests.post(f'{base}/edits', headers=h, json={}, timeout=30)
edit.raise_for_status()
edit_id = edit.json()['id']
try:
bundles = requests.get(f'{base}/edits/{edit_id}/bundles', headers=h, timeout=30)
bundles.raise_for_status()
codes = {int(b['versionCode']) for b in bundles.json().get('bundles', [])}
if wanted not in codes:
raise SystemExit(
f'::error::versionCode {wanted} not found in uploaded bundles '
f'(last 10: {sorted(codes)[-10:]})'
)
release = {
'name': version_name,
'versionCodes': [str(wanted)],
}
if rollout >= 1.0:
release['status'] = 'completed'
elif rollout > 0:
release['status'] = 'inProgress'
release['userFraction'] = rollout
else:
release['status'] = 'draft'
r = requests.put(
f'{base}/edits/{edit_id}/tracks/production',
headers=h,
json={'track': 'production', 'releases': [release]},
timeout=30,
)
if r.status_code >= 400:
print(f'::error::production track update failed: {r.status_code} {r.text}')
r.raise_for_status()
c = requests.post(f'{base}/edits/{edit_id}:commit', headers=h, timeout=60)
if c.status_code >= 400:
print(f'::error::commit failed: {c.status_code} {c.text}')
c.raise_for_status()
print(f'::notice::Promoted versionCode {wanted} ({version_name}) to '
f'production (rollout={rollout}).')
except Exception:
try:
requests.delete(f'{base}/edits/{edit_id}', headers=h, timeout=30)
except Exception:
pass
raise
PY