forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNew-DockerTestBuild.ps1
More file actions
292 lines (250 loc) · 8.32 KB
/
Copy pathNew-DockerTestBuild.ps1
File metadata and controls
292 lines (250 loc) · 8.32 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
param ( [switch]$Force, [switch]$UseExistingMsi )
$script:Constants = @{
AccountName = 'PowerShell'
UrlBase = 'https://ci.appveyor.com'
ApiUrl = 'https://ci.appveyor.com/api'
ProjectName = 'powershell-f975h'
TestImageName = "remotetestimage"
MsiName = "PSCore.msi"
Token = "" # in this particular use we don't need a token
}
function Get-AppVeyorBuildArtifact {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$build,
[string]$downloadFolder = '.',
[string]$artifactFilter = '*.msi',
[string]$artifactLocalFileName = $Constants.MsiName
)
$headers = @{
'Authorization' = "Bearer {0}" -f $Constants.Token
'Content-type' = 'application/json'
}
# get project with last build details
$URL = "{0}/projects/{1}/{2}/build/$build" -f $Constants.ApiUrl,$Constants.AccountName,$Constants.ProjectName,$build
$project = Invoke-RestMethod -Method Get -Uri $URL -Headers $headers
# we assume here that build has a single job
# get this job id
$jobId = $project.build.jobs[0].jobId
# get job artifacts (just to see what we've got)
$URL = "{0}/buildjobs/{1}/artifacts" -f $Constants.ApiUrl,$jobid
$artifacts = Invoke-RestMethod -Method Get -Uri $URL -Headers $headers
# here we just take the first artifact, but you could specify its file name
# $artifactFileName = 'MyWebApp.zip'
$artifact = $artifacts | Where-Object {$_.fileName -like $artifactFilter} | Select-Object -First 1
$artifactFileName = $artifact.fileName
# artifact will be downloaded as
$localArtifactPath = "$downloadFolder\$artifactLocalFileName"
# download artifact
# -OutFile - is local file name where artifact will be downloaded into
$URI = "{0}/buildjobs/{1}/artifacts/{2}" -f $Constants.ApiUrl,$jobId,$artifactFileName
Invoke-RestMethod -Method Get -Uri $URI -OutFile $localArtifactPath -Headers $headers
return $localArtifactPath
}
# Get a collection of appveyor objects representing the builds for the last day
function Get-AppVeyorBuilds
{
[CmdletBinding()]
param(
[ValidateNotNullOrEmpty()]
[string]$ExtensionBranch = 'master',
[ValidateRange(1,7)][int]$Days = 7,
[int]$Last = 1
)
[datetime]$start = ((get-date).AddDays(-${Days}))
$results = Get-AppVeyorBuildRange -Start $start -LastBuildId $null -ExtensionBranch $ExtensionBranch
$results
}
function Get-AppVeyorBuildRange
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, Position=0)]
[string]
$ExtensionBranch = 'dev',
[Parameter(Mandatory=$true, Position=1)]
[datetime]
$Start,
[Parameter(Mandatory=$true, Position=2)]
[AllowNull()]
[Object]
$LastBuildId,
[Parameter(Mandatory=$false, Position=3)]
[int]
$Records = 20,
[switch]
$IncludeRunning
)
$result = @{
builds = @()
LastBuildId = ''
FoundLast = $false
}
if($LastBuildId)
{
$startBuildIdString = "&startBuildId=$LastBuildId"
}
$URI = "{0}/projects/{1}/{2}/history?recordsNumber={3}{4}&branch{5}" -f $Constants.ApiUrl,$Constants.AccountName,
$Constants.ProjectName,$Records,$startBuildIdString,$ExtensionBranch
$project = Invoke-RestMethod -Method Get -Uri $URI
foreach($build in $project.builds)
{
if($build.Status -ne 'running' -or $IncludeRunning)
{
$createdString = $build.created
$created = [datetime]::Parse($createdString)
if($created -gt $Start)
{
$version = $build.version
$result.lastBuildId = $build.buildId
$URI = "{0}/projects/{1}/{2}/build/{3}" -f $Constants.ApiUrl, $Constants.AccountName,
$Constants.ProjectName,$version
$buildProject = Invoke-RestMethod -Method Get -Uri $URI -Headers $headers -verbose:$false
$result.builds += Convert-AppVeyorBuildJson -build $buildProject.Build
}
else
{
$result.foundLast = $true
}
}
}
return $result
}
# Convert AppVeyor Build Json into a more usable PSObject
function Convert-AppVeyorBuildJson
{
[CmdletBinding()]
param
(
[Parameter(Mandatory=$true, Position=0)]
[Object]
$build
)
$Job = $build.jobs[0]
$status = $build.status
[datetime] $started = [datetime]::MinValue
[datetime] $finished = [datetime]::MaxValue
if($build.started)
{
[datetime] $started = [datetime]::Parse($build.started)
}
if($build.finished)
{
[datetime] $finished = [datetime]::Parse($build.finished)
}
$duration = $null
if($status -ne 'running')
{
$duration = $finished.Subtract($started)
}
$version = $build.version
$link = '<a href="{0}/project/{0}/{1}/build/{2}">Results</a>' -f $Constants.BaseUrl,$Constants.AccountName, $Constants.ProjectName, $version
$tests = @()
$tag = [string]::Empty
if($build.message.StartsWith('[') -and $build.message.Contains(']'))
{
$tag = $build.message.Substring(1,$build.message.IndexOf(']')).Replace(']','')
if($tag.Contains('('))
{
$tagParts = $tag.Split('(')
$tag = $tagParts[0]
for($i=1;$i -lt $tagParts.Count; $i++)
{
$tests += $tagParts[$i].Replace(')','')
}
}
$tag = $tag
}
$testString = $tests -join ','
$result = [PsCustomObject]@{
version = $version
Id = $build.BuildId
author = $build.authorName
branch = $Build.branch
status = $status
started = $started
StartedDay = [datetime]$started.Date
finished = $finished
duration = $duration
message = $build.message
testsCount = $Job.testsCount
passedTestsCount = $Job.passedTestsCount
failedTestsCount = $Job.failedTestsCount
link = $link
JobId = $Job.JobId
Tests = $testString
Tag = $tag
}
$result.pstypenames.Clear()
$result.pstypenames.Add('AppVeyorBuildSummary')
$result
}
############
### MAIN ###
############
#### DOCKER OPS #####
# is docker installed?
$dockerExe = get-command docker -ea silentlycontinue
if ( $dockerExe.name -ne "docker.exe" ) {
throw "Cannot find docker, is it installed?"
}
# Check to see if we already have an image, and if so
# delete it if -Force was used, otherwise throw and exit
$TestImage = docker images $Constants.TestImageName --format '{{.Repository}}'
if ( $TestImage -eq $Constants.TestImageName)
{
if ( $Force )
{
docker rmi $Constants.TestImageName
}
else
{
throw ("{0} already exists, use '-Force' to remove" -f $Constants.TestImageName)
}
}
# check again - there could be some permission problems
$TestImage = docker images $Constants.TestImageName --format '{{.Repository}}'
if ( $TestImage -eq $Constants.TestImageName)
{
throw ("'{0}' still exists, giving up" -f $Constants.TestImageName)
}
#### MSI CHECKS ####
# check to see if the MSI is present
$MsiExists = test-path $Constants.MsiName
$msg = "{0} exists, use -Force to remove or -UseExistingMsi to use" -f $Constants.MsiName
if ( $MsiExists -and ! ($force -or $useExistingMsi))
{
throw $msg
}
# remove the msi
if ( $MsiExists -and $Force -and ! $UseExistingMsi )
{
Remove-Item -force $Constants.MsiName
$MsiExists = $false
}
# a couple of checks before downloading or using the existing one
# if the msi exists and -UseExistingMsi is present, we'll use the
# one we found
if ( ! $MsiExists -and $UseExistingMsi )
{
throw ("{0} does not exist" -f $Constants.MsiName)
}
elseif ( $MsiExists -and ! $UseExistingMsi )
{
throw $msg
}
elseif ( ! ($MsiExists -and $UseExistingMsi) ) # download the msi
{
$builds = Get-AppVeyorBuilds -ExtensionBranch master
$build = $builds.builds | sort-object finished | select-object -last 1
Get-AppVeyorBuildArtifact -build $build.version
}
# last check before bulding the image
if ( ! (test-path $Constants.MsiName) )
{
throw ("{0} does not exist, giving up" -f $Constants.MsiName)
}
# collect the builds and select the last one
Docker build --tag $Constants.TestImageName .