Skip to content

Commit

Permalink
[skip ci] create workflow cleanup script
Browse files Browse the repository at this point in the history
  • Loading branch information
luncliff committed Nov 23, 2024
1 parent a79aac1 commit c5fa076
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
File renamed without changes.
59 changes: 59 additions & 0 deletions scripts/gh-cleanup-runs.ps1
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"

0 comments on commit c5fa076

Please sign in to comment.