|
| 1 | +name: '🧰 Maint: Delete workflow runs' |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + validity-days: |
| 7 | + description: 'Validity days' |
| 8 | + default: '7' |
| 9 | + type: choice |
| 10 | + options: ['0', '1', '2', '3', '4', '5', '6', '7'] |
| 11 | + |
| 12 | + name-pattern: |
| 13 | + description: 'Regular expression to match workflow name' |
| 14 | + required: false |
| 15 | + default: '' |
| 16 | + type: string |
| 17 | + |
| 18 | + dry-run: |
| 19 | + required: false |
| 20 | + default: true |
| 21 | + type: boolean |
| 22 | + |
| 23 | +jobs: |
| 24 | + main: |
| 25 | + permissions: |
| 26 | + actions: write |
| 27 | + |
| 28 | + runs-on: ubuntu-latest |
| 29 | + steps: |
| 30 | + - name: Delete workflow runs |
| 31 | + uses: actions/github-script@v6 |
| 32 | + with: |
| 33 | + script: | |
| 34 | + core.info('validity-days: ${{ inputs.validity-days }}'); |
| 35 | + core.info('name-pattern: ${{ inputs.name-pattern }}'); |
| 36 | + core.info('dry-run: ${{ inputs.dry-run }}'); |
| 37 | +
|
| 38 | + let deleteWorkflowRun; |
| 39 | + if (${{ inputs.dry-run }} == true) { |
| 40 | + core.warning('Running in dry-run mode.'); |
| 41 | + deleteWorkflowRun = (owner, repo, run_id) => undefined; |
| 42 | + } else { |
| 43 | + deleteWorkflowRun = github.rest.actions.deleteWorkflowRun; |
| 44 | + } |
| 45 | +
|
| 46 | + const validity_days = parseInt('${{ inputs.validity-days }}', 10); |
| 47 | + const re = new RegExp('${{ inputs.name-pattern }}'); |
| 48 | + const current_date = new Date(); |
| 49 | + const per_page = 30; |
| 50 | +
|
| 51 | + async function numWorkflowRuns(workflow_id) { |
| 52 | + let total_count = 0 |
| 53 | + try { |
| 54 | + const wf_runs = await github.rest.actions.listWorkflowRuns({ |
| 55 | + owner: context.repo.owner, |
| 56 | + repo: context.repo.repo, |
| 57 | + workflow_id: workflow_id, |
| 58 | + per_page: 1, |
| 59 | + }); |
| 60 | + total_count = wf_runs.data.total_count; |
| 61 | + } catch (e) { |
| 62 | + } |
| 63 | + return total_count; |
| 64 | + } |
| 65 | +
|
| 66 | + async function validWorkflowRunIds(workflow_id, num_wf_runs) { |
| 67 | + const max_pages = Math.ceil(num_wf_runs / per_page); |
| 68 | + let ids = [] |
| 69 | + try { |
| 70 | + let num_pages = 1; |
| 71 | + do { |
| 72 | + const wf_runs = await github.rest.actions.listWorkflowRuns({ |
| 73 | + owner: context.repo.owner, |
| 74 | + repo: context.repo.repo, |
| 75 | + workflow_id: workflow_id, |
| 76 | + per_page: per_page, |
| 77 | + page: num_pages, |
| 78 | + }); |
| 79 | + for (const wf_run of wf_runs.data.workflow_runs) { |
| 80 | + if (wf_run.status != 'completed') { |
| 81 | + continue; |
| 82 | + } |
| 83 | +
|
| 84 | + const expiry_date = new Date(wf_run.updated_at); |
| 85 | + expiry_date.setDate(expiry_date.getDate() + validity_days); |
| 86 | + const has_expired = current_date.getTime() > expiry_date.getTime(); |
| 87 | + if (!has_expired) { |
| 88 | + continue; |
| 89 | + } |
| 90 | +
|
| 91 | + ids.push(wf_run.id); |
| 92 | + } |
| 93 | + } while(num_pages++ < max_pages); |
| 94 | + } catch (e) { |
| 95 | + } |
| 96 | + return ids; |
| 97 | + } |
| 98 | +
|
| 99 | + const wfs = await github.rest.actions.listRepoWorkflows({ |
| 100 | + owner: context.repo.owner, |
| 101 | + repo: context.repo.repo, |
| 102 | + }); |
| 103 | + for (const wf of wfs.data.workflows) { |
| 104 | + const basename = wf.path.split('/').reverse()[0]; |
| 105 | + const has_matched = re.test(basename); |
| 106 | + if (!has_matched) { |
| 107 | + continue; |
| 108 | + } |
| 109 | +
|
| 110 | + let num_deleted_wf_runs = 0; |
| 111 | + const num_wf_runs = await numWorkflowRuns(wf.id); |
| 112 | + if (!num_wf_runs) { |
| 113 | + core.info(`[${basename}] This workflow has no runs yet.`); |
| 114 | + continue; |
| 115 | + } |
| 116 | +
|
| 117 | + const wf_run_ids = await validWorkflowRunIds(wf.id, num_wf_runs); |
| 118 | + if (!wf_run_ids.length) { |
| 119 | + core.info(`[${basename}] Nothing matches the given conditions.`); |
| 120 | + continue; |
| 121 | + } |
| 122 | +
|
| 123 | + for (const wf_run_id of wf_run_ids) { |
| 124 | + deleteWorkflowRun({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + run_id: wf_run_id, |
| 128 | + }); |
| 129 | + num_deleted_wf_runs++; |
| 130 | + } |
| 131 | + core.notice(`[${basename}] ${num_deleted_wf_runs}/${num_wf_runs} workflow runs have been deleted.`); |
| 132 | + } |
0 commit comments