-
-
Save BradRyan/48b96cba3cdc75df848ce0497f8d5cac to your computer and use it in GitHub Desktop.
Custom plugin for filtering builds on Netlify by doppler environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* When environemnt variables are updated in Doppler, Doppler posts a deploy webhook (created in Netlify). | |
* Doppler uses the same webhook for updates to all environments (prd, stg, dev). Since the webhook is setup | |
* for deploying to the master/main branch, we want to ignore requests from all environments except for 'prd' | |
* | |
* Doppler Reference: https://docs.doppler.com/docs/enclave-webhooks | |
* Netlify Reference: https://docs.netlify.com/configure-builds/build-plugins/create-plugins | |
* | |
* @param {string} [inputs.title] - Title of the build hook used by Doppler. Assumes webhook name | |
* of "Doppler" if not provided | |
*/ | |
const PAYLOAD_INITIAL = { | |
config: {}, | |
project: {}, | |
workplace: {} | |
}; | |
module.exports = function dopplerWebhookDeployments(inputs) { | |
const { | |
INCOMING_HOOK_TITLE, | |
INCOMING_HOOK_BODY = PAYLOAD_INITIAL, | |
INCOMING_HOOK_URL, | |
CONTEXT | |
} = process.env; | |
const webhookName = (inputs && inputs.title) || 'Doppler'; | |
const dopplerBuildHook = INCOMING_HOOK_TITLE === webhookName; | |
const dopperEnvUpdate = INCOMING_HOOK_BODY.config.environment; | |
const prodEnvUpdate = dopperEnvUpdate === 'prd'; | |
if (dopplerBuildHook) { | |
return { | |
onPreBuild: ({ utils }) => { | |
console.log('Incoming Doppler build request', { | |
INCOMING_HOOK_TITLE, | |
INCOMING_HOOK_URL, | |
INCOMING_HOOK_BODY, | |
CONTEXT | |
}); | |
if (!prodEnvUpdate) { | |
utils.build.cancelBuild( | |
`Doppler build hook for ${dopperEnvUpdate}. Only production updates trigger deployments on master.` | |
); | |
} | |
} | |
}; | |
} | |
// If deploy is not triggered by doppler then no need to run onPreBuild hook. | |
return {}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment