1- // Mirrors the newest build on a Google Play track to a rolling GitHub
2- // prerelease as a universal APK . Play generates and signs that APK from the
1+ // Publishes closed-testing betas, or downloads the internal APK for a normal
2+ // nightly release . Play generates and signs the APK from the
33// uploaded bundle with the app signing key, so it carries the same signature
44// as the Play Store install. Run by .github/workflows/mobile-android-apk.yml.
55const { execFileSync } = require ( "node:child_process" ) ;
@@ -77,43 +77,57 @@ function gh(...args) {
7777
7878function readRelease ( tag ) {
7979 try {
80- return JSON . parse ( gh ( "release" , "view" , tag , "--json" , "assets,body" ) ) ;
80+ return JSON . parse ( gh ( "release" , "view" , tag , "--json" , "assets,body,isDraft " ) ) ;
8181 } catch {
8282 return undefined ;
8383 }
8484}
8585
86- // Downloads the build's universal APK and uploads it, creating the release on
87- // first use. Returns false when Play lists no universal APK, which happens
88- // while it is still processing a fresh upload.
89- async function uploadApk ( { token, appUrl, build, tag, apkName, notes, title, release } ) {
86+ // Download to a temporary directory so a failed request cannot leave a partial
87+ // APK among the release assets.
88+ async function downloadApk ( { token, appUrl, build, apkName } ) {
9089 const generated = await (
9190 await play ( token , "GET" , `${ appUrl } /generatedApks/${ build . versionCode } ` )
9291 ) . json ( ) ;
93- // One entry per app signing key. More than one means a key rotation, so
94- // fail rather than guess which signature testers should get.
9592 const universal = ( generated . generatedApks ?? [ ] ) . filter ( ( entry ) => entry . generatedUniversalApk ) ;
96- if ( universal . length === 0 ) return false ;
93+ if ( universal . length === 0 ) return undefined ;
9794 if ( universal . length > 1 ) {
9895 throw new Error ( `Play returned ${ universal . length } universal APKs, one per signing key.` ) ;
9996 }
10097 const { downloadId } = universal [ 0 ] . generatedUniversalApk ;
98+ const directory = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "apk-" ) ) ;
99+ const apkPath = path . join ( directory , apkName ) ;
100+ try {
101+ const download = await play (
102+ token ,
103+ "GET" ,
104+ `${ appUrl } /generatedApks/${ build . versionCode } /downloads/${ encodeURIComponent ( downloadId ) } :download?alt=media` ,
105+ ) ;
106+ await pipeline ( Readable . fromWeb ( download . body ) , fs . createWriteStream ( apkPath ) ) ;
107+ return apkPath ;
108+ } catch ( error ) {
109+ fs . rmSync ( directory , { recursive : true , force : true } ) ;
110+ throw error ;
111+ }
112+ }
101113
102- const apkPath = path . join ( fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "apk-" ) ) , apkName ) ;
103- const download = await play (
104- token ,
105- "GET" ,
106- `${ appUrl } /generatedApks/${ build . versionCode } /downloads/${ encodeURIComponent ( downloadId ) } :download?alt=media` ,
107- ) ;
108- await pipeline ( Readable . fromWeb ( download . body ) , fs . createWriteStream ( apkPath ) ) ;
109-
114+ function publishBeta ( tag , apkPath , notes , title ) {
115+ const release = readRelease ( tag ) ;
116+ if (
117+ release &&
118+ ! release . isDraft &&
119+ release . assets . some (
120+ ( asset ) =>
121+ asset . name === path . basename ( apkPath ) && asset . state === "uploaded" && asset . size > 0 ,
122+ )
123+ )
124+ return ;
110125 if ( ! release ) {
111- // Prerelease with a non-v tag: release.yml, the desktop updater, the CLI,
112- // and the marketing site's latest-release lookup all ignore it.
113126 gh (
114127 "release" ,
115128 "create" ,
116129 tag ,
130+ "--draft" ,
117131 "--prerelease" ,
118132 "--latest=false" ,
119133 "--title" ,
@@ -122,106 +136,92 @@ async function uploadApk({ token, appUrl, build, tag, apkName, notes, title, rel
122136 notes ,
123137 ) ;
124138 }
125- // --clobber replaces an asset a failed upload left behind.
126139 gh ( "release" , "upload" , tag , apkPath , "--clobber" ) ;
127- return true ;
140+ gh (
141+ "release" ,
142+ "edit" ,
143+ tag ,
144+ "--draft=false" ,
145+ "--prerelease" ,
146+ "--latest=false" ,
147+ "--title" ,
148+ title ,
149+ "--notes" ,
150+ notes ,
151+ ) ;
128152}
129153
130154async function main ( ) {
131155 const packageName = requireEnv ( "PACKAGE_NAME" ) ;
132- const tag = requireEnv ( "RELEASE_TAG" ) ;
133- const title = "T3 Code for Android (testing)" ;
134- const key = JSON . parse ( requireEnv ( "GOOGLE_PLAY_SERVICE_ACCOUNT_JSON" ) ) ;
135- const summary = ( line ) =>
136- process . env . GITHUB_STEP_SUMMARY &&
137- fs . appendFileSync ( process . env . GITHUB_STEP_SUMMARY , `${ line } \n` ) ;
138-
139- const token = await playAccessToken ( key ) ;
156+ const outputDirectory = process . env . APK_OUTPUT_DIR ;
157+ const channel = outputDirectory ? "internal" : "beta" ;
158+ const track = outputDirectory ? "internal" : "alpha" ;
159+ const token = await playAccessToken ( JSON . parse ( requireEnv ( "GOOGLE_PLAY_SERVICE_ACCOUNT_JSON" ) ) ) ;
140160 const appUrl = `${ PLAY_API } /${ packageName } ` ;
141- let release = readRelease ( tag ) ;
142- const assets = release ?. assets ?? [ ] ;
143- const sections = [ ] ;
144- for ( const { track, channel, label, stability } of [
145- {
146- track : "alpha" ,
147- channel : "beta" ,
148- label : "Beta" ,
149- stability :
150- "Promoted for wider testing. Recommended over internal builds, but may still contain bugs." ,
151- } ,
152- {
153- track : "internal" ,
154- channel : "internal" ,
155- label : "Internal" ,
156- stability : "Newest, less-tested builds. May contain serious bugs." ,
157- } ,
158- ] ) {
159- const prefix = `t3code-android-${ channel } -` ;
160- let available = assets . filter (
161- ( asset ) => asset . name . startsWith ( prefix ) && asset . state === "uploaded" && asset . size > 0 ,
161+ const build = await newestTrackBuild ( token , appUrl , track ) ;
162+ if ( ! build ) {
163+ console . log ( `The ${ track } track has no live release.` ) ;
164+ return ;
165+ }
166+ const safeName = build . versionName . replace ( / [ ^ 0 - 9 A - Z a - z . ] + / g, "-" ) . replace ( / ^ - | - $ / g, "" ) ;
167+ const apkName = `t3code-android-${ channel } -${ safeName } -${ build . versionCode } .apk` ;
168+ const tag = outputDirectory ? undefined : requireEnv ( "RELEASE_TAG" ) ;
169+ const snapshotTag = `android-beta-${ build . versionCode } ` ;
170+ const release = tag ? readRelease ( tag ) : undefined ;
171+ const snapshot = tag ? readRelease ( snapshotTag ) : undefined ;
172+ const hasApk = ( value ) =>
173+ value &&
174+ ! value . isDraft &&
175+ value . assets . some (
176+ ( asset ) => asset . name === apkName && asset . state === "uploaded" && asset . size > 0 ,
162177 ) ;
163- const build = await newestTrackBuild ( token , appUrl , track ) ;
164- if ( build ) {
165- const safeName = build . versionName . replace ( / [ ^ 0 - 9 A - Z a - z . ] + / g, "-" ) . replace ( / ^ - | - $ / g, "" ) ;
166- const apkName = `${ prefix } ${ safeName } -${ build . versionCode } .apk` ;
167- const published = assets . some (
168- ( asset ) => asset . name === apkName && asset . state === "uploaded" && asset . size > 0 ,
169- ) ;
170- if (
171- published ||
172- ( await uploadApk ( {
173- token,
174- appUrl,
175- build,
176- tag,
177- apkName,
178- notes : "Android testing APKs. Release notes are being updated." ,
179- title,
180- release,
181- } ) )
182- ) {
183- release ??= { assets : [ ] , body : "" } ;
184- available = [ { name : apkName } ] ;
185- summary ( `Published or verified \`${ apkName } \` on \`${ tag } \`.` ) ;
186- // Clean up only this track after its replacement is available. Retire
187- // the legacy unlabelled APK when the beta replacement is published.
188- for ( const stale of assets ) {
189- const belongsToTrack =
190- stale . name . startsWith ( prefix ) ||
191- ( track === "alpha" && / ^ t 3 c o d e - a n d r o i d - (? ! b e t a - | i n t e r n a l - ) .* \. a p k $ / . test ( stale . name ) ) ;
192- if ( belongsToTrack && stale . name . endsWith ( ".apk" ) && stale . name !== apkName ) {
193- gh ( "release" , "delete-asset" , tag , stale . name , "--yes" ) ;
194- }
195- }
196- } else {
197- summary (
198- `Play is still processing the ${ track } APK for ${ build . versionCode } ; keeping the previous download.` ,
199- ) ;
178+ const pruneBetaAssets = ( ) => {
179+ for ( const stale of release ?. assets ?? [ ] ) {
180+ if ( stale . name . endsWith ( ".apk" ) && stale . name !== apkName ) {
181+ gh ( "release" , "delete-asset" , tag , stale . name , "--yes" ) ;
200182 }
201183 }
202- sections . push (
203- `## ${ label } ` ,
204- "" ,
205- stability ,
206- "" ,
207- ...available . map (
208- ( asset ) =>
209- `- [${ asset . name } ](https://github.com/${ requireEnv ( "GH_REPO" ) } /releases/download/${ tag } /${ asset . name } )` ,
210- ) ,
211- ...( available . length ? [ ] : [ "No APK available yet." ] ) ,
184+ } ;
185+ if ( hasApk ( release ) && hasApk ( snapshot ) ) {
186+ pruneBetaAssets ( ) ;
187+ console . log ( `${ apkName } is already published.` ) ;
188+ return ;
189+ }
190+ const apkPath = await downloadApk ( { token, appUrl, build, apkName } ) ;
191+ if ( ! apkPath ) {
192+ console . log ( `Play is still processing the ${ track } APK for ${ build . versionCode } .` ) ;
193+ return ;
194+ }
195+ try {
196+ if ( outputDirectory ) {
197+ fs . mkdirSync ( outputDirectory , { recursive : true } ) ;
198+ const destination = path . join ( outputDirectory , apkName ) ;
199+ const temporary = `${ destination } .tmp` ;
200+ try {
201+ fs . copyFileSync ( apkPath , temporary ) ;
202+ fs . renameSync ( temporary , destination ) ;
203+ } finally {
204+ fs . rmSync ( temporary , { force : true } ) ;
205+ }
206+ return ;
207+ }
208+ const notes = [
209+ `T3 Code for Android beta, mirrored from Google Play closed testing. Build: ${ build . versionName } (${ build . versionCode } ).` ,
212210 "" ,
211+ "These builds have been promoted for wider testing, but may still contain bugs. The beta is optional; the store app also works with Nightly servers." ,
212+ ] . join ( "\n" ) ;
213+ // Publish the versioned beta first, then advance the stable download page.
214+ // The build number makes retries and unchanged hourly checks idempotent.
215+ publishBeta (
216+ snapshotTag ,
217+ apkPath ,
218+ notes ,
219+ `Android beta ${ build . versionName } (${ build . versionCode } )` ,
213220 ) ;
214- }
215- if ( ! release ) return ;
216- const notes = [
217- "T3 Code Android APKs mirrored from Google Play. Only the newest available APK from each track is kept here." ,
218- "" ,
219- ...sections ,
220- ]
221- . join ( "\n" )
222- . trim ( ) ;
223- if ( release . body . replace ( / \r \n / g, "\n" ) . trim ( ) !== notes ) {
224- gh ( "release" , "edit" , tag , "--title" , title , "--notes" , notes ) ;
221+ publishBeta ( tag , apkPath , notes , "T3 Code for Android (beta)" ) ;
222+ pruneBetaAssets ( ) ;
223+ } finally {
224+ fs . rmSync ( path . dirname ( apkPath ) , { recursive : true , force : true } ) ;
225225 }
226226}
227227
0 commit comments