Skip to content

Commit e777549

Browse files
committed
Remove pages-build-deployment workflow runs except the most recent one
1 parent e982879 commit e777549

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

.github/workflows/clean-workflows.yml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ jobs:
197197
}
198198
console.log("✅ Cleanup complete.");
199199
200-
- name: ✂ Remove pages-build-deployment (gh-pages) workflow runs
200+
- name: ✂ Remove pages-build-deployment workflow runs except the most recent one
201201
uses: actions/github-script@v8 # https://github.com/actions/github-script
202202
with:
203203
github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -218,22 +218,28 @@ jobs:
218218
}
219219
const workflowId = workflow.id;
220220
console.log(`✅ Found workflow ID: ${workflowId}`);
221+
let isFirstRun = true;
221222
// Fetch workflow runs
222-
for (let page = 0; page < pages; page += 1) {
223+
for (let page = 1; page <= pages; page++) {
223224
const response = await github.rest.actions.listWorkflowRuns({
224225
owner: context.repo.owner,
225226
repo: context.repo.repo,
226-
workflow_id: workflowId, // Use dynamically fetched ID
227+
workflow_id: workflowId,
227228
page: page,
228229
per_page: 50,
229230
});
230-
if (response.data.workflow_runs.length === 0) break;
231-
if (response.data.workflow_runs.length > 0) {
232-
for (const run of response.data.workflow_runs) {
233-
runs_to_delete.push([run.id, run.name]);
231+
const runs = response.data.workflow_runs;
232+
if (runs.length === 0) break;
233+
for (const run of runs) {
234+
// Skip the newest run
235+
if (isFirstRun) {
236+
console.log(`⏭ Keeping latest run ${run.id}`);
237+
isFirstRun = false;
238+
continue;
234239
}
240+
runs_to_delete.push([run.id, run.name]);
235241
}
236-
if (response.data.workflow_runs.length < 50) break;
242+
if (runs.length < 50) break;
237243
}
238244
// Delete workflow runs
239245
for (const run of runs_to_delete) {

0 commit comments

Comments
 (0)