-
Notifications
You must be signed in to change notification settings - Fork 13
/
run-workflow.ps1
149 lines (124 loc) · 3.87 KB
/
run-workflow.ps1
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
param (
[Parameter(Mandatory = $true)]
[string]$pat,
[Parameter(Mandatory = $true)]
[string]$type,
[Parameter(Mandatory = $false)]
[string]$sha = "",
[Parameter(Mandatory = $false)]
[string]$ref = "",
[Parameter(Mandatory = $false)]
[string]$pr = "",
[Parameter(Mandatory = $false)]
[string]$run_id = "",
[Parameter(Mandatory = $false)]
[string]$logs = "",
[Parameter(Mandatory = $false)]
[string]$filter = "",
[Parameter(Mandatory = $false)]
[switch]$skipWaiting,
[Parameter(Mandatory = $false)]
[string]$project = ""
)
Set-StrictMode -Version 'Latest'
$PSDefaultParameterValues['*:ErrorAction'] = 'Stop'
$headers = @{
"Accept" = "application/vnd.github+json"
"Authorization" = "Bearer $pat"
"X-GitHub-Api-Version" = "2022-11-28"
}
$guid = (New-Guid).ToString()
function Start-Workflow {
$url = "https://api.github.com/repos/microsoft/netperf/dispatches"
$body = @{
event_type = "run-$type"
client_payload = @{
guid = $guid
sha = $sha
ref = $ref
pr = $pr
run_id = $run_id
logs = $logs
filter = $filter
}
} | ConvertTo-Json
Write-Debug "POST $body to $url"
$result = Invoke-WebRequest -Uri $url -Method POST -Headers $headers -Body $body
}
function Get-Runs {
$url = "https://api.github.com/repos/microsoft/netperf/actions/runs?event=repository_dispatch"
Write-Debug "GET $url"
return ((Invoke-WebRequest -Uri $url -Method GET -Headers $headers).Content | ConvertFrom-Json).workflow_runs
}
function Get-Run {
param([string]$runId)
$url = "https://api.github.com/repos/microsoft/netperf/actions/runs/$runId"
Write-Debug "GET $url"
return (Invoke-WebRequest -Uri $url -Method GET -Headers $headers).Content | ConvertFrom-Json
}
function Get-Jobs {
param([string]$runId)
$url = "https://api.github.com/repos/microsoft/netperf/actions/runs/$runId/jobs"
Write-Debug "GET $url"
return ((Invoke-WebRequest -Uri $url -Method GET -Headers $headers).Content | ConvertFrom-Json).jobs
}
function Get-RunId {
for ($i = 0; $i -lt 10; $i++) { # Try up to 10 times
$workflows = Get-Runs
foreach ($workflow in $workflows) {
if ($project) {
if ($project.name -ne $workflow.name) {
continue
}
}
$jobs = Get-Jobs $workflow.id
foreach ($job in $jobs) {
if ($job.name.Contains($guid)) {
return $workflow.id
}
}
}
Write-Host "Run not found, retrying in 1 second..."
Start-Sleep -Seconds 1
}
Write-Error "Run not found!"
return $null
}
function Get-RunStatus {
param([string]$id)
$run = Get-Run $id
if ($run.status -ne "completed") {
return $false
}
if ($run.conclusion -ne "success") {
Write-Error "Run completed with status $($run.conclusion)!"
}
return $true
}
function Wait-ForRun {
param([string]$id)
for ($i = 0; $i -lt 300; $i++) { # 300 * 30 sec = 2.5 hours
if (Get-RunStatus $id) {
return
}
Start-Sleep -Seconds 30
}
Write-Error "Run timed out!"
}
# Start the new workflow run.
Write-Host "Triggering new workflow ($guid)..."
Start-Workflow
# Wait a bit for the github backend to update.
Start-Sleep 10
# Find the workflow run.
Write-Host "Looking for workflow run..."
$id = Get-RunId
Write-Host "Found: https://github.com/microsoft/netperf/actions/runs/$id"
if ($skipWaiting) {
return $id
}
# Wait for the run to complete.
Write-Host "Waiting for run to complete..."
Wait-ForRun $id
Write-Host "Run succeeded!"
return $id