forked from Ed-Fi-Alliance-OSS/Ed-Fi-ODS-Implementation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.githubactions.ps1
269 lines (221 loc) · 7.48 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
# 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")]
$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
)
$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
}
}
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($odsBranch -ne ''){
$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 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 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 }
}
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 }
StandardVersions { Invoke-StandardVersions }
default { throw "Command '$Command' is not recognized" }
}
}