Conditional builds for multiple build hooks (Multiple sites, 1 CMS)

Hi,

I am building multiple Gatsby sites (one for each of our facilities) that all connect to 1 CMS (Sanity.io) and are hosted on Netlify. The sites are differentiated in the graphql query by facility record ID. I’ve set up a separate build hook for each of the sites.

I would like to set up the build process so that only the site who’s data has changed on the CMS is the one that’s rebuilt.

I imagine it has to do with one of the hook Environment Variables and the netlify.toml file, but I am unsure how to go about it.

How can I set this up properly?

TIA

Sites (so far):

  • cathedral-health-care.netlify.app
  • river-terrace.netlify.app

Tutorials / posts viewed

Deploy Log (River Terrace)

11:30:05 AM: build-image version: 53b83b6bede2920f236b25b6f5a95334320dc849
11:30:05 AM: build-image tag: v3.6.0
11:30:05 AM: buildbot version: f66121aa8b7df3f09ef4ca9f2f1aa9cddabd6c71
11:30:05 AM: Fetching cached dependencies
11:30:05 AM: Starting to download cache of 301.0MB
11:30:07 AM: Finished downloading cache in 1.665308568s
11:30:07 AM: Starting to extract cache
11:30:17 AM: Finished extracting cache in 10.372448122s
11:30:17 AM: Finished fetching cache in 12.119105986s
11:30:17 AM: Starting to prepare the repo for build
11:30:18 AM: Preparing Git Reference refs/heads/master
11:30:19 AM: Different publish path detected, going to use the one specified in the Netlify configuration file: 'public' versus 'public/' in the Netlify UI
11:30:19 AM: Different build command detected, going to use the one specified in the Netlify configuration file: 'yarn build' versus 'gatsby build' in the Netlify UI
11:30:19 AM: Starting build script
11:30:19 AM: Installing dependencies
11:30:19 AM: Python version set to 2.7
11:30:20 AM: Started restoring cached node version
11:30:22 AM: Finished restoring cached node version
11:30:23 AM: v12.18.0 is already installed.
11:30:24 AM: Now using node v12.18.0 (npm v6.14.4)
11:30:24 AM: Started restoring cached build plugins
11:30:24 AM: Finished restoring cached build plugins
11:30:24 AM: Attempting ruby version 2.7.1, read from environment
11:30:25 AM: Using ruby version 2.7.1
11:30:25 AM: Using PHP version 5.6
11:30:25 AM: Started restoring cached node modules
11:30:25 AM: Finished restoring cached node modules
11:30:25 AM: Started restoring cached yarn cache
11:30:25 AM: Finished restoring cached yarn cache
11:30:26 AM: Installing NPM modules using Yarn version 1.22.4
11:30:26 AM: yarn install v1.22.4
11:30:27 AM: [1/4] Resolving packages...
11:30:27 AM: success Already up-to-date.
11:30:27 AM: Done in 1.01s.
11:30:27 AM: NPM modules installed using Yarn
11:30:28 AM: Started restoring cached go cache
11:30:28 AM: Finished restoring cached go cache
11:30:28 AM: go version go1.14.4 linux/amd64
11:30:28 AM: go version go1.14.4 linux/amd64
11:30:28 AM: Installing missing commands
11:30:28 AM: Verify run directory
11:30:29 AM: ​
11:30:29 AM: ────────────────────────────────────────────────────────────────
11:30:29 AM:   Netlify Build                                                 
11:30:29 AM: ────────────────────────────────────────────────────────────────
11:30:29 AM: ​
11:30:29 AM: ❯ Version
11:30:29 AM:   @netlify/build 6.0.0
11:30:29 AM: ​
11:30:29 AM: ❯ Flags
11:30:29 AM:   deployId: 5fd24d0bbc27100007e879b5
11:30:29 AM:   mode: buildbot
11:30:29 AM: ​
11:30:29 AM: ❯ Current directory
11:30:29 AM:   /opt/build/repo
11:30:29 AM: ​
11:30:29 AM: ❯ Config file
11:30:29 AM:   /opt/build/repo/netlify.toml
11:30:29 AM: ​
11:30:29 AM: ❯ Context
11:30:29 AM:   production
11:30:29 AM: ​
11:30:29 AM: ────────────────────────────────────────────────────────────────
11:30:29 AM:   1. build.command from netlify.toml                            
11:30:29 AM: ────────────────────────────────────────────────────────────────
11:30:29 AM: ​
11:30:29 AM: $ yarn build
11:30:30 AM: yarn run v1.22.4
11:30:30 AM: $ gatsby build
11:30:33 AM: Using environment config: 'production'
11:30:33 AM: success open and validate gatsby-configs - 0.036s
11:30:34 AM: success load plugins - 0.726s
11:30:34 AM: success onPreInit - 0.029s
11:30:34 AM: success delete html and css files from previous builds - 0.003s
11:30:34 AM: success initialize cache - 0.006s
11:30:34 AM: success copy gatsby files - 0.028s
11:30:34 AM: warning [sanity] Using `watchMode` when not in develop mode might prevent your build from completing
11:30:34 AM: info [sanity] Fetching remote GraphQL schema
11:30:34 AM: info [sanity] Transforming to Gatsby-compatible GraphQL SDL
11:30:34 AM: info [sanity] Stitching GraphQL schemas from SDL
11:30:34 AM: success onPreBootstrap - 0.344s
11:30:34 AM: success createSchemaCustomization - 0.006s
11:30:34 AM: info [sanity] Fetching export stream for dataset
11:30:34 AM: info [sanity] Watch mode enabled, starting a listener
11:30:34 AM: info [sanity] Done! Exported 10 documents.
11:30:34 AM: success Checking for changed pages - 0.000s
11:30:34 AM: success source and transform nodes - 0.271s
11:30:35 AM: success building schema - 0.308s
11:30:35 AM: info Total nodes: 38, SitePage nodes: 1 (use --verbose for breakdown)
11:30:35 AM: success createPages - 0.002s
11:30:35 AM: success Checking for changed pages - 0.000s
11:30:35 AM: success createPagesStatefully - 0.063s
11:30:35 AM: success update schema - 0.027s
11:30:35 AM: success onPreExtractQueries - 0.003s
11:30:35 AM: success extract queries from components - 0.631s
11:30:35 AM: success write out redirect data - 0.001s
11:30:35 AM: success onPostBootstrap - 0.000s
11:30:35 AM: info bootstrap finished - 5.615s
11:30:35 AM: success run static queries - 0.023s - 1/1 43.69/s
11:30:35 AM: success run page queries - 0.006s - 5/5 864.15/s
11:30:35 AM: success write out requires - 0.004s
11:31:13 AM: success Building production JavaScript and CSS bundles - 37.683s
11:31:13 AM: success Rewriting compilation hashes - 0.002s
11:31:20 AM: {}
11:31:20 AM: {}
11:31:20 AM: success Building static HTML for pages - 7.059s - 5/5 0.71/s
11:31:20 AM: success onPostBuild - 0.001s
11:31:20 AM: info Done building in 50.6771465 sec
11:31:21 AM: Done in 50.99s.
11:31:21 AM: ​
11:31:21 AM: (build.command completed in 51.2s)
11:31:21 AM: ​
11:31:21 AM: ────────────────────────────────────────────────────────────────
11:31:21 AM:   2. Functions bundling                                         
11:31:21 AM: ────────────────────────────────────────────────────────────────
11:31:21 AM: ​
11:31:21 AM: Packaging Functions from functions directory:
11:31:21 AM:  - helloWorld.js
11:31:21 AM:  - postmarkSend.js
11:31:21 AM: ​
11:31:21 AM: (Functions bundling completed in 668ms)
11:31:21 AM: ​
11:31:21 AM: ────────────────────────────────────────────────────────────────
11:31:21 AM:   Netlify Build Complete                                        
11:31:21 AM: ────────────────────────────────────────────────────────────────
11:31:21 AM: ​
11:31:21 AM: (Netlify Build completed in 52s)
11:31:21 AM: Caching artifacts
11:31:21 AM: Started saving node modules
11:31:21 AM: Finished saving node modules
11:31:21 AM: Started saving build plugins
11:31:21 AM: Finished saving build plugins
11:31:21 AM: Started saving yarn cache
11:31:21 AM: Finished saving yarn cache
11:31:21 AM: Started saving pip cache
11:31:22 AM: Finished saving pip cache
11:31:22 AM: Started saving emacs cask dependencies
11:31:22 AM: Finished saving emacs cask dependencies
11:31:22 AM: Started saving maven dependencies
11:31:22 AM: Finished saving maven dependencies
11:31:22 AM: Started saving boot dependencies
11:31:22 AM: Finished saving boot dependencies
11:31:22 AM: Started saving rust rustup cache
11:31:22 AM: Finished saving rust rustup cache
11:31:22 AM: Started saving rust cargo bin cache
11:31:22 AM: Finished saving rust cargo bin cache
11:31:22 AM: Started saving go dependencies
11:31:22 AM: Finished saving go dependencies
11:31:22 AM: Build script success
11:31:22 AM: Starting to deploy site from 'public'
11:31:22 AM: Creating deploy tree 
11:31:22 AM: Creating deploy upload records
11:31:22 AM: 0 new files to upload
11:31:22 AM: 0 new functions to upload
11:31:22 AM: Starting post processing
11:31:22 AM: Post processing - HTML
11:31:22 AM: Post processing - header rules
11:31:22 AM: Post processing - redirect rules
11:31:22 AM: Post processing done
11:31:23 AM: Site is live ✨
11:32:14 AM: Finished processing build request in 2m9.47022827s

Deploy Log (Cathedral)

11:28:37 AM: build-image version: 53b83b6bede2920f236b25b6f5a95334320dc849
11:28:37 AM: build-image tag: v3.6.0
11:28:37 AM: buildbot version: f66121aa8b7df3f09ef4ca9f2f1aa9cddabd6c71
11:28:37 AM: Fetching cached dependencies
11:28:37 AM: Starting to download cache of 302.2MB
11:28:39 AM: Finished downloading cache in 2.018606399s
11:28:39 AM: Starting to extract cache
11:28:51 AM: Finished extracting cache in 11.969224626s
11:28:51 AM: Finished fetching cache in 14.09391878s
11:28:51 AM: Starting to prepare the repo for build
11:28:51 AM: Preparing Git Reference refs/heads/master
11:28:53 AM: Different functions path detected, going to use the one specified in the Netlify configuration file: 'functions' versus '' in the Netlify UI
11:28:53 AM: Starting build script
11:28:53 AM: Installing dependencies
11:28:53 AM: Python version set to 2.7
11:28:54 AM: Started restoring cached node version
11:28:57 AM: Finished restoring cached node version
11:28:58 AM: v12.18.0 is already installed.
11:28:59 AM: Now using node v12.18.0 (npm v6.14.4)
11:28:59 AM: Started restoring cached build plugins
11:28:59 AM: Finished restoring cached build plugins
11:28:59 AM: Attempting ruby version 2.7.1, read from environment
11:29:01 AM: Using ruby version 2.7.1
11:29:01 AM: Using PHP version 5.6
11:29:01 AM: Started restoring cached node modules
11:29:01 AM: Finished restoring cached node modules
11:29:01 AM: Started restoring cached yarn cache
11:29:01 AM: Finished restoring cached yarn cache
11:29:02 AM: Installing NPM modules using Yarn version 1.22.4
11:29:03 AM: yarn install v1.22.4
11:29:03 AM: [1/4] Resolving packages...
11:29:04 AM: success Already up-to-date.
11:29:04 AM: Done in 1.39s.
11:29:04 AM: NPM modules installed using Yarn
11:29:04 AM: Started restoring cached go cache
11:29:04 AM: Finished restoring cached go cache
11:29:05 AM: go version go1.14.4 linux/amd64
11:29:05 AM: go version go1.14.4 linux/amd64
11:29:05 AM: Installing missing commands
11:29:05 AM: Verify run directory
11:29:07 AM: ​
11:29:07 AM: ────────────────────────────────────────────────────────────────
11:29:07 AM:   Netlify Build                                                 
11:29:07 AM: ────────────────────────────────────────────────────────────────
11:29:07 AM: ​
11:29:07 AM: ❯ Version
11:29:07 AM:   @netlify/build 6.0.0
11:29:07 AM: ​
11:29:07 AM: ❯ Flags
11:29:07 AM:   deployId: 5fd24cb36307fd1ea017b689
11:29:07 AM:   mode: buildbot
11:29:07 AM: ​
11:29:07 AM: ❯ Current directory
11:29:07 AM:   /opt/build/repo
11:29:07 AM: ​
11:29:07 AM: ❯ Config file
11:29:07 AM:   /opt/build/repo/netlify.toml
11:29:07 AM: ​
11:29:07 AM: ❯ Context
11:29:07 AM:   production
11:29:07 AM: ​
11:29:07 AM: ────────────────────────────────────────────────────────────────
11:29:07 AM:   1. build.command from netlify.toml                            
11:29:07 AM: ────────────────────────────────────────────────────────────────
11:29:07 AM: ​
11:29:07 AM: $ yarn build
11:29:07 AM: yarn run v1.22.4
11:29:07 AM: $ gatsby build
11:29:11 AM: Using environment config: 'production'
11:29:11 AM: success open and validate gatsby-configs - 0.039s
11:29:11 AM: success load plugins - 0.744s
11:29:11 AM: success onPreInit - 0.082s
11:29:11 AM: success delete html and css files from previous builds - 0.007s
11:29:11 AM: success initialize cache - 0.012s
11:29:11 AM: success copy gatsby files - 0.039s
11:29:12 AM: warning [sanity] Using `watchMode` when not in develop mode might prevent your build from completing
11:29:12 AM: info [sanity] Fetching remote GraphQL schema
11:29:12 AM: info [sanity] Transforming to Gatsby-compatible GraphQL SDL
11:29:12 AM: info [sanity] Stitching GraphQL schemas from SDL
11:29:12 AM: success onPreBootstrap - 0.242s
11:29:12 AM: success createSchemaCustomization - 0.008s
11:29:12 AM: info [sanity] Fetching export stream for dataset
11:29:12 AM: info [sanity] Watch mode enabled, starting a listener
11:29:12 AM: info [sanity] Done! Exported 10 documents.
11:29:12 AM: success Checking for changed pages - 0.000s
11:29:12 AM: success source and transform nodes - 0.282s
11:29:13 AM: success building schema - 0.426s
11:29:13 AM: info Total nodes: 38, SitePage nodes: 1 (use --verbose for breakdown)
11:29:13 AM: success createPages - 0.001s
11:29:13 AM: success Checking for changed pages - 0.000s
11:29:13 AM: success createPagesStatefully - 0.074s
11:29:13 AM: success update schema - 0.030s
11:29:13 AM: success onPreExtractQueries - 0.004s
11:29:14 AM: success extract queries from components - 0.838s
11:29:14 AM: success write out redirect data - 0.001s
11:29:14 AM: success onPostBootstrap - 0.000s
11:29:14 AM: info bootstrap finished - 6.445s
11:29:14 AM: success run static queries - 0.033s - 1/1 30.60/s
11:29:14 AM: success run page queries - 0.007s - 5/5 680.62/s
11:29:14 AM: success write out requires - 0.004s
11:29:54 AM: success Building production JavaScript and CSS bundles - 40.623s
11:29:54 AM: success Rewriting compilation hashes - 0.002s
11:30:01 AM: {}
11:30:01 AM: {}
11:30:01 AM: success Building static HTML for pages - 6.460s - 5/5 0.77/s
11:30:01 AM: success onPostBuild - 0.001s
11:30:01 AM: info Done building in 53.901874477 sec
11:30:01 AM: Done in 54.24s.
11:30:01 AM: ​
11:30:01 AM: (build.command completed in 54.5s)
11:30:01 AM: ​
11:30:01 AM: ────────────────────────────────────────────────────────────────
11:30:01 AM:   2. Functions bundling                                         
11:30:01 AM: ────────────────────────────────────────────────────────────────
11:30:01 AM: ​
11:30:01 AM: Packaging Functions from functions directory:
11:30:01 AM:  - helloWorld.js
11:30:01 AM:  - postmarkSend.js
11:30:02 AM: ​
11:30:02 AM: (Functions bundling completed in 578ms)
11:30:02 AM: ​
11:30:02 AM: ────────────────────────────────────────────────────────────────
11:30:02 AM:   Netlify Build Complete                                        
11:30:02 AM: ────────────────────────────────────────────────────────────────
11:30:02 AM: ​
11:30:02 AM: (Netlify Build completed in 55.2s)
11:30:02 AM: Caching artifacts
11:30:02 AM: Started saving node modules
11:30:02 AM: Finished saving node modules
11:30:02 AM: Started saving build plugins
11:30:02 AM: Finished saving build plugins
11:30:02 AM: Started saving yarn cache
11:30:02 AM: Finished saving yarn cache
11:30:02 AM: Started saving pip cache
11:30:02 AM: Finished saving pip cache
11:30:02 AM: Started saving emacs cask dependencies
11:30:02 AM: Finished saving emacs cask dependencies
11:30:02 AM: Started saving maven dependencies
11:30:02 AM: Finished saving maven dependencies
11:30:02 AM: Started saving boot dependencies
11:30:02 AM: Finished saving boot dependencies
11:30:02 AM: Started saving rust rustup cache
11:30:02 AM: Finished saving rust rustup cache
11:30:02 AM: Started saving rust cargo bin cache
11:30:02 AM: Finished saving rust cargo bin cache
11:30:02 AM: Started saving go dependencies
11:30:02 AM: Finished saving go dependencies
11:30:02 AM: Build script success
11:30:02 AM: Starting to deploy site from 'public'
11:30:02 AM: Creating deploy tree 
11:30:02 AM: Creating deploy upload records
11:30:02 AM: 2 new files to upload
11:30:02 AM: 0 new functions to upload
11:30:03 AM: Starting post processing
11:30:03 AM: Post processing - HTML
11:30:03 AM: Post processing - header rules
11:30:03 AM: Post processing - redirect rules
11:30:03 AM: Post processing done
11:30:03 AM: Site is live ✨
11:30:54 AM: Finished processing build request in 2m17.799595563s```

Got it!

Created a build plugin for each site:

// index.js
module.exports = {
  onPreBuild: ({ utils, netlifyConfig }) => {
    const INCOMING_HOOK_BODY =
      netlifyConfig.build.environment.INCOMING_HOOK_BODY
    if (INCOMING_HOOK_BODY) {
      JSON.parse(INCOMING_HOOK_BODY).ids.updated.includes(
        "[Sanity.io Facility ID]"
      )
        ? console.log("This site updated")
        : utils.build.cancelBuild("This site not updated")
    }
  },
}
2 Likes