-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8736 from hicommonwealth/ka.cloudflarePurgeCache
Added script to automate cloudflare cache purge
- Loading branch information
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
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
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
41 changes: 41 additions & 0 deletions
41
packages/commonwealth/server/scripts/purgeCloudflareCache.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { logger } from '@hicommonwealth/core'; | ||
import fetch from 'node-fetch'; | ||
import { config } from '../config'; | ||
|
||
const log = logger(import.meta); | ||
|
||
async function purgeCache(zoneId?: string, apiKey?: string) { | ||
if (!zoneId || !apiKey) { | ||
log.warn('Missing Cloudflare env variables. Skipping cache purge'); | ||
return; | ||
} | ||
|
||
const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/purge_cache`; | ||
const body = { | ||
purge_everything: true, | ||
}; | ||
const headers = { | ||
Authorization: `Bearer ${apiKey}`, | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: headers, | ||
body: JSON.stringify(body), | ||
}); | ||
|
||
const responseData = await response.json(); | ||
log.info('Cache purge request successful:'); | ||
log.info(responseData); | ||
} catch (error) { | ||
log.error('Error purging cache:', error.message); | ||
} | ||
} | ||
|
||
purgeCache(config.CLOUDFLARE.ZONE_ID, config.CLOUDFLARE.API_KEY) | ||
.then(() => log.info('finished cloudflare purge script')) | ||
.catch((e) => { | ||
log.error('cloudflare purge script failed:', e); | ||
}); |
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