-
Notifications
You must be signed in to change notification settings - Fork 33
/
build.githubactions.ps1
596 lines (497 loc) · 20.2 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
# 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","StandardVersions", "InstallCredentialHandler","WorkflowCheck","ComparePackageVersions","CreateOrUpdateRepositoriesJson","TestBranchExists")]
$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,
[ValidateSet('4.0.0', '5.2.0')]
[string] $StandardVersion,
[string]$Url,
[string]$ExpectedStatus = 'completed',
[string[]]$ExpectedConclusions = 'success',
[string]$StatusEnvName,
[string]$RepoName,
[string]$BranchName
)
$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 ){
nuget pack $NuspecFilePath -OutputDirectory $packageOutput -Version $version -Properties configuration=$Configuration -Properties id=$PackageName -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 Implementation 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."
}
$userProfilePath = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile);
if ($userProfilePath -ne '') {
$profilePath = $userProfilePath
} else {
$profilePath = $env:UserProfile
}
$tempPath = [System.IO.Path]::GetTempPath()
$pluginLocation = [System.IO.Path]::Combine($profilePath, ".nuget", "plugins");
$tempZipLocation = [System.IO.Path]::Combine($tempPath, "CredProviderZip");
$localNetcoreCredProviderPath = [System.IO.Path]::Combine("netcore", "CredentialProvider.Microsoft");
$fullNetcoreCredProviderPath = [System.IO.Path]::Combine($pluginLocation, $localNetcoreCredProviderPath)
$sourceUrl = 'https://github.com/microsoft/artifacts-credprovider/releases/download/v1.1.1/Microsoft.Net6.NuGet.CredentialProvider.zip'
$zipFile = 'Microsoft.Net6.NuGet.CredentialProvider.zip'
$pluginZip = Join-Path ([IO.Path]::GetTempPath()) $zipFile
Write-Host "Downloading $sourceUrl to $pluginZip"
try {
$client = New-Object System.Net.WebClient
$client.DownloadFile($sourceUrl, $pluginZip)
} catch {
Write-Error "Unable to download $sourceUrl to the location $pluginZip"
}
Write-Host "Download complete."
if (-not (Test-Path $pluginZip)) {
Write-Warning "Microsoft.Net6.NuGet.CredentialProvider file '$zipFile' not found."
return
}
$tempZipLocation = Join-Path ([IO.Path]::GetTempPath()) 'CredProviderZip'
if ($zipFile.EndsWith('.zip')) {
Write-Host "Extracting $zipFile..."
if (Test-Path $pluginZip) {
Expand-Archive -Force -Path $pluginZip -DestinationPath $tempZipLocation
}
$tempNetcorePath = Join-Path $tempZipLocation 'plugins' $localNetcoreCredProviderPath
Write-Verbose "Copying Credential Provider from $tempNetcorePath to $fullNetcoreCredProviderPath"
if (Test-Path $tempNetcorePath) {
Copy-Item $tempNetcorePath -Destination $fullNetcoreCredProviderPath -Force -Recurse
} else {
Write-Error "The Credential Provider was not found at $tempNetcorePath."
}
# Remove $tempZipLocation directory
Write-Verbose "Removing the Credential Provider temp directory $tempZipLocation"
Remove-Item $tempZipLocation -Force -Recurse
}
}
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 WorkflowCheck {
$headers = @{
Authorization = "Bearer $env:EDFI_ODS_TOKEN"
Accept = "application/vnd.github.v3+json"
}
$response = Invoke-RestMethod -Uri $Url -Headers $headers
if ( $response.workflow_runs.Count -gt 0) {
Write-Host "Found workflow runs for URL: $Url."
$runTracker = @{} # Create an empty hash table to track printed run_numbers
$response.workflow_runs | Where-Object { $_.status -eq $ExpectedStatus -and $ExpectedConclusions -contains $_.conclusion } |
Sort-Object -Property run_number -Descending | Select-Object -First 1| ForEach-Object {
if (-not $runTracker.ContainsKey($_.run_number)) {
Write-Host "Run ID: $($_.id)"
Write-Host "Run Name: $($_.name)"
Write-Host "Run Number: $($_.run_number)"
Write-Host "Event: $($_.event)"
Write-Host "Status: $($_.status)"
Write-Host "Conclusion: $($_.conclusion)"
Write-Host "Created At: $($_.created_at)"
Write-Host "Updated At: $($_.updated_at)"
Write-Host "URL: $($_.html_url)"
# Mark this run_number as printed
$runTracker[$_.run_number] = $true
echo "$StatusEnvName=true" >> $Env:GITHUB_ENV
echo "rebuild_database_templates_lastrunId=$($_.id)" >> $Env:GITHUB_ENV
}
}
} else {
Write-Host "No matching workflow runs found for URL: $Url."
echo "$StatusEnvName=false" >> $Env:GITHUB_ENV
}
}
function ComparePackageVersions {
# API URLs for branch and main
$url_branch = "https://api.github.com/repos/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/contents/configuration.packages.json?ref=$Env:current_branch"
$url_main = "https://api.github.com/repos/Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation/contents/configuration.packages.json?ref=main"
$headers = @{
Authorization = "Bearer $env:EDFI_ODS_TOKEN"
Accept = "application/vnd.github.v3+json"
}
# Get the file content for the branch
$response_branch = Invoke-RestMethod -Uri $url_branch -Headers $headers
$content_branch = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($response_branch.content))
$json_branch = $content_branch | ConvertFrom-Json
# Get the file content for the main branch
$response_main = Invoke-RestMethod -Uri $url_main -Headers $headers
$content_main = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($response_main.content))
$json_main = $content_main | ConvertFrom-Json
# Extract the PackageVersion for the package name
$branch_version = $json_branch.packages.$PackageName.PackageVersion
$main_version = $json_main.packages.$PackageName.PackageVersion
# Convert versions to a comparable format
$branch_version = [Version]$branch_version
$main_version = [Version]$main_version
# Compare the versions and print
if ($branch_version -gt $main_version) {
Write-Host "Package versions are different:"
Write-Host "Branch ($branch): $branch_version"
Write-Host "Main: $main_version"
echo "$StatusEnvName=true" >> $Env:GITHUB_ENV
} else {
Write-Host "Package versions are the same:"
Write-Host "Branch ($branch): $branch_version"
Write-Host "Main: $main_version"
echo "$StatusEnvName=false" >> $Env:GITHUB_ENV
}
}
function CreateOrUpdateRepositoriesJson {
$FilePath = "$PSScriptRoot/repositories.json"
$headers = @{
Authorization = "Bearer $env:EDFI_ODS_TOKEN"
Accept = "application/vnd.github.v3+json"
}
# Fetch the last commit details
$response = Invoke-RestMethod -Uri $url -Headers $headers
#Define the messages to ignore
$ignoreMessages = @(
"Updating for new CodeGen version",
"Updating for new Extensions- TPDM, Sample, Homograph package version",
"Updating for new EdFi.Ods.Minimal.Template , EdFi.Ods.Minimal.Template.PostgreSQL ,EdFi.Ods.Populated.Template ,EdFi.Ods.Populated.Template.PostgreSQL package version"
)
if ($null -eq $response -or $response.Length -eq 0) {
Write-Host "API response is null or empty. Stopping the build."
exit 1
}
# # Filter out the commits with the ignored messages
$filteredCommits = $response | Where-Object { $ignoreMessages -notcontains $_.commit.message }
if ($null -eq $filteredCommits) {
Write-Host "Stopping the build. filteredCommits returns null"
exit 1
}
if ($filteredCommits -and $filteredCommits.Count -gt 0) {
$latestCommit = $filteredCommits[0]
$commitMessage = $latestCommit.commit.message
$commitId = $latestCommit.sha
} else {
Write-Host "No valid commits found after filtering. Stopping the build."
exit 1
}
$jsonContent = $null
# Check if the file exists
if (Test-Path $FilePath) {
Write-Host "File Exists $FilePath ."
# Read the existing content
$jsonContent = Get-Content -Path $FilePath -Raw | ConvertFrom-Json
if ($null -eq $jsonContent.repositories) {
$jsonContent.repositories = @()
}
# Find the repository entry or create a new one
$repoEntry = $jsonContent.repositories | Where-Object { $_.repo_name -eq $RepoName }
Write-Host "Printing before checking where new Commit ID exists or not "
Get-Content $filePath | Write-Host
if ($repoEntry) {
# Check if the existing commit ID is different from the latest commit ID
if ($repoEntry.commit_id -ne $commitId) {
# Update the entry if the commit ID is different
Write-Host "Printing before updating new Commit ID"
Get-Content $filePath | Write-Host
Write-Host "New commit ID is different with old Commit Id "
$repoEntry.repo_name = $RepoName
$repoEntry.commit_message = $commitMessage
$repoEntry.commit_id = $commitId
$repoEntry.IscommitChanged = 'true'
Write-Host "Updated repository entry for $RepoName with new commit ID."
Write-Host "IscommitChanged is true."
} else {
Write-Host "The commit ID for $RepoName is already up to date."
Write-Host "IscommitChanged is false."
$repoEntry.IscommitChanged = 'false'
}
} else {
# Add new entry if the repository entry does not exist
$newRepoEntry = @{
repo_name = $RepoName
commit_message = $commitMessage
commit_id = $commitId
IscommitChanged = 'true'
}
$jsonContent.repositories += $newRepoEntry
Write-Host "Added new repository entry for $RepoName."
Write-Host "IscommitChanged is true."
}
} else {
# Create a new file with the initial content
Write-Host "File not Exists $FilePath .new file is created"
$jsonContent = @{
repositories = @(
@{
repo_name = $RepoName
commit_message = $CommitMessage
commit_id = $CommitId
IscommitChanged = 'true'
}
)
}
Write-Host "IscommitChanged is true."
}
# Write updated content back to the file
$jsonContent | ConvertTo-Json -Depth 4 | Out-File -FilePath $FilePath -Encoding UTF8
# Print the repositories.json content for verification
Write-Host "Print the repositories.json content for verification"
Get-Content $filePath | Write-Host
}
function TestBranchExists {
$headers = @{
Authorization = "Bearer $env:EDFI_ODS_TOKEN"
Accept = "application/vnd.github.v3+json"
}
try {
$response = Invoke-RestMethod -Uri $url -Headers $headers
if ($response.name -eq $BranchName) {
Write-Host "Branch '$BranchName' exists in repository '$RepoName'."
return
} else {
Write-Host "Branch '$BranchName' does not exist in repository '$RepoName'."
Write-Host "Stopping the build."
exit 1
}
} catch {
Write-Host "Branch '$BranchName' does not exist in repository '$RepoName'."
Write-Host "Stopping the build."
exit 1
}
}
function Invoke-StandardVersions {
Invoke-Step { StandardVersions }
}
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-WorkflowCheck {
Invoke-Step { WorkflowCheck }
}
function Invoke-ComparePackageVersions {
Invoke-Step { ComparePackageVersions }
}
function Invoke-CreateOrUpdateRepositoriesJson {
Invoke-Step { CreateOrUpdateRepositoriesJson }
}
function Invoke-TestBranchExists {
Invoke-Step { TestBranchExists }
}
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 }
WorkflowCheck { Invoke-WorkflowCheck }
ComparePackageVersions { Invoke-ComparePackageVersions }
CreateOrUpdateRepositoriesJson { Invoke-CreateOrUpdateRepositoriesJson }
TestBranchExists { Invoke-TestBranchExists }
default { throw "Command '$Command' is not recognized" }
}
}