@@ -99,7 +99,20 @@ export abstract class JobHandler {
9999 null ,
100100 null
101101 ) ;
102- await this . jobRepository . updateWithStatus ( this . currJob . _id , files , JobStatus . completed ) ;
102+
103+ // Prevent the Autobuilder's usual build completion from being sent after content staging job
104+ // if the user already has a Gatsby Cloud site. Job should be marked as
105+ // completed after the Gatsby Cloud build via the SnootyBuildComplete lambda.
106+ const { _id : jobId , user } = this . currJob ;
107+ const gatsbyCloudSiteId = await this . _repoEntitlementsRepo . getGatsbySiteIdByGithubUsername ( user ) ;
108+ if ( gatsbyCloudSiteId && this . currJob . payload . jobType === 'githubPush' ) {
109+ this . logger . info (
110+ jobId ,
111+ `User ${ user } has a Gatsby Cloud site. The Autobuilder will not mark the build as completed right now.`
112+ ) ;
113+ } else {
114+ await this . jobRepository . updateWithStatus ( jobId , files , JobStatus . completed ) ;
115+ }
103116 } else {
104117 if ( publishResult . error ) {
105118 await this . jobRepository . updateWithErrorStatus ( this . currJob . _id , publishResult . error ) ;
@@ -266,14 +279,15 @@ export abstract class JobHandler {
266279 [ `${ stage } EndTime` ] : end ,
267280 } ;
268281
269- this . _jobRepository . findOneAndUpdateExecutionTime ( this . currJob . _id , update ) ;
282+ this . _jobRepository . updateExecutionTime ( this . currJob . _id , update ) ;
270283 return resp ;
271284 }
272285
273286 private async exeBuildModified ( ) : Promise < void > {
274287 const stages = {
275288 [ 'get-build-dependencies' ] : 'buildDepsExe' ,
276289 [ 'next-gen-parse' ] : 'parseExe' ,
290+ [ 'persistence-module' ] : 'persistenceExe' ,
277291 [ 'next-gen-html' ] : 'htmlExe' ,
278292 [ 'oas-page-build' ] : 'oasPageBuildExe' ,
279293 } ;
@@ -303,6 +317,12 @@ export abstract class JobHandler {
303317 const makeCommandsResp = await this . _commandExecutor . execute ( [ command ] ) ;
304318 await this . logBuildDetails ( makeCommandsResp ) ;
305319 }
320+
321+ // Call Gatsby Cloud preview webhook after persistence module finishes for staging builds
322+ const isFeaturePreviewWebhookEnabled = process . env . GATSBY_CLOUD_PREVIEW_WEBHOOK_ENABLED ?. toLowerCase ( ) === 'true' ;
323+ if ( key === 'persistence-module' && this . name === 'Staging' && isFeaturePreviewWebhookEnabled ) {
324+ await this . callGatsbyCloudWebhook ( ) ;
325+ }
306326 }
307327 await this . _logger . save ( this . currJob . _id , `${ '(BUILD)' . padEnd ( 15 ) } Finished Build` ) ;
308328 }
@@ -635,27 +655,44 @@ export abstract class JobHandler {
635655 return this . _logger ;
636656 }
637657
638- protected async previewWebhook ( ) : Promise < AxiosResponse < string > > {
639- const previewWebhookURL = 'https://webhook.gatsbyjs.com/hooks/data_source' ;
640- const githubUsername = this . currJob . user ;
641- const gatsbySiteId = await this . _repoEntitlementsRepo . getGatsbySiteIdByGithubUsername ( githubUsername ) ;
642- if ( ! gatsbySiteId ) {
643- const message = `User ${ githubUsername } does not have a Gatsby Cloud Site ID.` ;
644- this . _logger . warn ( 'Gatsby Cloud Preview Webhook' , message ) ;
645- throw new Error ( message ) ;
646- }
658+ // Invokes Gatsby Cloud Preview Webhook
659+ protected async callGatsbyCloudWebhook ( ) : Promise < void > {
660+ const featurePreviewWebhookEnabled = process . env . GATSBY_CLOUD_PREVIEW_WEBHOOK_ENABLED ;
661+ // Logging for Debugging purposes only will remove once we see the build working in Gatsby.
662+ await this . logger . save (
663+ this . currJob . _id ,
664+ `${ '(GATSBY_CLOUD_PREVIEW_WEBHOOK_ENABLED)' . padEnd ( 15 ) } ${ featurePreviewWebhookEnabled } `
665+ ) ;
647666
648- const url = `${ previewWebhookURL } /${ gatsbySiteId } ` ;
649- return await axios . post (
650- url ,
651- {
652- jobId : this . currJob . _id ,
653- status : this . currJob . status ,
654- } ,
655- {
656- headers : { 'x-gatsby-cloud-data-source' : 'gatsby-source-snooty-preview' } ,
667+ try {
668+ const previewWebhookURL = 'https://webhook.gatsbyjs.com/hooks/data_source' ;
669+ const githubUsername = this . currJob . user ;
670+ const gatsbySiteId = await this . _repoEntitlementsRepo . getGatsbySiteIdByGithubUsername ( githubUsername ) ;
671+ if ( ! gatsbySiteId ) {
672+ const message = `User ${ githubUsername } does not have a Gatsby Cloud Site ID.` ;
673+ this . _logger . warn ( 'Gatsby Cloud Preview Webhook' , message ) ;
674+ throw new Error ( message ) ;
657675 }
658- ) ;
676+
677+ const url = `${ previewWebhookURL } /${ gatsbySiteId } ` ;
678+ const response = await axios . post (
679+ url ,
680+ {
681+ jobId : this . currJob . _id ,
682+ } ,
683+ {
684+ headers : { 'x-gatsby-cloud-data-source' : 'gatsby-source-snooty-preview' } ,
685+ }
686+ ) ;
687+ await this . _jobRepository . updateExecutionTime ( this . currJob . _id , { gatsbyCloudStartTime : new Date ( ) } ) ;
688+ await this . logger . save ( this . currJob . _id , `${ '(POST Webhook Status)' . padEnd ( 15 ) } ${ response . status } ` ) ;
689+ } catch ( err ) {
690+ await this . logger . save (
691+ this . currJob . _id ,
692+ `${ '(POST Webhook)' . padEnd ( 15 ) } Failed to POST to Gatsby Cloud webhook: ${ err } `
693+ ) ;
694+ throw err ;
695+ }
659696 }
660697}
661698
0 commit comments