Skip to content

Commit 7c24459

Browse files
authored
Add cache purge jsdelivr step to CI (#1243)
1 parent beff4b4 commit 7c24459

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"postinstall": "husky install",
2525
"changeset": "changeset",
2626
"update-versions-and-changelogs": "changeset version && yarn version-run-all && bash scripts/update-lockfile.sh",
27-
"release": "yarn clean && yarn build --force && changeset publish && git push origin HEAD:master --follow-tags --no-verify",
27+
"release": "yarn clean && yarn build --force && changeset publish && git push origin HEAD:master --follow-tags --no-verify && yarn scripts purge-cdn-cache",
2828
"version-run-all": "yarn workspaces foreach -vpt --no-private run version",
2929
"core": "yarn workspace @segment/analytics-core",
3030
"browser": "yarn workspace @segment/analytics-next",

scripts/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
".": "yarn run -T turbo run --filter=@internal/scripts",
77
"lint": "yarn concurrently 'yarn:eslint .' 'yarn:tsc --noEmit'",
88
"create-release-from-tags": "yarn ts-node-script --files create-release-from-tags/run.ts",
9+
"purge-cdn-cache": "yarn concurrently 'yarn:purge-cdn-cache:*'",
10+
"purge-cdn-cache:signals": "node purge-cdn-cache.js '@segment/analytics-signals' 'packages/signals/signals/dist/umd'",
11+
"purge-cdn-cache:consent": "node purge-cdn-cache.js '@segment/analytics-consent-wrapper-onetrust' 'packages/consent/consent-wrapper-onetrust/dist/umd'",
912
"test": "yarn jest",
1013
"tsc": "yarn run -T tsc",
1114
"eslint": "yarn run -T eslint",

scripts/purge-cdn-cache.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
const { execSync } = require('node:child_process')
2+
const fs = require('node:fs')
3+
const path = require('node:path')
4+
/**
5+
* Given a package name and a relative path to the dist folder -- it reads all the files and runs them through the jsdelivr cache purge API.
6+
* On CI, will abort the purge unless the current commit has a tag with the package name on it.
7+
*/
8+
const purgeJsDelivrCache = async (packageName, relativePath) => {
9+
console.log(`\n\n`)
10+
const gitRoot = path.resolve(__dirname, '..')
11+
const fullPath = path.join(gitRoot, relativePath)
12+
13+
if (!fs.existsSync(fullPath)) {
14+
console.error(`Path does not exist: ${fullPath}`)
15+
process.exit(1)
16+
}
17+
18+
// Only run this script if the given package has been published
19+
// Check if the current git HEAD has a tag containing the package name
20+
// This is a bit odd versus just having the cache purge on a prepublish hooks, but publish hooks like post/prepublish had been flaky on CI with changesets when I was setting this up.
21+
if (process.env.CI) {
22+
console.log('Searching for matching tag for package...')
23+
try {
24+
const tags = execSync('git tag --contains HEAD', {
25+
cwd: gitRoot,
26+
}).toString()
27+
28+
const hasMatchingTag = tags.includes(packageName)
29+
if (!hasMatchingTag) {
30+
console.log(
31+
`No tags containing the package name "${packageName}" found on the current git HEAD. Aborting cache purge.`
32+
)
33+
process.exit(0)
34+
}
35+
} catch (error) {
36+
console.error(`Failed to check git tags: ${error.message}`)
37+
process.exit(1)
38+
}
39+
}
40+
41+
const files = fs.readdirSync(fullPath)
42+
43+
console.log(
44+
`Purging files for ${packageName}: ${JSON.stringify(files, null, 2)}`
45+
)
46+
for (const file of files) {
47+
const filePath = path.join(relativePath, file)
48+
console.log(`Purging cache: ${file}...`)
49+
const url = `https://purge.jsdelivr.net/npm/${packageName}/${filePath}`
50+
51+
try {
52+
const response = await fetch(url)
53+
if (!response.ok) {
54+
console.error(`Failed to purge: ${url} - Status: ${response.status}`)
55+
}
56+
} catch (error) {
57+
console.error(`Failed to purge: ${url} - Error: ${error.message}`)
58+
}
59+
}
60+
console.log(`\nPurge of ${packageName} finished.`)
61+
}
62+
63+
const [packageName, relativePath] = process.argv.slice(2)
64+
65+
if (!packageName || !relativePath) {
66+
console.error('Usage: node purge-cdn-cache.js <package-name> <relative-path>')
67+
process.exit(1)
68+
}
69+
70+
purgeJsDelivrCache(packageName, relativePath)

0 commit comments

Comments
 (0)