Skip to content

Commit 01b6589

Browse files
committed
version v2.1.0
1 parent feeb820 commit 01b6589

File tree

651 files changed

+484
-212630
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

651 files changed

+484
-212630
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
package-lock.json

README.md

Lines changed: 109 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,94 @@
1-
# delete-workflow-runs v2
2-
The GitHub action to delete workflow runs in a repository. This action (written in JavaScript) wraps two Workflow Runs API:
3-
* [**List repository workflows**](https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#list-repository-workflows) -- Lists the workflows in a repository.
1+
# Delete Workflow Runs v2.1.0
42

5-
* [**List workflow runs**](https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#list-workflow-runs) -- List all workflow runs for a workflow.
3+
A GitHub Action to delete workflow runs in a repository. This Action uses JavaScript and interacts with the GitHub API to manage workflow runs efficiently.
64

7-
* [**Delete a workflow run**](https://docs.github.com/en/free-pro-team@latest/rest/reference/actions#delete-a-workflow-run) -- Delete a specific workflow run.
5+
## Features
86

9-
The action will calculate the number of days that each workflow run has been retained so far, then use this number to compare with the number you specify for the input parameter "[**`retain_days`**](#3-retain_days)". If the retention days of the workflow run has reached (equal to or greater than) the specified number, the workflow run will be deleted.
10-
11-
## What's new?
12-
* Added ability to match multiple values with "[**`delete_workflow_by_state_pattern`**](#6-delete_workflow_by_state_pattern)" & "[**`delete_run_by_conclusion_pattern`**](#7-delete_run_by_conclusion_pattern)" by using a comma-separated list
13-
* Removed 'all' option from "[**`delete_workflow_pattern`**](#5-delete_workflow_pattern)", simply don't provide it a value for all workflows to be targeted
14-
* Added the input parameter "[**`delete_run_by_conclusion_pattern`**](#7-delete_run_by_conclusion_pattern)" - filters runs by conclusion (useful for `skipped`!)
15-
* Added the input parameter "[**`delete_workflow_by_state_pattern`**](#6-delete_workflow_by_state_pattern)" - filters workflows by state
16-
* Added the input parameter "[**`dry_run`**](#8-dry_run)" - only logs targeted workflows & runs, no deletions are performed
17-
* Added ability to filter workflows by workflow filename (in addition to the name)
18-
##
7+
- Deletes workflow runs based on retention period and minimum runs to keep.
8+
- Supports filtering by workflow name, filename, state, or run conclusion.
9+
- Includes a dry-run mode to simulate deletions without making changes.
10+
- Skips runs linked to active branches or pull requests (optional).
11+
- Optimized to avoid uploading `node_modules` by bundling code with `@vercel/ncc`.
1912

2013
## Inputs
14+
2115
### 1. `token`
22-
#### Required: YES
23-
#### Default: `${{ github.token }}`
24-
The token used to authenticate.
25-
* If the workflow runs are in the current repository where the action is running, using **`github.token`** is OK, but you must specify additional permissions within your build job (or at a higher level) to allow the default token access to read the repository contents and to write (delete) action-related data, see the examples below. More details, see the [**`GITHUB_TOKEN`**](https://docs.github.com/en/free-pro-team@latest/actions/reference/authentication-in-a-workflow).
26-
* If the workflow runs are in another repository, you need to use a personal access token (PAT) that must have the **`repo`** scope. More details, see "[Creating a personal access token](https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/creating-a-personal-access-token)".
16+
17+
- **Required**: Yes
18+
- **Default**: `${{ github.token }}`
19+
- The GitHub token for authentication. Use `github.token` for the current repository (requires `actions: write` and `contents: read` permissions) or a Personal Access Token (PAT) with `repo` scope for other repositories.
2720

2821
### 2. `repository`
29-
#### Required: YES
30-
#### Default: `${{ github.repository }}`
31-
Name of the repository where the workflow runs are located
22+
23+
- **Required**: Yes
24+
- **Default**: `${{ github.repository }}`
25+
- The repository name in `{owner}/{repo}` format.
3226

3327
### 3. `retain_days`
34-
#### Required: YES
35-
#### Default: 30
36-
Amount of days used to compare with the retention days of each workflow
28+
29+
- **Required**: Yes
30+
- **Default**: `30`
31+
- Number of days to retain workflow runs before deletion.
3732

3833
### 4. `keep_minimum_runs`
39-
#### Required: YES
40-
#### Default: 6
41-
Minimum runs to keep for each workflow
34+
35+
- **Required**: Yes
36+
- **Default**: `6`
37+
- Minimum number of runs to keep per workflow.
4238

4339
### 5. `delete_workflow_pattern`
44-
#### Required: NO
45-
Name or filename of the workflow (if not set, all workflows are targeted)
40+
41+
- **Required**: No
42+
- Target workflows by name or filename. Omit to target all workflows.
4643

4744
### 6. `delete_workflow_by_state_pattern`
48-
#### Required: NO
49-
#### Default: 'ALL'
50-
Filter workflows by state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually
51-
_Multiple state values permitted as a comma-separated list_
45+
46+
- **Required**: No
47+
- Filter workflows by state (comma-separated): `active`, `deleted`, `disabled_fork`, `disabled_inactivity`, `disabled_manually`.
5248

5349
### 7. `delete_run_by_conclusion_pattern`
54-
#### Required: NO
55-
#### Default: 'ALL'
56-
Remove runs based on conclusion: action_required, cancelled, failure, skipped, success
57-
_Multiple conclusion values permitted as a comma-separated list_
50+
51+
- **Required**: No
52+
- Filter runs by conclusion (comma-separated): `action_required`, `cancelled`, `failure`, `skipped`, `success`.
5853

5954
### 8. `dry_run`
60-
#### Required: NO
61-
Logs simulated changes, no deletions are performed
62-
##
55+
56+
- **Required**: No
57+
- **Default**: `false`
58+
- Simulate deletions and log actions without performing them.
6359

6460
### 9. `check_branch_existence`
65-
#### Required: NO
66-
If true, the removage of a workflow is skipped, when a run is attached to a existing branch. Set to true avoids that check runs are deleted and the checks are not more present. (excludes main)
67-
##
61+
62+
- **Required**: No
63+
- **Default**: `false`
64+
- Skip deletion if the run is linked to an existing branch (excludes `main`).
6865

6966
### 10. `check_pullrequest_exist`
70-
#### Required: NO
71-
If true, the Runs will be checked for linkage to a PR.
72-
##
7367

68+
- **Required**: No
69+
- **Default**: `false`
70+
- Skip deletion if the run is linked to a pull request.
71+
72+
## Setup
73+
74+
1. Ensure the repository has a `package.json` with dependencies and a build script using `@vercel/ncc`.
75+
2. Run `npm install` and `npm run build` to generate `dist/index.js`.
76+
3. Commit the `dist/` folder, but exclude `node_modules/` using `.gitignore`.
77+
4. Use the Action in your workflow as shown below.
7478

7579
## Examples
76-
### In scheduled workflow, see [schedule event](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#schedule).
77-
> **Tip:** Using scheduled workflow is the recommended way that can periodically, automatically delete old workflow runs.
80+
81+
### Scheduled Workflow
82+
83+
Run monthly to delete old workflow runs:
84+
7885
```yaml
7986
name: Delete old workflow runs
8087
on:
8188
schedule:
82-
- cron: '0 0 1 * *'
83-
# Run monthly, at 00:00 on the 1st day of month.
84-
89+
- cron: "0 0 1 * *" # Monthly at 00:00 on the 1st
8590
jobs:
86-
del_runs:
91+
delete-runs:
8792
runs-on: ubuntu-latest
8893
permissions:
8994
actions: write
@@ -98,28 +103,29 @@ jobs:
98103
keep_minimum_runs: 6
99104
```
100105
101-
### In manual triggered workflow, see [workflow_dispatch event](https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#workflow_dispatch).
102-
> In this way, you can manually trigger the workflow at any time to delete old workflow runs. <br/>
103-
![manual workflow](img/example.PNG)
106+
### Manual Workflow
107+
108+
Trigger manually with customizable inputs:
109+
104110
```yaml
105111
name: Delete old workflow runs
106112
on:
107113
workflow_dispatch:
108114
inputs:
109115
days:
110-
description: 'Days-worth of runs to keep for each workflow'
116+
description: "Days to retain runs"
111117
required: true
112-
default: '30'
118+
default: "30"
113119
minimum_runs:
114-
description: 'Minimum runs to keep for each workflow'
120+
description: "Minimum runs to keep"
115121
required: true
116-
default: '6'
122+
default: "6"
117123
delete_workflow_pattern:
118-
description: 'Name or filename of the workflow (if not set, all workflows are targeted)'
124+
description: "Workflow name or filename (omit for all)"
119125
required: false
120126
delete_workflow_by_state_pattern:
121-
description: 'Filter workflows by state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually'
122-
required: true
127+
description: "Workflow state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually"
128+
required: false
123129
default: "ALL"
124130
type: choice
125131
options:
@@ -129,8 +135,8 @@ on:
129135
- disabled_inactivity
130136
- disabled_manually
131137
delete_run_by_conclusion_pattern:
132-
description: 'Remove runs based on conclusion: action_required, cancelled, failure, skipped, success'
133-
required: true
138+
description: "Run conclusion: action_required, cancelled, failure, skipped, success"
139+
required: false
134140
default: "ALL"
135141
type: choice
136142
options:
@@ -142,11 +148,15 @@ on:
142148
- skipped
143149
- success
144150
dry_run:
145-
description: 'Logs simulated changes, no deletions are performed'
151+
description: "Simulate deletions"
146152
required: false
147-
153+
default: "false"
154+
type: choice
155+
options:
156+
- "false"
157+
- "true"
148158
jobs:
149-
del_runs:
159+
delete-runs:
150160
runs-on: ubuntu-latest
151161
permissions:
152162
actions: write
@@ -163,36 +173,45 @@ jobs:
163173
delete_workflow_by_state_pattern: ${{ github.event.inputs.delete_workflow_by_state_pattern }}
164174
delete_run_by_conclusion_pattern: >-
165175
${{
166-
startsWith(github.event.inputs.delete_run_by_conclusion_pattern, 'Unsuccessful:')
167-
&& 'action_required,cancelled,failure,skipped'
168-
|| github.event.inputs.delete_run_by_conclusion_pattern
176+
startsWith(github.event.inputs.delete_run_by_conclusion_pattern, 'Unsuccessful:') &&
177+
'action_required,cancelled,failure,skipped' ||
178+
github.event.inputs.delete_run_by_conclusion_pattern
169179
}}
170180
dry_run: ${{ github.event.inputs.dry_run }}
171181
```
172182
173-
### Using with self-hosted git hub enterprise
174-
175-
If you're using this action in a GHE environment, the value you provide for `baseUrl` needs to be the precise and full URL.
176-
Consult the [documentation](https://docs.github.com/en/enterprise-server@3.14/rest/quickstart?apiVersion=2022-11-28) for your GHE instance to determine the correct URL to use.
183+
### GitHub Enterprise
177184
178-
For this example, let's say that you're running a modern version of GHE and it is accessible at `https://github.mycompany.com`.
179-
The API endpoint for your GHE instance will (probably) be `https://github.mycompany.com/api/v3`.
180-
So the resulting configuration would look like this:
185+
For GitHub Enterprise, specify the API base URL:
181186
182187
```yaml
183188
jobs:
184-
clean_up:
185-
# <...>
186-
steps:
187-
- name: Delete workflow runs
188-
uses: Mattraks/delete-workflow-runs@v2
189-
with:
190-
# <...>
191-
baseUrl: https://github.mycompany.com/api/v3
189+
delete-runs:
190+
runs-on: ubuntu-latest
191+
permissions:
192+
actions: write
193+
contents: read
194+
steps:
195+
- name: Delete old workflow runs
196+
uses: Mattraks/delete-workflow-runs@v2
197+
with:
198+
token: ${{ secrets.PAT_TOKEN }}
199+
baseUrl: https://github.mycompany.com/api/v3
200+
repository: mycompany/myrepo
201+
retain_days: 30
202+
keep_minimum_runs: 6
192203
```
193204
194-
##
205+
## Development
206+
207+
To build the Action:
208+
209+
1. Install dependencies: `npm install`
210+
2. Build the Action: `npm run build`
211+
3. Commit the `dist/` folder to the repository.
212+
213+
The `node_modules` folder is excluded via `.gitignore` to reduce repository size.
195214

196215
## License
197-
The scripts and documentation in this project are released under the [MIT License](LICENSE).
198-
##
216+
217+
This project is licensed under the [MIT License](LICENSE).

action.yaml

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,53 @@
1-
name: 'Delete workflow runs'
2-
description: 'An action to delete workflow runs in a repository.'
3-
author: 'Mattraks'
4-
1+
name: "Delete Workflow Runs"
2+
description: "GitHub Action to delete workflow runs in a repository."
3+
author: "Mattraks"
54
inputs:
65
token:
7-
description: 'Authentication token'
6+
description: "GitHub authentication token"
87
required: true
98
default: ${{ github.token }}
10-
119
baseUrl:
12-
description: 'Base API URL'
10+
description: "GitHub API base URL (for GitHub Enterprise)"
1311
required: false
1412
default: "https://api.github.com"
15-
1613
repository:
17-
description: 'Name of the repository'
14+
description: "Repository name in {owner}/{repo} format"
1815
required: true
1916
default: ${{ github.repository }}
20-
2117
retain_days:
22-
description: 'Days-worth of runs to keep for each workflow'
18+
description: "Number of days to retain workflow runs"
2319
required: true
24-
default: 30
25-
20+
default: "30"
2621
keep_minimum_runs:
27-
description: 'Minimum runs to keep for each workflow'
22+
description: "Minimum number of runs to keep per workflow"
2823
required: true
29-
default: 6
30-
24+
default: "6"
3125
delete_workflow_pattern:
32-
description: 'Name or filename of the workflow (if not set, all workflows are targeted)'
26+
description: "Workflow name or filename pattern (omit for all)"
3327
required: false
34-
3528
delete_workflow_by_state_pattern:
36-
description: 'Filter workflows by state: active, deleted, disabled_fork, disabled_inactivity, disabled_manually'
29+
description: "Workflow states to target (comma-separated or ALL)"
3730
required: false
38-
31+
default: "ALL"
3932
delete_run_by_conclusion_pattern:
40-
description: 'Remove runs based on conclusion: action_required, cancelled, failure, skipped, success'
33+
description: "Run conclusions to target (comma-separated or ALL)"
4134
required: false
42-
35+
default: "ALL"
4336
dry_run:
44-
description: 'Logs simulated changes, no deletions are performed'
37+
description: "Simulate deletions without performing them"
4538
required: false
46-
39+
default: "false"
4740
check_branch_existence:
48-
description: 'If the workflow was triggered by a branch, the branch must be deleted before the workflow run is deleted as well.'
41+
description: "Skip runs from existing branches (excludes main)"
4942
required: false
50-
43+
default: "false"
5144
check_pullrequest_exist:
52-
description: "If the run is linked to a pull request, the deletion is skipped."
45+
description: "Skip runs linked to pull requests"
5346
required: false
54-
55-
runs:
56-
# with node16, I got this error:
57-
# fetch is not set. Please pass a fetch implementation as new Octokit({ request: { fetch }}).
58-
# Learn more at https://github.com/octokit/octokit.js/#fetch-missing
59-
using: 'node20'
60-
main: 'dist/index.js'
61-
47+
default: "false"
6248
branding:
63-
icon: 'delete'
64-
color: 'red'
49+
icon: "trash"
50+
color: "red"
51+
runs:
52+
using: "node20"
53+
main: "dist/index.js"

0 commit comments

Comments
 (0)