Last active
January 7, 2023 17:48
-
-
Save Erisa/2b666fa729619c226a44f7765e0d4872 to your computer and use it in GitHub Desktop.
Repeat for clearing out Pages deployments (https://repeat.dev)
This file contains hidden or 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
const config = [ | |
{ | |
project: "erisa", | |
days: 180 | |
}, | |
{ | |
project: "super-secret-sauce", | |
days: 60 | |
} | |
] | |
export default { | |
async cron(cron: Repeat.Cron, env: Repeat.Env): Promise<void> { | |
for (let job of config) { | |
await env.unstable.tasks.add(job); | |
} | |
}, | |
async task(task: Repeat.Task, env: Repeat.Env) { | |
console.log(task.data); | |
const data = JSON.parse(task.data); | |
const endpoint = `https://api.cloudflare.com/client/v4/accounts/${env.variables.ACCOUNT_ID}/pages/projects/${data['project']}/deployments`; | |
const expirationDays = parseInt(data['days']); | |
try { | |
const init = { | |
headers: { | |
'Content-Type': 'application/json;charset=UTF-8', | |
'Authorization': env.variables.API_TOKEN, | |
}, | |
}; | |
let response: any; | |
let deployments: any; | |
response = await fetch(endpoint, init); | |
deployments = await response.json(); | |
const pages: number = Math.ceil(deployments.result_info.total_count / deployments.result_info.per_page); | |
console.log(pages); | |
for (let page = pages; page > 1; page--) { | |
console.log(page); | |
let newEndpoint = endpoint + `?page=${page}`; | |
response = await fetch(newEndpoint, init); | |
deployments = await response.json(); | |
await clearOldDeploys(deployments, expirationDays, env, endpoint, data['project']); | |
} | |
} catch (e) { | |
// log error | |
console.error('cron failed!', e.message); | |
} | |
}, | |
}; | |
async function clearOldDeploys( | |
deployments: any, | |
expirationDays: number, | |
env: Repeat.Env, | |
endpoint: string, | |
label: string | |
) { | |
// console.log(deployments) | |
let count = 0; | |
let promises = []; | |
for (const deployment of deployments.result) { | |
if ((Date.now() - new Date(deployment.created_on).getTime()) / 86400000 > expirationDays) { | |
// Delete the deployment | |
promises.push(async () => { | |
let resp = await fetch(`${endpoint}/${deployment.id}`, { | |
method: 'DELETE', | |
headers: { | |
'Content-Type': 'application/json;charset=UTF-8', | |
'Authorization': env.variables.API_TOKEN, | |
}, | |
}); | |
if (resp.status === 200) { | |
console.log(`deleted ${deployment.id} (${new Date(deployment.created_on).getTime()})`); | |
count += 1; | |
} else { | |
console.log(`${resp.status}: ${await resp.text()}`); | |
} | |
}); | |
} | |
} | |
// Thank you James! | |
while (promises.length > 0) { | |
await Promise.allSettled(promises.splice(0, 4).map(f => f())); | |
} | |
env.metrics.write('deployments_deleted', count, label); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment