netlify-purge-cloudflare-on-deploy
Advanced tools
Comparing version 1.0.7 to 1.1.0
69
index.js
const fetch = require('node-fetch'); | ||
const { | ||
env: { CLOUDFLARE_API_KEY, CLOUDFLARE_ZONE_ID, CLOUDFLARE_EMAIL }, | ||
env: { CLOUDFLARE_API_TOKEN, CLOUDFLARE_API_KEY, CLOUDFLARE_ZONE_ID, CLOUDFLARE_EMAIL }, | ||
} = require('process'); | ||
let authMethod = 'na'; | ||
switch( true ) { | ||
case typeof CLOUDFLARE_API_TOKEN !== 'undefined' && typeof CLOUDFLARE_ZONE_ID !== 'undefined': | ||
authMethod = 'TOKEN'; | ||
break; | ||
case typeof CLOUDFLARE_API_KEY !== 'undefined' && typeof CLOUDFLARE_ZONE_ID !== 'undefined' && typeof CLOUDFLARE_EMAIL !== 'undefined': | ||
authMethod = 'KEY'; | ||
break; | ||
} | ||
module.exports = { | ||
onPreBuild: ({ utils }) => { | ||
if (!CLOUDFLARE_API_KEY || !CLOUDFLARE_ZONE_ID || !CLOUDFLARE_EMAIL) { | ||
return utils.build.failBuild( | ||
'You need to add CLOUDFLARE_API_KEY, CLOUDFLARE_ZONE_ID and CLOUDFLARE_EMAIL to your Netlify environment variables.' | ||
async onEnd({ | ||
utils: { | ||
build: { failPlugin, failBuild }, | ||
}, | ||
}) { | ||
// Since calling utils.build.failBuild will not actually fail the build in onPreBuild, moved the conditions in onPreBuild. | ||
if( authMethod === 'na' ) { | ||
return failBuild( | ||
'Could not determine auth method. Please review the plugin README file and verify your environment variables' | ||
); | ||
} | ||
}, | ||
async onEnd({ | ||
inputs, | ||
utils: { | ||
build: { failPlugin, failBuild }, | ||
}, | ||
}) { | ||
else if( authMethod !== 'TOKEN' && authMethod !== 'KEY' ) { | ||
return failBuild( | ||
"'" + authMethod + "' is not a valid Authentication Method. Please report this issue to the developer." | ||
); | ||
} | ||
else { | ||
console.log('Cloudflare ' + authMethod + ' Authentication method detected.'); | ||
} | ||
console.log('Preparing to trigger Cloudflare cache purge'); | ||
let baseUrl = `https://api.cloudflare.com/client/v4/zones/${CLOUDFLARE_ZONE_ID}/purge_cache`; | ||
let headers = { | ||
'X-Auth-Email': CLOUDFLARE_EMAIL, | ||
'X-Auth-Key': CLOUDFLARE_API_KEY, | ||
'Content-Type': 'application/json', | ||
}; | ||
let headers; | ||
switch( authMethod ) { | ||
case 'TOKEN': | ||
headers = { | ||
'Authorization': 'Bearer ' + CLOUDFLARE_API_TOKEN, | ||
'Content-Type': 'application/json' | ||
}; | ||
break; | ||
case 'KEY': | ||
headers = { | ||
'X-Auth-Email': CLOUDFLARE_EMAIL, | ||
'X-Auth-Key': CLOUDFLARE_API_KEY, | ||
'Content-Type': 'application/json' | ||
}; | ||
break; | ||
} | ||
let body = { purge_everything: true }; | ||
@@ -39,6 +70,6 @@ | ||
return failPlugin( | ||
"Cloudflare cache couldn't be purged. Status: " + statusText | ||
"Cloudflare cache couldn't be purged. Status: " + status + " " + statusText | ||
); | ||
} | ||
console.log('Cloudflare cache purged!'); | ||
console.log('Cloudflare cache purged successfully!'); | ||
} catch (error) { | ||
@@ -45,0 +76,0 @@ return failBuild('Cloudflare cache purge failed', { error }); |
{ | ||
"name": "netlify-purge-cloudflare-on-deploy", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"description": "Automatically purge Cloudflare cache on Netlify deploy.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# netlify-purge-cloudflare-on-deploy | ||
Automatically purge Cloudflare cache on Netlify deploy. | ||
## NOTES: | ||
* Strictly this plugin triggers the cache purge before deploy (but after build), as this is the only functionality Netlify provides. | ||
* Cloudflare supports two methods of authentication. API TOKEN (Recommended) and API KEY (Legacy). | ||
* In the event the plugin finds both API Token and API Key environment variables, it will default to using API Token as this is the recommended method of authentication. | ||
## API TOKEN Authentication (Recommended, granular security controls) | ||
From Cloudflare, you'll need: | ||
* Your Zone ID. Go to your Cloudflare dashboard, enter your website, and look in the bottom right hand corner under 'API'. | ||
* An API Token | ||
#### API TOKEN - Instructions for creating new Cloudflare API Token | ||
1. In Cloudflare, navigate to My Profile --> API Tokens | ||
2. Click Create Token Button | ||
3. Click Custom Token Section --> Create Custom Token --> Get Started Button | ||
4. On the Create Custom Token Page: | ||
* Token Name --> Netlify Purge Cache Token (or anything other name that is meaningful to you) | ||
* Permissions --> Zone --> Cache Purge --> Purge | ||
* Click Continue to Summary button | ||
5. Click Create Token Button | ||
6. Record the API Token to your password safe of choice. This is the last time you will have an opportunity to see this token. | ||
#### API TOKEN - Instructions for configuring Netlify | ||
In Netlify, go to Settings -> Build & Deploy -> Environment -> Environment variables and set up: | ||
* CLOUDFLARE_ZONE_ID | ||
* CLOUDFLARE_API_TOKEN | ||
## API KEY Authentication (Legacy, full account access) | ||
It is recommended you use API Token authentication instead. Please see above for instructions. From Cloudflare, you'll need: | ||
* Your Cloudflare email address. | ||
@@ -9,11 +36,19 @@ * Your Zone ID. Go to your Cloudflare dashboard, enter your website, and look in the bottom right hand corner under 'API'. | ||
Strictly this plugin triggers the cache purge before deploy (but after build), as this is the only functionality Netlify provides. | ||
#### API KEY - Instructions for finding your Cloudflare API Key | ||
1. In Cloudflare, navigate to My Profile --> API Tokens | ||
2. Below the API Tokens section, you will see the API Keys section | ||
3. Click the View button to the right of Global API Key | ||
4. Authenticate again using your CLoudflare account password and hCaptcha | ||
5. Your API Key is revealed. No need to record this as you can access it anytime again in the future. | ||
#### API KEY - Instructions for configuring Netlify | ||
In Netlify, go to Settings -> Build & Deploy -> Environment -> Environment variables and set up: | ||
* CLOUDFLARE_EMAIL | ||
* CLOUDFLARE_ZONE_ID | ||
* CLOUDFLARE_API_KEY | ||
* CLOUDFLARE_EMAIL | ||
## Build failure with 'Bad request' | ||
Check you set up your environment variables properly. | ||
@@ -20,0 +55,0 @@ |
6796
69
59