-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCleanup-Runners.ps1
More file actions
38 lines (34 loc) · 1.63 KB
/
Cleanup-Runners.ps1
File metadata and controls
38 lines (34 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#This script invokes GitHub-CLI (Pre-installed on container image)
Param (
[Parameter(Mandatory = $false)]
[string]$owner = $env:GH_OWNER,
[Parameter(Mandatory = $false)]
[string]$repo = $env:GH_REPOSITORY,
[Parameter(Mandatory = $false)]
[string]$pat = $env:GH_TOKEN
)
#Use --with-token to pass in a PAT token on standard input. The minimum required scopes for the token are: "repo", "read:org".
#Alternatively, gh will use the authentication token found in environment variables. See gh help environment for more info.
#To use gh in GitHub Actions, add GH_TOKEN: $ to "env". on Docker run: Docker run -e GH_TOKEN='myPatToken'
gh auth login --with-token $pat
#Cleanup#
#Look for any old/stale dockerNode- registrations to clean up
#Windows containers cannot gracefully remove registration via powershell due to issue: https://github.com/moby/moby/issues/25982#
#For this reason we can use this scrip to cleanup old offline instances/registrations
$runnerBaseName = "dockerNode-"
$runnerListJson = gh api -H "Accept: application/vnd.github.v3+json" "/repos/$owner/$repo/actions/runners"
$runnerList = (ConvertFrom-Json -InputObject $runnerListJson).runners
Foreach ($runner in $runnerList) {
try {
If (($runner.name -like "$runnerBaseName*") -and ($runner.status -eq "offline")) {
write-host "Unregsitering old stale runner: $($runner.name)"
gh api --method DELETE -H "Accept: application/vnd.github.v3+json" "/repos/$owner/$repo/actions/runners/$($runner.id)"
}
}
catch {
Write-Error $_.Exception.Message
}
}
#Remove PAT token after cleanup
$pat=$null
$env:GH_TOKEN=$null