-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[skip ci] create workflow cleanup script
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<# | ||
.SYNOPSIS | ||
PowerShell script to remove Workflow run in the GitHub Actions | ||
.PARAMETER Repository | ||
The name of the repository. | ||
.PARAMETER Workflow | ||
The name of the workflow in the repository. | ||
.PARAMETER HeadBranch | ||
Branch of the workflow runs. Used to select the matching runs. | ||
.PARAMETER Conclusion | ||
Final status of the workflow run. ex) cancelled | ||
.PARAMETER GitHubHost | ||
Hostname for GitHub CLI, API request. github.com, git.company.com | ||
.LINK | ||
https://docs.github.com/en/actions/monitoring-and-troubleshooting-workflows/using-workflow-run-logs | ||
#> | ||
param | ||
( | ||
[String]$Repository = "", | ||
[Parameter(Mandatory = $true)][String]$Workflow, | ||
[String]$HeadBranch = "main", | ||
[String]$Conclusion = "cancelled", | ||
[String]$GitHubHost = "$env:GH_HOST" | ||
) | ||
|
||
# If the Repository is empty, we will parse it from the origin URL | ||
if ($Repository -eq "") { | ||
[String]$OriginURL = $(git remote get-url origin) | ||
# Parse the organization and repository name from OriginURL | ||
# For example, if the URL is https://github.com/luncliff/vcpkg-registry, the value will be "luncliff/vcpkg-registry" | ||
$Repository = $OriginURL -replace "https://$GitHubHost/", "" -replace ".git", "" | ||
Write-Output "Repository: $Repository" | ||
} | ||
|
||
# ex) '.workflow_runs[] | select(.conclusion != "") | .id' | ||
[String]$Query = ".workflow_runs[] | select(.head_branch == ""$HeadBranch"") | select(.conclusion == ""$Conclusion"") | .id" | ||
Write-Output "Query: $Query" | ||
|
||
gh api ` | ||
-H "Accept: application/vnd.github+json" ` | ||
-H "X-GitHub-Api-Version: 2022-11-28" ` | ||
"/repos/$Repository/actions/workflows/$Workflow/runs" ` | ||
--paginate ` | ||
--jq $Query > "runs.txt" | ||
|
||
foreach ($RunID in Get-Content "runs.txt") { | ||
gh api ` | ||
--method DELETE ` | ||
-H "Accept: application/vnd.github+json" ` | ||
-H "X-GitHub-Api-Version: 2022-11-28" ` | ||
"/repos/$Repository/actions/runs/$RunID" | ||
Write-Output "Deleted: $RunID" | ||
Start-Sleep -Milliseconds 100 | ||
} | ||
|
||
Remove-Item -Force "runs.txt" |