-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbuild.githubactions.ps1
452 lines (374 loc) · 13.4 KB
/
build.githubactions.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# SPDX-License-Identifier: Apache-2.0
# Licensed to the Ed-Fi Alliance under one or more agreements.
# The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
# See the LICENSE and NOTICES files in the project root for more information.
[CmdLetBinding()]
param(
# Command to execute, defaults to "Build".
[string]
[ValidateSet(
"DotnetClean",
"Restore",
"Build",
"Test",
"Pack",
"Publish",
"CheckoutBranch",
"InstallCredentialHandler",
"StandardVersions",
"StandardTag",
"TpdmTag",
"TriggerImplementationRepositoryWorkflows")]
$Command = "Build",
[switch] $SelfContained,
# Informational version number, defaults 1.0
[string]
$InformationalVersion = "1.0",
# Build counter from the automation tool.
[string]
$BuildCounter = "1",
[string]
$BuildIncrementer = "0",
# .NET project build configuration, defaults to "Release". Options are: Debug, Release.
[string]
[ValidateSet("Debug", "Release")]
$Configuration = "Release",
[bool]
$DryRun = $false,
[string]
$NugetApiKey,
[string]
$EdFiNuGetFeed,
# .Net Project Solution Name
[string]
$Solution,
# .Net Project Name
[string]
$ProjectFile,
[string]
$PackageName,
[string]
$TestFilter,
[string]
$NuspecFilePath,
[string]
$RelativeRepoPath,
[string]
$Copyright = "Copyright @ " + $((Get-Date).year) + " Ed-Fi Alliance, LLC and Contributors",
[ValidateSet('4.0.0', '5.2.0')]
[string] $StandardVersion
)
$newRevision = ([int]$BuildCounter) + ([int]$BuildIncrementer)
$version = "$InformationalVersion.$newRevision"
$packageOutput = "$PSScriptRoot/NugetPackages"
$packagePath = "$packageOutput/$PackageName.$version.nupkg"
if ([string]::IsNullOrWhiteSpace($Solution)){
$Solution =$ProjectFile
}
function Invoke-Execute {
param (
[ScriptBlock]
$Command
)
$global:lastexitcode = 0
& $Command
if ($lastexitcode -ne 0) {
throw "Error executing command: $Command"
}
}
function Invoke-Step {
param (
[ScriptBlock]
$block
)
$command = $block.ToString().Trim()
Write-Host
Write-Host $command -fore CYAN
&$block
}
function Invoke-Main {
param (
[ScriptBlock]
$MainBlock
)
try {
&$MainBlock
Write-Host
Write-Host "Build Succeeded" -fore GREEN
exit 0
} catch [Exception] {
Write-Host
Write-Error $_.Exception.Message
Write-Host
Write-Error "Build Failed"
exit 1
}
}
function DotnetClean {
Invoke-Execute { dotnet clean $Solution -c $Configuration --nologo -v minimal }
}
function Restore {
Invoke-Execute { dotnet restore $Solution --verbosity:normal }
}
function Compile {
Invoke-Execute {
dotnet --info
dotnet build $Solution -c $Configuration --version-suffix $version --no-restore -p:StandardVersion=$StandardVersion
}
}
function Pack {
if ([string]::IsNullOrWhiteSpace($PackageName) -and [string]::IsNullOrWhiteSpace($NuspecFilePath)){
Invoke-Execute {
dotnet pack $ProjectFile -c $Configuration --output $packageOutput --no-build --no-restore --verbosity normal -p:VersionPrefix=$version -p:NoWarn=NU5123
}
}
if ($NuspecFilePath -Like "*.nuspec" -and $null -ne $PackageName){
$xml = [xml](Get-Content $NuspecFilePath)
$xml.package.metadata.id = $PackageName
$xml.package.metadata.description = $PackageName
$xml.Save($NuspecFilePath)
nuget pack $NuspecFilePath -OutputDirectory $packageOutput -Version $version -Properties configuration=$Configuration -Properties copyright=$Copyright -NoPackageAnalysis -NoDefaultExcludes
}
if ([string]::IsNullOrWhiteSpace($NuspecFilePath) -and $null -ne $PackageName){
Invoke-Execute {
dotnet pack $ProjectFile -c $Configuration --output $packageOutput --no-build --no-restore --verbosity normal -p:VersionPrefix=$version -p:NoWarn=NU5123 -p:PackageId=$PackageName
}
}
}
function Publish {
Invoke-Execute {
if (-not $NuGetApiKey) {
throw "Cannot push a NuGet package without providing an API key in the `NuGetApiKey` argument."
}
if (-not $EdFiNuGetFeed) {
throw "Cannot push a NuGet package without providing a feed in the `EdFiNuGetFeed` argument."
}
if($DryRun){
Write-Host "Dry run enabled, not pushing package."
} else {
Write-Host "Pushing $packagePath to $EdFiNuGetFeed"
dotnet nuget push $packagePath --api-key $NuGetApiKey --source $EdFiNuGetFeed
}
}
}
function Test {
if(-not $TestFilter) {
Invoke-Execute { dotnet test $solution -c $Configuration --no-build --no-restore -v normal }
} else {
Invoke-Execute { dotnet test $solution -c $Configuration --no-build --no-restore -v normal --filter TestCategory!~"$TestFilter" }
}
}
function CheckoutBranch {
Set-Location $RelativeRepoPath
$odsBranch = $Env:REPOSITORY_DISPATCH_BRANCH
Write-Output "OdsBranch: $odsBranch"
if(![string]::IsNullOrEmpty($odsBranch)){
$patternName = "refs/heads/$odsBranch"
$does_corresponding_branch_exist = $false
$does_corresponding_branch_exist = git ls-remote --heads origin $odsBranch | Select-String -Pattern $patternName -SimpleMatch -Quiet
if ($does_corresponding_branch_exist -eq $true) {
Write-Output "Corresponding branch for $odsBranch exists in $RelativeRepoPath repo, so checking it out"
git fetch origin $odsBranch
git checkout $odsBranch
} else {
Write-Output "Corresponding branch for $odsBranch does not exist in Implementation repo, so not changing branch checked out"
}
} else {
Write-Output "ref_name: $Env:REF_NAME"
$current_branch = "$Env:REF_NAME"
if ($current_branch -like "*/merge"){
Write-Output "ref_name is PR, so using head_ref: $Env:HEAD_REF"
$current_branch = "$Env:HEAD_REF"
}
$patternName = "refs/heads/$current_branch"
Write-Output "Pattern Name is $patternName" -fore GREEN
$branch_exists = $false
$branch_exists = git ls-remote --heads origin $current_branch | Select-String -Pattern $patternName -SimpleMatch -Quiet
if ($branch_exists -eq $true) {
Write-Output "Current branch exists, so setting to $current_branch"
git fetch origin $current_branch
git checkout $current_branch
} else {
Write-Output "did not match on any results for changing ODS checkout branch"
}
}
}
function Get-IsWindows {
<#
.SYNOPSIS
Checks to see if the current machine is a Windows machine.
.EXAMPLE
Get-IsWindows returns $True
#>
if ($null -eq $IsWindows) {
# This section will only trigger when the automatic $IsWindows variable is not detected.
# Every version of PS released on Linux contains this variable so it will always exist.
# $IsWindows does not exist pre PS 6.
return $true
}
return $IsWindows
}
function InstallCredentialHandler {
if (Get-IsWindows -and -not Get-InstalledModule | Where-Object -Property Name -eq "7Zip4Powershell") {
Install-Module -Force -Scope CurrentUser -Name 7Zip4Powershell
Write-Host "Installed 7Zip4Powershell."
}
# using WebClient is faster then Invoke-WebRequest but shows no progress
$sourceUrl = ' https://github.com/microsoft/artifacts-credprovider/releases/download/v1.0.0/Microsoft.NuGet.CredentialProvider.zip'
$fileName = 'Microsoft.NuGet.CredentialProvider.zip'
$zipFilePath = Join-Path ([IO.Path]::GetTempPath()) $fileName
Write-Host "Downloading file from $sourceUrl..."
$webClient = New-Object System.Net.WebClient
$webClient.DownloadFile($sourceUrl, $zipFilePath)
Write-Host "Download complete."
if (-not (Test-Path $zipFilePath)) {
Write-Warning "Microsoft.NuGet.CredentialProvider file '$fileName' not found."
exit 0
}
$packageFolder = Join-Path ([IO.Path]::GetTempPath()) 'Microsoft.NuGet.CredentialProvider/'
if ($fileName.EndsWith('.zip')) {
Write-Host "Extracting $fileName..."
if (Test-Path $zipFilePath) { Expand-Archive -Force -Path $zipFilePath -DestinationPath $packageFolder }
Copy-Item -Path $packageFolder\* -Destination "~/.nuget/" -Recurse -Force
Write-Host "Extracted to: ~\.nuget\plugins\" -ForegroundColor Green
}
}
function StandardVersions {
$standardProjectDirectory = Split-Path $Solution -Resolve
$standardProjectPath = Join-Path $standardProjectDirectory "/Standard/"
$versions = (Get-ChildItem -Path $standardProjectPath -Directory -Force -ErrorAction SilentlyContinue | Select -ExpandProperty Name | %{ "'" + $_ + "'" }) -Join ','
$standardVersions = "[$versions]"
return $standardVersions
}
function StandardTag {
if (-not $StandardVersion) {
throw "StandardVersion is required."
}
return RepositoryTag('Ed-Fi-Standard')
}
function TpdmTag {
if (-not $StandardVersion) {
throw "StandardVersion is required."
}
return RepositoryTag('Ed-Fi-TPDM-Artifacts')
}
function RepositoryTag {
param (
[string]
$Repository
)
$standardProjectDirectory = Split-Path $Solution -Resolve
$versionMapPath = Join-Path $standardProjectDirectory "/versionmap.json"
$versionTag = (Get-Content $versionMapPath | ConvertFrom-Json).$Repository.$StandardVersion
return $versionTag
}
function TriggerImplementationRepositoryWorkflows {
<#
.SYNOPSIS
Searches for the corresponding PR in the Implementation repository; if found,
adds and removes a label to the PR, restarting all the PR workflows.
Note that the workflows must be configured to be triggered by the `unlabeled` pull_request event type.
#>
Assert-EnvironmentVariablesInitialized(@("current_branch", "REPOSITORY_OWNER", "EDFI_ODS_IMP_TOKEN"))
$headers = @{
Authorization = "Bearer $Env:EDFI_ODS_IMP_TOKEN"
Accept = "application/vnd.github.v3+json"
}
$body = @{
state = "open"
head = "${Env:REPOSITORY_OWNER}:${Env:current_branch}"
}
if ($Env:BASE_REF) {
$body.Add('base', $Env:BASE_REF)
}
Write-Host "Looking for a PR in the Implementation repository with the next parameters=$($body | ConvertTo-Json)."
$pr = Invoke-WebRequest `
-Uri "https://api.github.com/repos/$Env:REPOSITORY_OWNER/Ed-Fi-ODS-Implementation/pulls" `
-Body $body `
-Headers $headers `
| ConvertFrom-Json `
| Select-Object -First 1
if (-not $pr.number) {
Write-Host "There's no matching PR."
return
}
Write-Host "Triggering workflows in the matching PR: https://github.com/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/pull/$($pr.number)"
$label = "Trigger from ODS repo"
Invoke-WebRequest `
-Method Post `
-ContentType 'application/json' `
-Uri "https://api.github.com/repos/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/issues/$($pr.number)/labels" `
-Body @(@{labels = @($label) } | ConvertTo-Json) `
-Headers $headers `
| Out-Null
Start-Sleep -Seconds 1
Invoke-WebRequest `
-Method Delete `
-Uri "https://api.github.com/repos/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/issues/$($pr.number)/labels/$([uri]::EscapeDataString($label))" `
-Headers $headers `
| Out-Null
Write-Output "EXIT_STEP=true">> $Env:GITHUB_ENV
}
function Assert-EnvironmentVariablesInitialized {
param (
[Parameter(Mandatory = $true)]
[string[]]$Names
)
foreach ($name in $Names) {
if (-not (Test-Path "Env:$name")) {
throw "The environment variable '$name' must be initialized."
}
}
}
function Invoke-Build {
Write-Host "Building Version $version" -ForegroundColor Cyan
Invoke-Step { DotnetClean }
Invoke-Step { Compile }
}
function Invoke-DotnetClean {
Invoke-Step { DotnetClean }
}
function Invoke-Restore {
Invoke-Step { Restore }
}
function Invoke-Publish {
Invoke-Step { Publish }
}
function Invoke-Tests {
Invoke-Step { Test }
}
function Invoke-Pack {
Invoke-Step { Pack }
}
function Invoke-CheckoutBranch {
Invoke-Step { CheckoutBranch }
}
function Invoke-InstallCredentialHandler {
Invoke-Step { InstallCredentialHandler }
}
function Invoke-StandardVersions {
Invoke-Step { StandardVersions }
}
function Invoke-StandardTag {
Invoke-Step { StandardTag }
}
function Invoke-TpdmTag {
Invoke-Step { TpdmTag }
}
Invoke-Main {
switch ($Command) {
DotnetClean { Invoke-DotnetClean }
Restore { Invoke-Restore }
Build { Invoke-Build }
Test { Invoke-Tests }
Pack { Invoke-Pack }
Publish { Invoke-Publish }
CheckoutBranch { Invoke-CheckoutBranch }
InstallCredentialHandler { Invoke-InstallCredentialHandler }
StandardVersions { Invoke-StandardVersions }
StandardTag { Invoke-StandardTag }
TpdmTag { Invoke-TpdmTag }
TriggerImplementationRepositoryWorkflows { TriggerImplementationRepositoryWorkflows }
default { throw "Command '$Command' is not recognized" }
}
}