├── .gitignore
├── README.md
├── action.yml
├── LICENSE
└── del.sh
/.gitignore:
--------------------------------------------------------------------------------
1 | !.gitignore
2 | .DS_Store
3 | npm-debug.log*
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Function description / 功能说明
2 |
3 | This Actions can delete specified repository's Releases and Workflow run logs.
4 |
5 | 这个 Actions 可以删除指定仓库的 Releases 和 Workflows 运行记录。
6 |
7 | ## Instructions / 使用说明
8 |
9 | You can use this Actions by introducing it in the `.github/workflows/*.yml` workflow script, such as in [delete.yml](https://github.com/ophub/amlogic-s9xxx-armbian/blob/main/.github/workflows/delete-older-releases-workflows.yml).
10 |
11 | 在 `.github/workflows/*.yml` 工作流脚本中引入此 Actions 即可使用,例如 [delete.yml](https://github.com/ophub/amlogic-s9xxx-armbian/blob/main/.github/workflows/delete-older-releases-workflows.yml)。
12 |
13 | ```yaml
14 | - name: Delete releases and workflows runs
15 | uses: ophub/delete-releases-workflows@main
16 | with:
17 | delete_releases: true
18 | releases_keep_latest: 5
19 | delete_workflows: true
20 | workflows_keep_day: 10
21 | gh_token: ${{ secrets.GITHUB_TOKEN }}
22 | ```
23 |
24 | ## Setting instructions / 设置说明
25 |
26 | You can configure the following options in the delete.yml file:
27 |
28 | 您可以在 delete.yml 文件中使用以下选项进行配置:
29 |
30 | | Key / 选项 | Required | Description / 说明 |
31 | | ----------------------- | ---------- | ---------------------------------------- |
32 | | delete_releases | `Required`
`必选项` | Set whether to delete releases files (options: `true`/`false`). The default is `false`.
设置是否删除 releases 文件(选项:`true`/`false`),默认为 `false`。 |
33 | | prerelease_option | Optional
可选项 | Set whether to differentiate pre-release versions (options: `all`/`true`/`false`). `all` indicates all types, `true`/`false` represent only deleting releases files marked with this type. The default is `all`.
设置是否区分预发行版本(选项:`all`/`true`/`false`)。`all`表示全部类型,`true`/`false`代表仅删除标记为此类型的 releases 文件。默认为 `all`。 |
34 | | releases_keep_latest | Optional
可选项 | Set how many of the latest Releases versions to keep (`integer`, such as: 5). Setting to `0` means delete all, and the default is to keep `90`.
设置保留几个最新的 Releases 版本(`整数`。如:5),设置为 `0` 表示全部删除,默认保留 `90` 个。 |
35 | | releases_keep_keyword | Optional
可选项 | Set the `keywords` of the Releases' tags to be preserved. Multiple keywords are separated by `/` (for example: `book/tool`). The default value is `none`.
设置需要保留的 Releases 的 tags `关键字`,多个关键字使用 `/` 分割(例如:`book/tool`),默认值 `无`。 |
36 | | delete_tags | Optional
可选项 | Set whether to delete the tags associated with Releases (options: `true`/`false`). The default is `false`.
设置是否删除与 Releases 关联的 tags(选项:`true`/`false`),默认为 `false`。 |
37 | | delete_workflows | `Required`
`必选项` | Set whether to delete workflow run logs (options: `true`/`false`). The default is `false`.
设置是否删除 workflows 运行记录(选项:`true`/`false`),默认为 `false`。 |
38 | | workflows_keep_day | Optional
可选项 | Set how many days' workflow logs to keep (`integer`, such as: 30). Setting to `0` means delete all. The default is `90` days.
设置保留几天以内的 workflows 记录(`整数`。如:30),设置为 `0` 表示全部删除。默认为 `90` 天。 |
39 | | workflows_keep_keyword | Optional
可选项 | Set the `keywords` for the names of the workflow run logs to be kept. Multiple keywords are separated by `/` (for example: `book/tool`). The default value is `none`.
设置需要保留的 workflows 运行记录的名称`关键字`,多个关键字使用 `/` 分割(例如:`book/tool`),默认值 `无`。 |
40 | | out_log | Optional
可选项 | Set whether to output detailed json logs (options: `true`/`false`). The default value is `false`.
设置是否输出详细的 json 日志(选项:`true`/`false`),默认值 `false`。 |
41 | | repo | Optional
可选项 | Set the `/` for the execution operation, the default is the `current repository`.
设置执行操作的 `/` ,默认为`当前仓库`。 |
42 | | gh_token | `Required`
`必选项` | Set the [GITHUB_TOKEN](https://docs.github.com/en/actions/security-guides/automatic-token-authentication) password for executing the delete operation.
设置执行删除操作的 [GITHUB_TOKEN](https://docs.github.com/zh/actions/security-guides/automatic-token-authentication#about-the-github_token-secret) 口令。 |
43 |
44 | - Each run can delete up to 1000 Releases and 1000 Workflow run logs. If there are more records, the delete operation needs to be run multiple times.
45 | - 每次运行可以删除 1000 个 Releases 和 1000 个 Workflows 运行记录,如有更多记录,需要多次运行删除操作。
46 |
47 | ## Links / 链接
48 |
49 | - [GitHub Docs](https://docs.github.com/en/rest/releases/releases?list-releases)
50 | - [unifreq/openwrt_packit](https://github.com/unifreq/openwrt_packit)
51 | - [amlogic-s9xxx-armbian](https://github.com/ophub/amlogic-s9xxx-armbian)
52 | - [amlogic-s9xxx-openwrt](https://github.com/ophub/amlogic-s9xxx-openwrt)
53 | - [flippy-openwrt-actions](https://github.com/ophub/flippy-openwrt-actions)
54 |
55 | ## License / 许可
56 |
57 | The delete-releases-workflows © OPHUB is licensed under [GPL-2.0](https://github.com/ophub/delete-releases-workflows/blob/main/LICENSE)
58 |
--------------------------------------------------------------------------------
/action.yml:
--------------------------------------------------------------------------------
1 | name: "delete-releases-workflows"
2 | author: "ophub"
3 | description: "Delete older releases and workflow runs"
4 | inputs:
5 | repo:
6 | description: "Repo name in the form of /"
7 | required: false
8 | default: "${{ github.repository }}"
9 | delete_releases:
10 | description: "Whether to delete releases records"
11 | required: true
12 | default: "false"
13 | delete_tags:
14 | description: "Whether to delete associated tags"
15 | required: false
16 | default: "false"
17 | prerelease_option:
18 | description: "Whether to differentiate pre-release"
19 | required: false
20 | default: "all"
21 | releases_keep_latest:
22 | description: "How many latest releases to keep"
23 | required: false
24 | default: "90"
25 | releases_keep_keyword:
26 | description: "Keyword of the keep releases"
27 | required: false
28 | default: ""
29 | delete_workflows:
30 | description: "Whether to delete workflows records"
31 | required: true
32 | default: "false"
33 | workflows_keep_day:
34 | description: "Days to keep workflows"
35 | required: false
36 | default: "90"
37 | workflows_keep_keyword:
38 | description: "keywords for keep workflows"
39 | required: false
40 | default: ""
41 | out_log:
42 | description: "Output a list log for each step"
43 | required: false
44 | default: "false"
45 | gh_token:
46 | description: "Set the token"
47 | required: true
48 | default: ""
49 |
50 | runs:
51 | using: "composite"
52 | steps:
53 | - shell: bash
54 | run: |
55 | #======================================= Define common functions =======================================
56 | #
57 | # Error color
58 | ERROR="[\033[91m ERROR \033[0m]"
59 | error_msg() {
60 | echo -e "${ERROR} ${1}"
61 | exit 1
62 | }
63 | #
64 | # Validation Boolean type function
65 | validate_boolean() {
66 | local var="${1}" param_name="${2}"
67 | if [[ ! "${var}" =~ ^(true|false)$ ]]; then
68 | error_msg "Invalid value for $param_name: ${var} must be 'true' or 'false'"
69 | fi
70 | }
71 | #
72 | # Verify integer function
73 | validate_integer() {
74 | local var="${1}" param_name="${2}"
75 | if [[ ! "${var}" =~ ^(0|[1-9][0-9]*)$ ]]; then
76 | error_msg "Invalid value for $param_name: ${var} must be a positive integer"
77 | fi
78 | }
79 | #
80 | # Verify pre-release options function
81 | validate_prerelease() {
82 | local var="${1}" param_name="${2}"
83 | if [[ ! "${var}" =~ ^(true|false|all)$ ]]; then
84 | error_msg "Invalid value for $param_name: ${var} must be 'true', 'false', or 'all'."
85 | fi
86 | }
87 | #
88 | #====================================== Validate input parameters ======================================
89 | #
90 | # Print the current action path and workflow run ID
91 | cd ${{ github.action_path }}
92 | echo -e "Current Actions path: ${PWD}"
93 | echo -e "Current workflow run_id: ${GITHUB_RUN_ID}"
94 |
95 | # Receive input parameters related to releases
96 | delete_releases="${{ inputs.delete_releases }}"
97 | prerelease_option="${{ inputs.prerelease_option }}"
98 | releases_keep_latest="${{ inputs.releases_keep_latest }}"
99 | releases_keep_keyword="${{ inputs.releases_keep_keyword }}"
100 | delete_tags="${{ inputs.delete_tags }}"
101 | #
102 | # Receive input parameters related to workflows
103 | delete_workflows="${{ inputs.delete_workflows }}"
104 | workflows_keep_day="${{ inputs.workflows_keep_day }}"
105 | workflows_keep_keyword="${{ inputs.workflows_keep_keyword }}"
106 | #
107 | # Receive input parameters related to system environment
108 | out_log="${{ inputs.out_log }}"
109 | repo="${{ inputs.repo }}"
110 | gh_token="${{ inputs.gh_token }}"
111 |
112 | # Verify releases parameters
113 | [[ -z "${delete_releases}" ]] && error_msg "Please set whether to delete releases."
114 | [[ -n "${delete_releases}" ]] && validate_boolean "${delete_releases}" "delete_releases"
115 | [[ -n "${prerelease_option}" ]] && validate_prerelease "${prerelease_option}" "prerelease_option"
116 | [[ -n "${releases_keep_latest}" ]] && validate_integer "${releases_keep_latest}" "releases_keep_latest"
117 | [[ -n "${delete_tags}" ]] && validate_boolean "${delete_tags}" "delete_tags"
118 | #
119 | # Verify workflows parameters
120 | [[ -z "${delete_workflows}" ]] && error_msg "Please set whether to delete workflows."
121 | [[ -n "${delete_workflows}" ]] && validate_boolean "${delete_workflows}" "delete_workflows"
122 | [[ -n "${workflows_keep_day}" ]] && validate_integer "${workflows_keep_day}" "workflows_keep_day"
123 | #
124 | # Validate system environment parameters
125 | [[ -n "${out_log}" ]] && validate_boolean "${out_log}" "out_log"
126 | [[ -z "${gh_token}" ]] && error_msg "Please set the token: [ gh_token ]."
127 | #
128 | #===================================== Build command line arguments =====================================
129 | #
130 | # Initialize command line arguments
131 | make_command=""
132 | #
133 | # Build command line arguments for releases
134 | [[ -n "${delete_releases}" ]] && make_command="${make_command} -a ${delete_releases}"
135 | [[ -n "${prerelease_option}" ]] && make_command="${make_command} -p ${prerelease_option}"
136 | [[ -n "${releases_keep_latest}" ]] && make_command="${make_command} -l ${releases_keep_latest}"
137 | [[ -n "${releases_keep_keyword}" ]] && make_command="${make_command} -w ${releases_keep_keyword}"
138 | [[ -n "${delete_tags}" ]] && make_command="${make_command} -t ${delete_tags}"
139 | #
140 | # Build command line arguments for workflows
141 | [[ -n "${delete_workflows}" ]] && make_command="${make_command} -s ${delete_workflows}"
142 | [[ -n "${workflows_keep_day}" ]] && make_command="${make_command} -d ${workflows_keep_day}"
143 | [[ -n "${workflows_keep_keyword}" ]] && make_command="${make_command} -k ${workflows_keep_keyword}"
144 | #
145 | # Build command line arguments for system environment
146 | [[ -n "${out_log}" ]] && make_command="${make_command} -o ${out_log}"
147 | [[ -n "${repo}" ]] && make_command="${make_command} -r ${repo}"
148 | [[ -n "${gh_token}" ]] && make_command="${make_command} -g ${gh_token}"
149 | #
150 | # Execute the deletion script
151 | chmod +x del.sh
152 | ./del.sh ${make_command}
153 | #
154 | #=========================================== End of script =============================================
155 |
156 | branding:
157 | icon: "terminal"
158 | color: "gray-dark"
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 |
294 | Copyright (C)
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | , 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
--------------------------------------------------------------------------------
/del.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | #==============================================================================================
3 | #
4 | # Function: Delete older releases and workflow runs
5 | # Copyright (C) 2023- https://github.com/ophub/delete-releases-workflows
6 | # Use api.github.com official documentation
7 | # https://docs.github.com/en/rest/releases/releases?list-releases
8 | # https://docs.github.com/en/rest/actions/workflow-runs?list-workflow-runs-for-a-repository
9 | #
10 | #======================================= Functions list =======================================
11 | #
12 | # error_msg : Output error message
13 | # init_var : Initialize all variables
14 | #
15 | # get_releases_list : Get the release list
16 | # out_releases_list : Output the release list
17 | # del_releases_file : Delete releases files
18 | # del_releases_tags : Delete releases tags
19 | #
20 | # get_workflows_list : Get the workflows list
21 | # out_workflows_list : Output the workflows list
22 | # del_workflows_runs : Delete workflows runs
23 | #
24 | #=============================== Set make environment variables ===============================
25 | #
26 | # Set default value
27 | delete_releases="false"
28 | delete_tags="false"
29 | prerelease_option="all"
30 | releases_keep_latest="90"
31 | releases_keep_keyword=()
32 | delete_workflows="false"
33 | workflows_keep_day="90"
34 | workflows_keep_keyword=()
35 | out_log="false"
36 |
37 | # Set the API to return 100 results per page
38 | github_per_page="100"
39 | # Set the maximum limit for API queries to 100 pages
40 | github_max_page="100"
41 |
42 | # Set font color
43 | STEPS="[\033[95m STEPS \033[0m]"
44 | INFO="[\033[94m INFO \033[0m]"
45 | NOTE="[\033[93m NOTE \033[0m]"
46 | ERROR="[\033[91m ERROR \033[0m]"
47 | SUCCESS="[\033[92m SUCCESS \033[0m]"
48 | #
49 | #==============================================================================================
50 |
51 | error_msg() {
52 | echo -e "${ERROR} ${1}"
53 | exit 1
54 | }
55 |
56 | init_var() {
57 | echo -e "${STEPS} Start Initializing Variables..."
58 |
59 | # Install the necessary dependent packages
60 | sudo apt-get -qq update && sudo apt-get -qq install -y jq curl
61 |
62 | # If it is followed by [ : ], it means that the option requires a parameter value
63 | local options="r:a:t:p:l:w:s:d:k:o:g:"
64 | parsed_args=$(getopt -o "${options}" -- "${@}")
65 | [[ ${?} -ne 0 ]] && error_msg "Parameter parsing failed."
66 | eval set -- "${parsed_args}"
67 |
68 | while true; do
69 | case "${1}" in
70 | -r | --repo)
71 | if [[ -n "${2}" ]]; then
72 | repo="${2}"
73 | shift 2
74 | else
75 | error_msg "Invalid -r parameter [ ${2} ]!"
76 | fi
77 | ;;
78 | -a | --delete_releases)
79 | if [[ -n "${2}" ]]; then
80 | delete_releases="${2}"
81 | shift 2
82 | else
83 | error_msg "Invalid -a parameter [ ${2} ]!"
84 | fi
85 | ;;
86 | -t | --delete_tags)
87 | if [[ -n "${2}" ]]; then
88 | delete_tags="${2}"
89 | shift 2
90 | else
91 | error_msg "Invalid -t parameter [ ${2} ]!"
92 | fi
93 | ;;
94 | -p | --prerelease_option)
95 | if [[ -n "${2}" ]]; then
96 | prerelease_option="${2}"
97 | shift 2
98 | else
99 | error_msg "Invalid -p parameter [ ${2} ]!"
100 | fi
101 | ;;
102 | -l | --releases_keep_latest)
103 | if [[ -n "${2}" ]]; then
104 | releases_keep_latest="${2}"
105 | shift 2
106 | else
107 | error_msg "Invalid -l parameter [ ${2} ]!"
108 | fi
109 | ;;
110 | -w | --releases_keep_keyword)
111 | if [[ -n "${2}" ]]; then
112 | oldIFS="${IFS}"
113 | IFS="/"
114 | releases_keep_keyword=(${2})
115 | IFS="${oldIFS}"
116 | shift 2
117 | else
118 | error_msg "Invalid -w parameter [ ${2} ]!"
119 | fi
120 | ;;
121 | -s | --delete_workflows)
122 | if [[ -n "${2}" ]]; then
123 | delete_workflows="${2}"
124 | shift 2
125 | else
126 | error_msg "Invalid -s parameter [ ${2} ]!"
127 | fi
128 | ;;
129 | -d | --workflows_keep_day)
130 | if [[ -n "${2}" ]]; then
131 | workflows_keep_day="${2}"
132 | shift 2
133 | else
134 | error_msg "Invalid -d parameter [ ${2} ]!"
135 | fi
136 | ;;
137 | -k | --workflows_keep_keyword)
138 | if [[ -n "${2}" ]]; then
139 | oldIFS="${IFS}"
140 | IFS="/"
141 | workflows_keep_keyword=(${2})
142 | IFS="${oldIFS}"
143 | shift 2
144 | else
145 | error_msg "Invalid -k parameter [ ${2} ]!"
146 | fi
147 | ;;
148 | -o | --out_log)
149 | if [[ -n "${2}" ]]; then
150 | out_log="${2}"
151 | shift 2
152 | else
153 | error_msg "Invalid -o parameter [ ${2} ]!"
154 | fi
155 | ;;
156 | -g | --gh_token)
157 | if [[ -n "${2}" ]]; then
158 | gh_token="${2}"
159 | shift 2
160 | else
161 | error_msg "Invalid -g parameter [ ${2} ]!"
162 | fi
163 | ;;
164 | --)
165 | shift
166 | break
167 | ;;
168 | *)
169 | [[ -n "${1}" ]] && error_msg "Invalid option [ ${1} ]!"
170 | break
171 | ;;
172 | esac
173 | done
174 |
175 | echo -e "${INFO} repo: [ ${repo} ]"
176 | echo -e "${INFO} delete_releases: [ ${delete_releases} ]"
177 | echo -e "${INFO} delete_tags: [ ${delete_tags} ]"
178 | echo -e "${INFO} prerelease_option: [ ${prerelease_option} ]"
179 | echo -e "${INFO} releases_keep_latest: [ ${releases_keep_latest} ]"
180 | echo -e "${INFO} releases_keep_keyword: [ $(echo ${releases_keep_keyword[@]} | xargs) ]"
181 | echo -e "${INFO} delete_workflows: [ ${delete_workflows} ]"
182 | echo -e "${INFO} workflows_keep_day: [ ${workflows_keep_day} ]"
183 | echo -e "${INFO} workflows_keep_keyword: [ $(echo ${workflows_keep_keyword[@]} | xargs) ]"
184 | echo -e "${INFO} out_log: [ ${out_log} ]"
185 | echo -e ""
186 | }
187 |
188 | get_releases_list() {
189 | echo -e "${STEPS} Start querying the releases list..."
190 |
191 | # Set github API default page
192 | github_page="1"
193 |
194 | # Create a file to store the results
195 | all_releases_list="josn_api_releases"
196 | echo "" >${all_releases_list}
197 |
198 | # Get the release list
199 | while true; do
200 | response="$(
201 | curl -s -L \
202 | -H "Accept: application/vnd.github+json" \
203 | -H "Authorization: Bearer ${gh_token}" \
204 | -H "X-GitHub-Api-Version: 2022-11-28" \
205 | "https://api.github.com/repos/${repo}/releases?per_page=${github_per_page}&page=${github_page}"
206 | )"
207 |
208 | # Check if the response is empty or an error occurred
209 | if [[ -z "${response}" ]] || [[ "${response}" == *"Not Found"* ]]; then
210 | break
211 | else
212 | # Get the number of results returned by the current page
213 | get_results_length="$(echo "${response}" | jq '. | length')"
214 | echo -e "${INFO} (1.1.${github_page}) Query the [ ${github_page}th ] page and return [ ${get_results_length} ] results."
215 |
216 | # Sort the results
217 | echo "${response}" |
218 | jq -s '.[] | sort_by(.published_at)|reverse' |
219 | jq -c '.[] | {date: .published_at, id: .id, prerelease: .prerelease, tag_name: .tag_name}' \
220 | >>${all_releases_list}
221 | fi
222 |
223 | # Check if the current page has fewer results than the per_page limit
224 | if [[ "${get_results_length}" -lt "${github_per_page}" ]]; then
225 | break
226 | fi
227 |
228 | # Check if the current page is greater than the maximum page
229 | if [[ "${github_page}" -ge "${github_max_page}" ]]; then
230 | echo -e "${NOTE} (1.2.1) Reach the maximum page number (${github_max_page}) in the query. skip."
231 | break
232 | else
233 | github_page="$((github_page + 1))"
234 | fi
235 | done
236 |
237 | if [[ -s "${all_releases_list}" ]]; then
238 | # Remove empty lines
239 | sed -i '/^[[:space:]]*$/d' "${all_releases_list}"
240 |
241 | # Print the result log
242 | echo -e "${INFO} (1.3.1) The api.github.com for releases request successfully."
243 | [[ "${out_log}" =~ ^(true|yes)$ ]] && {
244 | echo -e "${INFO} (1.3.2) Count of releases list: [ $(cat ${all_releases_list} | wc -l) ]"
245 | echo -e "${INFO} (1.3.3) All releases list:\n$(cat ${all_releases_list})"
246 | }
247 | else
248 | echo -e "${NOTE} (1.3.4) The releases list is empty. skip."
249 | fi
250 | }
251 |
252 | out_releases_list() {
253 | echo -e "${STEPS} Start outputting the releases list..."
254 |
255 | if [[ -s "${all_releases_list}" ]]; then
256 | # Filter based on the prerelease option(all/false/true)
257 | if [[ "${prerelease_option}" == "all" ]]; then
258 | echo -e "${NOTE} (1.4.1) Do not filter the prerelease option. skip."
259 | elif [[ "${prerelease_option}" =~ ^(false|no)$ ]]; then
260 | echo -e "${INFO} (1.4.2) Filter the prerelease option: [ false ]"
261 | cat ${all_releases_list} | jq -r '.prerelease' | grep -w "true" | while read line; do sed -i "/${line}/d" ${all_releases_list}; done
262 | elif [[ "${prerelease_option}" =~ ^(true|yes)$ ]]; then
263 | echo -e "${INFO} (1.4.3) Filter the prerelease option: [ true ]"
264 | cat ${all_releases_list} | jq -r '.prerelease' | grep -w "false" | while read line; do sed -i "/${line}/d" ${all_releases_list}; done
265 | else
266 | error_msg "Invalid prerelease option [ ${prerelease_option} ]!"
267 | fi
268 | [[ "${out_log}" =~ ^(true|yes)$ ]] && echo -e "${INFO} (1.4.4) Current releases list:\n$(cat ${all_releases_list})"
269 | else
270 | echo -e "${NOTE} (1.4.5) The releases list is empty. skip."
271 | fi
272 |
273 | # Match tags that need to be filtered
274 | keep_releases_keyword_list="josn_keep_releases_keyword_list"
275 | if [[ "${#releases_keep_keyword[@]}" -ge "1" && -s "${all_releases_list}" ]]; then
276 | # Match tags that meet the criteria
277 | echo -e "${INFO} (1.5.1) Filter tags keywords: [ $(echo ${releases_keep_keyword[@]} | xargs) ]"
278 | for ((i = 0; i < ${#releases_keep_keyword[@]}; i++)); do
279 | cat ${all_releases_list} | jq -r .tag_name | grep -E "${releases_keep_keyword[$i]}" >>${keep_releases_keyword_list}
280 | done
281 | [[ "${out_log}" =~ ^(true|yes)$ && -s "${keep_releases_keyword_list}" ]] && {
282 | echo -e "${INFO} (1.5.2) List of tags that meet the criteria:\n$(cat ${keep_releases_keyword_list})"
283 | }
284 |
285 | # Remove the tags that need to be kept
286 | [[ -s "${keep_releases_keyword_list}" ]] && {
287 | cat ${keep_releases_keyword_list} | while read line; do sed -i "/${line}/d" ${all_releases_list}; done
288 | echo -e "${INFO} (1.5.3) The tags keywords filtering successfully."
289 | }
290 |
291 | # List of remaining tags after filtering.
292 | [[ "${out_log}" =~ ^(true|yes)$ ]] && echo -e "${INFO} (1.5.4) Current releases list:\n$(cat ${all_releases_list})"
293 | else
294 | echo -e "${NOTE} (1.5.5) The filter keyword is empty. skip."
295 | fi
296 |
297 | # Match the latest tags that need to be kept
298 | keep_releases_list="josn_keep_releases_list"
299 | if [[ -s "${all_releases_list}" ]]; then
300 | if [[ "${releases_keep_latest}" -eq "0" ]]; then
301 | echo -e "${INFO} (1.6.1) Delete all releases."
302 | else
303 | # Generate a list of tags that need to be kept
304 | cat ${all_releases_list} | head -n ${releases_keep_latest} >${keep_releases_list}
305 | echo -e "${INFO} (1.6.2) The keep tags list is generated successfully."
306 | [[ "${out_log}" =~ ^(true|yes)$ && -s "${keep_releases_list}" ]] && {
307 | echo -e "${INFO} (1.6.3) The keep tags list:\n$(cat ${keep_releases_list})"
308 | }
309 |
310 | # Remove releases that need to be kept from the full list
311 | sed -i "1,${releases_keep_latest}d" ${all_releases_list}
312 | fi
313 | else
314 | echo -e "${NOTE} (1.6.4) The releases list is empty. skip."
315 | fi
316 |
317 | # Delete list
318 | if [[ -s "${all_releases_list}" ]]; then
319 | [[ "${out_log}" =~ ^(true|yes)$ ]] && echo -e "${INFO} (1.6.5) Delete releases list:\n$(cat ${all_releases_list})"
320 | else
321 | echo -e "${NOTE} (1.6.6) The delete releases list is empty. skip."
322 | fi
323 |
324 | echo -e ""
325 | }
326 |
327 | del_releases_file() {
328 | echo -e "${STEPS} Start deleting releases files..."
329 |
330 | # Delete releases
331 | if [[ -s "${all_releases_list}" && -n "$(cat ${all_releases_list} | jq -r .id)" ]]; then
332 | cat ${all_releases_list} | jq -r .id | while read release_id; do
333 | {
334 | curl -s \
335 | -X DELETE \
336 | -H "Accept: application/vnd.github+json" \
337 | -H "Authorization: Bearer ${gh_token}" \
338 | -H "X-GitHub-Api-Version: 2022-11-28" \
339 | https://api.github.com/repos/${repo}/releases/${release_id}
340 | }
341 | done
342 | echo -e "${SUCCESS} (1.7.1) Releases deleted successfully."
343 | else
344 | echo -e "${NOTE} (1.7.2) No releases need to be deleted. skip."
345 | fi
346 |
347 | echo -e ""
348 | }
349 |
350 | del_releases_tags() {
351 | echo -e "${STEPS} Start deleting tags..."
352 |
353 | # Delete the tags associated with releases
354 | if [[ "${delete_tags}" =~ ^(true|yes)$ && -s "${all_releases_list}" && -n "$(cat ${all_releases_list} | jq -r .tag_name)" ]]; then
355 | cat ${all_releases_list} | jq -r .tag_name | while read tag_name; do
356 | {
357 | curl -s \
358 | -X DELETE \
359 | -H "Accept: application/vnd.github+json" \
360 | -H "Authorization: Bearer ${gh_token}" \
361 | -H "X-GitHub-Api-Version: 2022-11-28" \
362 | https://api.github.com/repos/${repo}/git/refs/tags/${tag_name}
363 | }
364 | done
365 | echo -e "${SUCCESS} (1.8.1) Tags deleted successfully."
366 | else
367 | echo -e "${NOTE} (1.8.2) No tags need to be deleted. skip."
368 | fi
369 |
370 | echo -e ""
371 | }
372 |
373 | get_workflows_list() {
374 | echo -e "${STEPS} Start querying the workflows list..."
375 |
376 | # Set github API default page
377 | github_page="1"
378 |
379 | # Create a file to store the results
380 | all_workflows_list="josn_api_workflows"
381 | echo "" >${all_workflows_list}
382 |
383 | # Get the workflows list
384 | while true; do
385 | response="$(
386 | curl -s -L \
387 | -H "Accept: application/vnd.github+json" \
388 | -H "Authorization: Bearer ${gh_token}" \
389 | -H "X-GitHub-Api-Version: 2022-11-28" \
390 | "https://api.github.com/repos/${repo}/actions/runs?per_page=${github_per_page}&page=${github_page}"
391 | )"
392 |
393 | # Check if the response is empty or an error occurred
394 | if [[ -z "${response}" ]] || [[ "${response}" == *"Not Found"* ]]; then
395 | break
396 | else
397 | # Get the number of results returned by the current page
398 | get_results_length="$(echo "${response}" | jq -r '.workflow_runs | length')"
399 | echo -e "(2.1.${github_page}) ${INFO} Query the [ ${github_page}th ] page and return [ ${get_results_length} ] results."
400 |
401 | # Sort the results
402 | echo "${response}" |
403 | jq -c '.workflow_runs[] | select(.status == "completed") | {date: .updated_at, id: .id, name: .name}' \
404 | >>${all_workflows_list}
405 | fi
406 |
407 | # Check if the current page has fewer results than the per_page limit
408 | if [[ "${get_results_length}" -lt "${github_per_page}" ]]; then
409 | break
410 | fi
411 |
412 | # Check if the current page is greater than the maximum page
413 | if [[ "${github_page}" -ge "${github_max_page}" ]]; then
414 | echo -e "${NOTE} (2.2.1) Reach the maximum page number (${github_max_page}) in the query. skip."
415 | break
416 | else
417 | github_page="$((github_page + 1))"
418 | fi
419 | done
420 |
421 | if [[ -s "${all_workflows_list}" ]]; then
422 | # Remove empty lines
423 | sed -i '/^[[:space:]]*$/d' "${all_workflows_list}"
424 |
425 | # Print the result log
426 | echo -e "${INFO} (2.3.1) The api.github.com for workflows request successfully."
427 | [[ "${out_log}" =~ ^(true|yes)$ ]] && {
428 | echo -e "${INFO} (2.3.2) Count of workflow runs: [ $(cat ${all_workflows_list} | wc -l) ]"
429 | echo -e "${INFO} (2.3.3) All workflows runs list:\n$(cat ${all_workflows_list})"
430 | }
431 | else
432 | echo -e "${NOTE} (2.3.4) The workflows list is empty. skip."
433 | fi
434 | }
435 |
436 | out_workflows_list() {
437 | echo -e "${STEPS} Start outputting the workflows list..."
438 |
439 | # The workflows containing keywords that need to be keep
440 | keep_keyword_workflows_list="josn_keep_keyword_workflows_list"
441 | # Remove workflows that match keywords and need to be kept
442 | if [[ "${#workflows_keep_keyword[@]}" -ge "1" && -s "${all_workflows_list}" ]]; then
443 | # Match the list of workflows that meet the keywords
444 | echo -e "${INFO} (2.4.1) Filter Workflows runs keywords: [ $(echo ${workflows_keep_keyword[@]} | xargs) ]"
445 | for ((i = 0; i < ${#workflows_keep_keyword[@]}; i++)); do
446 | cat ${all_workflows_list} | jq -r .name | grep -E "${workflows_keep_keyword[$i]}" >>${keep_keyword_workflows_list}
447 | done
448 | [[ "${out_log}" =~ ^(true|yes)$ && -s "${keep_keyword_workflows_list}" ]] && {
449 | echo -e "${INFO} (2.4.2) List of Workflows runs that meet the criteria:\n$(cat ${keep_keyword_workflows_list})"
450 | }
451 |
452 | # Remove the workflows that need to be kept
453 | [[ -s "${keep_keyword_workflows_list}" ]] && {
454 | cat ${keep_keyword_workflows_list} | while read line; do sed -i "/${line}/d" ${all_workflows_list}; done
455 | echo -e "${INFO} (2.4.3) The keyword filtering successfully."
456 | }
457 |
458 | # List of remaining workflows after filtering by keywords
459 | [[ "${out_log}" =~ ^(true|yes)$ ]] && echo -e "${INFO} (2.4.4) Current workflows runs list:\n$(cat ${all_workflows_list})"
460 | else
461 | echo -e "${NOTE} (2.4.5) The filter keyword is empty. skip."
462 | fi
463 |
464 | # Generate a date list of workflows
465 | all_workflows_date_list="josn_all_workflows_date_list"
466 | # Generate a keep list of workflows
467 | keep_workflows_list="josn_keep_workflows_list"
468 | # Temporary josn file
469 | tmp_josn_file="$(mktemp)"
470 | # Sort and generate a keep list of workflows
471 | if [[ -s "${all_workflows_list}" ]]; then
472 | if [[ "${workflows_keep_day}" -eq "0" ]]; then
473 | echo -e "${INFO} (2.5.1) Delete all workflows runs."
474 | else
475 | # Remove workflows that meet the retention time
476 | today_second=$(date -d "$(date +"%Y%m%d")" +%s)
477 | cat ${all_workflows_list} | jq -r '.date' | awk -F'T' '{print $1}' | tr ' ' '\n' >${all_workflows_date_list}
478 | cat ${all_workflows_date_list} | while read line; do
479 | line_second="$(date -d "${line//-/}" +%s)"
480 | day_diff="$(((${today_second} - ${line_second}) / 86400))"
481 | [[ "${day_diff}" -lt "${workflows_keep_day}" ]] && {
482 | grep "${line}T" ${all_workflows_list} >>${keep_workflows_list}
483 | sed -i "/${line}T/d" ${all_workflows_list}
484 | }
485 | done
486 | echo -e "${INFO} (2.5.2) The keep workflows runs list is generated successfully."
487 |
488 | # Remove duplicate lines
489 | [[ -s "${keep_workflows_list}" ]] && {
490 | awk '!a[$0]++' ${keep_workflows_list} >${tmp_josn_file} && mv -f ${tmp_josn_file} ${keep_workflows_list}
491 | [[ "${out_log}" =~ ^(true|yes)$ ]] && echo -e "${INFO} (2.5.3) Keep workflows list:\n$(cat ${keep_workflows_list})"
492 | }
493 | fi
494 | else
495 | echo -e "${NOTE} (2.5.4) The workflows runs list is empty. skip."
496 | fi
497 |
498 | # Delete list
499 | if [[ -s "${all_workflows_list}" ]]; then
500 | [[ "${out_log}" =~ ^(true|yes)$ ]] && echo -e "${INFO} (2.5.5) Delete workflows list:\n$(cat ${all_workflows_list})"
501 | else
502 | echo -e "${NOTE} (2.5.6) The delete workflows list is empty. skip."
503 | fi
504 |
505 | echo -e ""
506 | }
507 |
508 | del_workflows_runs() {
509 | echo -e "${STEPS} Start deleting workflows runs..."
510 |
511 | # Delete workflows runs
512 | if [[ -s "${all_workflows_list}" && -n "$(cat ${all_workflows_list} | jq -r .id)" ]]; then
513 | cat ${all_workflows_list} | jq -r .id | while read run_id; do
514 | {
515 | curl -s \
516 | -X DELETE \
517 | -H "Accept: application/vnd.github+json" \
518 | -H "Authorization: Bearer ${gh_token}" \
519 | -H "X-GitHub-Api-Version: 2022-11-28" \
520 | https://api.github.com/repos/${repo}/actions/runs/${run_id}
521 | }
522 | done
523 | echo -e "${SUCCESS} (2.6.1) Workflows runs deleted successfully."
524 | else
525 | echo -e "${NOTE} (2.6.2) No Workflows runs need to be deleted. skip."
526 | fi
527 |
528 | echo -e ""
529 | }
530 |
531 | # Show welcome message
532 | echo -e "${STEPS} Welcome to use the delete older releases and workflow runs tool!"
533 |
534 | # Perform related operations in sequence
535 | init_var "${@}"
536 |
537 | # Delete release
538 | if [[ "${delete_releases}" =~ ^(true|yes)$ ]]; then
539 | get_releases_list
540 | out_releases_list
541 | del_releases_file
542 | del_releases_tags
543 | else
544 | echo -e "${STEPS} Do not delete releases and tags."
545 | fi
546 |
547 | # Delete workflows
548 | if [[ "${delete_workflows}" =~ ^(true|yes)$ ]]; then
549 | get_workflows_list
550 | out_workflows_list
551 | del_workflows_runs
552 | else
553 | echo -e "${STEPS} Do not delete workflows."
554 | fi
555 |
556 | # Show all process completion prompts
557 | echo -e "${SUCCESS} All process completed successfully."
558 |
--------------------------------------------------------------------------------