-
Notifications
You must be signed in to change notification settings - Fork 71
/
Connect-AppVeyorToComputer.ps1
156 lines (125 loc) · 6.54 KB
/
Connect-AppVeyorToComputer.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
150
151
152
Function Connect-AppVeyorToComputer {
<#
.SYNOPSIS
Command to enable AppVeyor builds running on a host directly. Works with both hosted AppVeyor and AppVeyor Server.
.DESCRIPTION
You can connect your AppVeyor account (on both hosted AppVeyor and on-premise AppVeyor Server) to computer running Windows, Linux or Mac for AppVeyor to instantiate builds directly on it.
.PARAMETER AppVeyorUrl
AppVeyor URL. For hosted AppVeyor it is https://ci.appveyor.com. For Appveyor Server users it is URL of on-premise AppVeyor Server installation
.PARAMETER ApiToken
API key for specific account (not 'All accounts'). Hosted AppVeyor users can find it at https://ci.appveyor.com/api-keys. Appveyor Server users can find it at <appveyor_server_url>/api-keys.
.EXAMPLE
Connect-AppVeyorToComputer
Let command collect all required information
.EXAMPLE
Connect-AppVeyorToComputer -ApiToken XXXXXXXXXXXXXXXXXXXXX -AppVeyorUrl "https://ci.appveyor.com"
Run command with all required parameters so command will ask no questions. It will install AppVeyor Host Agent and configure "Process" build cloud in AppVeyor.
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true,HelpMessage="AppVeyor URL`nFor hosted AppVeyor it is https://ci.appveyor.com`nFor Appveyor Server users it is URL of on-premise AppVeyor Server installation")]
[string]$AppVeyorUrl,
[Parameter(Mandatory=$true,HelpMessage="API key for specific account (not 'All accounts')`nHosted AppVeyor users can find it at https://ci.appveyor.com/api-keys`nAppveyor Server users can find it at <appveyor_server_url>/api-keys")]
[string]$ApiToken
)
function ExitScript {
# some cleanup?
break all
}
$ErrorActionPreference = "Stop"
$StopWatch = New-Object System.Diagnostics.Stopwatch
$StopWatch.Start()
#Sanitize input
$AppVeyorUrl = $AppVeyorUrl.TrimEnd("/")
#Validate AppVeyor API access
$headers = ValidateAppVeyorApiAccess $AppVeyorUrl $ApiToken
EnsureElevatedModeOnWindows
try {
Write-Host "Configuring 'Process' build cloud in AppVeyor" -ForegroundColor Cyan
$hostName = $env:COMPUTERNAME # Windows
$imageName = "Windows"
$osType = "Windows"
if ($isLinux) {
# Linux
$hostName = (hostname)
$imageName = "Linux"
$osType = "Linux"
} elseif ($isMacOS) {
# macOS
$hostName = (hostname)
$imageName = "macOS"
$osType = "MacOS"
}
$build_cloud_name = "$hostName"
$hostAuthorizationToken = [Guid]::NewGuid().ToString('N')
$clouds = Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-clouds" -Headers $headers -Method Get
$cloud = $clouds | Where-Object ({$_.name -eq $build_cloud_name})[0]
if (-not $cloud) {
# check if there is a cloud already with the name "$build_cloud_name Docker" and grab $hostAuthorizationToken from there
$docker_build_cloud_name = "$hostName Docker"
$dockerCloud = $clouds | Where-Object ({$_.name -eq $docker_build_cloud_name})[0]
if ($dockerCloud -and $dockerCloud.CloudType -eq 'Docker') {
Write-Host "There is an existing 'Docker' cloud for that computer. Reading Host Agent authorization token from Docker cloud." -ForegroundColor DarkGray
$settings = Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-clouds/$($dockerCloud.buildCloudId)" -Headers $headers -Method Get
$hostAuthorizationToken = $settings.hostAuthorizationToken
}
# Add new build cloud
$body = @{
cloudType = "Process"
name = $build_cloud_name
hostAuthorizationToken = $hostAuthorizationToken
workersCapacity = 20
settings = @{
failureStrategy = @{
jobStartTimeoutSeconds = 60
provisioningAttempts = 2
}
cloudSettings = @{
general = @{
}
}
}
}
$jsonBody = $body | ConvertTo-Json -Depth 10
Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-clouds" -Headers $headers -Body $jsonBody -Method Post | Out-Null
$clouds = Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-clouds" -Headers $headers -Method Get
$cloud = $clouds | Where-Object ({$_.name -eq $build_cloud_name})[0]
Write-Host "A new AppVeyor build cloud '$build_cloud_name' has been added."
} else {
Write-Host "AppVeyor cloud '$build_cloud_name' already exists." -ForegroundColor DarkGray
if ($cloud.CloudType -eq 'Process') {
Write-Host "Reading Host Agent authorization token from the existing cloud."
$settings = Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-clouds/$($cloud.buildCloudId)" -Headers $headers -Method Get
$hostAuthorizationToken = $settings.hostAuthorizationToken
} else {
throw "Existing build cloud '$build_cloud_name' is not of 'Process' type."
}
}
Write-host "`nEnsure build worker image is available for AppVeyor projects" -ForegroundColor Cyan
$images = Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-worker-images" -Headers $headers -Method Get
$image = $images | Where-Object ({$_.name -eq $ImageName})[0]
if (-not $image) {
$body = @{
name = $imageName
osType = $osType
}
$jsonBody = $body | ConvertTo-Json
Invoke-RestMethod -Uri "$AppVeyorUrl/api/build-worker-images" -Headers $headers -Body $jsonBody -Method Post | Out-Null
Write-host "AppVeyor build worker image '$ImageName' has been created."
} else {
Write-host "AppVeyor build worker image '$ImageName' already exists." -ForegroundColor DarkGray
}
# Install Host Agent
InstallAppVeyorHostAgent $AppVeyorUrl $hostAuthorizationToken
$StopWatch.Stop()
$completed = "{0:hh}:{0:mm}:{0:ss}" -f $StopWatch.elapsed
Write-Host "`nThe script successfully completed in $completed." -ForegroundColor Green
#Report results and next steps
PrintSummary 'this computer' $AppVeyorUrl $cloud.buildCloudId $build_cloud_name $imageName
}
catch {
Write-Error $_
ExitScript
}
}