@@ -27,15 +27,45 @@ async function uploadFile(localPath, remotePath) {
2727
2828async function purgeCache ( ) {
2929 const purgePath = DEPLOY_PATH ? `${ PULLZONE_URL } /${ DEPLOY_PATH } /*` : `${ PULLZONE_URL } /*` ;
30- const res = await fetch ( `https://api.bunny.net/purge?url=${ encodeURIComponent ( purgePath ) } ` , {
31- method : 'POST' ,
32- headers : { AccessKey : API_KEY } ,
33- } ) ;
34- if ( ! res . ok ) {
35- const text = await res . text ( ) ;
36- throw new Error ( `Purge failed: ${ res . status } - ${ text } ` ) ;
30+ const url = `https://api.bunny.net/purge?url=${ encodeURIComponent ( purgePath ) } ` ;
31+ const maxAttempts = 5 ;
32+
33+ for ( let attempt = 1 ; attempt <= maxAttempts ; attempt ++ ) {
34+ try {
35+ const controller = new AbortController ( ) ;
36+ const timeout = setTimeout ( ( ) => controller . abort ( ) , 30_000 ) ;
37+ const res = await fetch ( url , {
38+ method : 'POST' ,
39+ headers : { AccessKey : API_KEY } ,
40+ signal : controller . signal ,
41+ } ) ;
42+ clearTimeout ( timeout ) ;
43+
44+ if ( res . ok ) {
45+ console . log ( '✓ Cache purged' ) ;
46+ return ;
47+ }
48+
49+ // Retry on 5xx and 429; fail fast on other 4xx (auth, bad URL, etc.)
50+ if ( res . status < 500 && res . status !== 429 ) {
51+ const text = await res . text ( ) ;
52+ throw new Error ( `Purge failed: ${ res . status } - ${ text . slice ( 0 , 200 ) } ` ) ;
53+ }
54+ console . warn ( `Purge attempt ${ attempt } /${ maxAttempts } failed: ${ res . status } ` ) ;
55+ } catch ( err ) {
56+ if ( err . message ?. startsWith ( 'Purge failed:' ) ) throw err ;
57+ console . warn ( `Purge attempt ${ attempt } /${ maxAttempts } error: ${ err . message } ` ) ;
58+ }
59+
60+ if ( attempt < maxAttempts ) {
61+ const delay = Math . min ( 2000 * 2 ** ( attempt - 1 ) , 30_000 ) ;
62+ await new Promise ( ( r ) => setTimeout ( r , delay ) ) ;
63+ }
3764 }
38- console . log ( '✓ Cache purged' ) ;
65+
66+ // Upload already succeeded; a failed purge only delays cache eviction.
67+ // Don't fail the deploy — warn loudly so the job stays green.
68+ console . warn ( '::warning::Cache purge failed after retries — new content will propagate as edge TTLs expire.' ) ;
3969}
4070
4171async function deploy ( ) {
0 commit comments