@@ -24,9 +24,6 @@ export interface PreviewDeployment extends Deployment {
2424/** Type describing a Firebase deployment. */
2525export type DeploymentInfo = ProductionDeployment | PreviewDeployment ;
2626
27- /** Path to a temporary file for the GCP service key credentials file. */
28- const gcpServiceKeyTmpFile = path . join ( os . tmpdir ( ) , 'mat-docs-deploy-gcp-key.json' ) ;
29-
3027/**
3128 * Deploys the docs site at the specified directory to Firebase with respect
3229 * to the deployment information provided.
@@ -42,26 +39,38 @@ export async function deployToSite(
4239 const firebase = async ( ...cmd : string [ ] ) =>
4340 $ `pnpm --dir=${ projectPath } /docs firebase --non-interactive ${ cmd } ` ;
4441
42+ const gcpServiceKeyTmpFile = path . join ( os . tmpdir ( ) , 'mat-docs-deploy-gcp-key.json' ) ;
43+
4544 // Setup GCP service key for the docs-app deployment.
4645 // https://firebase.google.com/docs/admin/setup.
4746 await fs . promises . writeFile ( gcpServiceKeyTmpFile , firebaseServiceKey ) ;
48- process . env [ 'GOOGLE_APPLICATION_CREDENTIALS' ] = gcpServiceKeyTmpFile ;
4947
50- await firebase ( 'use' , info . projectId , '--debug' ) ;
51- await firebase ( 'target:clear' , 'hosting' , 'mat-aio' ) ;
52- await firebase ( 'target:apply' , 'hosting' , 'mat-aio' , info . site . firebaseSiteId ) ;
48+ try {
49+ process . env [ 'GOOGLE_APPLICATION_CREDENTIALS' ] = gcpServiceKeyTmpFile ;
5350
54- if ( isPreviewDeployment ( info ) ) {
55- const channelId = info . channelId ;
56- const expires = info . expires ;
51+ await firebase ( 'use' , info . projectId , '--debug' ) ;
52+ await firebase ( 'target:clear' , 'hosting' , 'mat-aio' ) ;
53+ await firebase ( 'target:apply' , 'hosting' , 'mat-aio' , info . site . firebaseSiteId ) ;
5754
58- await firebase ( 'hosting:channel:deploy' , channelId , '--only' , 'mat-aio' , '--expires' , expires ) ;
59- } else {
60- await firebase ( 'deploy' , '--only' , 'hosting:mat-aio' , '--message' , info . description ) ;
61- }
55+ if ( isPreviewDeployment ( info ) ) {
56+ const channelId = info . channelId ;
57+ const expires = info . expires ;
6258
63- // Remove the temporary service key file (this is an optional step and just for sanity).
64- await fs . promises . rm ( gcpServiceKeyTmpFile , { force : true } ) ;
59+ await firebase (
60+ 'hosting:channel:deploy' ,
61+ channelId ,
62+ '--only' ,
63+ 'mat-aio' ,
64+ '--expires' ,
65+ expires ,
66+ ) ;
67+ } else {
68+ await firebase ( 'deploy' , '--only' , 'hosting:mat-aio' , '--message' , info . description ) ;
69+ }
70+ } finally {
71+ process . env [ 'GOOGLE_APPLICATION_CREDENTIALS' ] = undefined ;
72+ await fs . promises . rm ( gcpServiceKeyTmpFile , { force : true } ) ;
73+ }
6574}
6675
6776/** Whether the given deployment info corresponds to a preview deployment. */
0 commit comments