Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (135 loc) · 3.92 KB
/
Copy pathindex.js
File metadata and controls
157 lines (135 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const { ofetch } = require("ofetch");
const { backOff } = require("exponential-backoff");
const CF_API_TOKEN = process.env.CF_API_TOKEN;
const CF_ACCOUNT_ID = process.env.CF_ACCOUNT_ID;
const CF_DELETE_ALIASED_DEPLOYMENTS = process.env.CF_DELETE_ALIASED_DEPLOYMENTS;
const CF_PAGES_PROJECT_NAME = process.argv[2];
const MAX_ATTEMPTS = 5;
const sleep = (ms) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
const headers = {
Authorization: `Bearer ${CF_API_TOKEN}`,
};
/** Get the cononical deployment (the live deployment) */
async function getProductionDeploymentId() {
const body = await ofetch(
`https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/pages/projects/${CF_PAGES_PROJECT_NAME}`,
{
method: "GET",
headers,
parseResponse: JSON.parse,
},
);
if (!body.success) {
throw new Error(body.errors[0].message);
}
const prodDeploymentId = body.result.canonical_deployment.id;
if (!prodDeploymentId) {
throw new Error("Unable to ofetch production deployment ID");
}
return prodDeploymentId;
}
async function deleteDeployment(id) {
let params = "";
if (CF_DELETE_ALIASED_DEPLOYMENTS === "true") {
params = "?force=true"; // Forces deletion of aliased deployments
}
const body = await ofetch(
`https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/pages/projects/${CF_PAGES_PROJECT_NAME}/deployments/${id}${params}`,
{
method: "DELETE",
headers,
parseResponse: JSON.parse,
},
);
if (!body.success) {
throw new Error(body.errors[0].message);
}
console.log(`Deleted deployment ${id} for project ${CF_PAGES_PROJECT_NAME}`);
}
async function listDeploymentsPerPage(page) {
const body = await ofetch(
`https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/pages/projects/${CF_PAGES_PROJECT_NAME}/deployments?per_page=10&page=${page}`,
{
method: "GET",
headers,
parseResponse: JSON.parse,
},
);
if (!body.success) {
throw new Error(
`Could not ofetch deployments for ${CF_PAGES_PROJECT_NAME}`,
);
}
return body.result;
}
async function listAllDeployments() {
let page = 1;
const deploymentIds = [];
while (true) {
let result;
try {
result = await backOff(() => listDeploymentsPerPage(page), {
numOfAttempts: 5,
startingDelay: 1000, // 1s, 2s, 4s, 8s, 16s
retry: (_, attempt) => {
console.warn(
`Failed to list deployments on page ${page}... retrying (${attempt}/${MAX_ATTEMPTS})`,
);
return true;
},
});
} catch (err) {
console.warn(`Failed to list deployments on page ${page}.`);
console.warn(err);
process.exit(1);
}
for (const deployment of result) {
deploymentIds.push(deployment.id);
}
if (result.length) {
page = page + 1;
await sleep(500);
} else {
return deploymentIds;
}
}
}
async function main() {
if (!CF_API_TOKEN) {
throw new Error(
"Please set CF_API_TOKEN as an env variable to your API Token",
);
}
if (!CF_ACCOUNT_ID) {
throw new Error(
"Please set CF_ACCOUNT_ID as an env variable to your Account ID",
);
}
if (!CF_PAGES_PROJECT_NAME) {
throw new Error(
"Please set CF_PAGES_PROJECT_NAME as an env variable to your Pages project name",
);
}
const productionDeploymentId = await getProductionDeploymentId();
console.log(
`Found live production deployment to exclude from deletion: ${productionDeploymentId}`,
);
console.log("Listing all deployments, this may take a while...");
const deploymentIds = await listAllDeployments();
for (id of deploymentIds) {
if (id === productionDeploymentId) {
console.log(`Skipping production deployment: ${id}`);
} else {
try {
await deleteDeployment(id);
await sleep(500);
} catch (error) {
console.log(error);
}
}
}
}
main();