This repository was archived by the owner on Mar 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild.ps1
370 lines (303 loc) · 12.3 KB
/
Build.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
properties {
$IsTestingEnabled = $false
$IsObfuscateEnabled = $true
$IsInTeamBuild = $false
$IsDbRestoreEnabled = $false
$VariablePrefix = "Cms"
$DevProduct = 'Cms'
$DevEnvironment = 'LOCAL'
$CompanyName = 'Robust Haven Inc'
$RunInternalPackageDistribution = $false
$build_number = if ("$env:BUILD_NUMBER".length -gt 0) { "$env:BUILD_NUMBER" } else { "0" }
$build_vcs_number = if ("$env:BUILD_VCS_NUMBER".length -gt 0) { "$env:BUILD_VCS_NUMBER" } else { "0" }
$compileMessage = 'Executed Compile!'
$cleanMessage = 'Executed Clean!'
}
. .\BuildExt.ps1
Include "..\build\_init.ps1"
Framework "4.0x86"
# Framework "4.0x64"
TaskSetup {
#"Executing task setup"
}
TaskTearDown {
#"Executing task tear down"
}
formatTaskName {
param($taskName)
write-host $taskName -foregroundcolor Green
}
task default -depends Deploy
task Clean -depends IdentifyBuildVariables {
remove-item -force -recurse $script:TemporaryFolder -ErrorAction SilentlyContinue
remove-item -force -recurse $script:DeployFolder -ErrorAction SilentlyContinue
new-item $script:TemporaryFolder -itemType directory
if($IsInTeamBuild -eq $true)
{
new-item $script:DeployFolder -itemType directory
}
}
task Init -depends Clean, IdentifyBuildVariables {
foreach ($package in $script:PackageItems)
{
& $package.OnPreInitScriptBlock
if(($package.ProjectType -eq [ProjectTypes]::XCopy) -or ($package.ProjectType -eq [ProjectTypes]::Library) -or ($package.ProjectType -eq [ProjectTypes]::None)) {
continue;
}
$configFilePathInProject = $package.RootFolder + '\' + $package.ConfigurationFileName
Generate-EmptyXmlConfiguration -file $configFilePathInProject
if($IsInTeamBuild -eq $false)
{
# create project config from baseline
$sourceConfigName = $script:BuildFolder + '\Configuration\' + $package.SolutionName + '.' + $package.ConfigurationFileName
Generate-Config -XDTTask $script:XDTTask `
-FlexibleConfigTask $script:FlexibleConfigTask `
-EnvironmentMetaBase $script:EnvironmentMetaBase `
-BuildFolder $script:BuildFolder `
-RootFolder $package.RootFolder `
-TemporaryFolder $script:TemporaryFolder `
-SolutionName $package.SolutionName `
-NuspecId $package.NuspecId `
-DevProduct $DevProduct `
-DevBranch $script:DevBranch `
-DevEnvironment $script:DevEnvironment `
-DevTask $script:DevTask `
-DevId $script:DevId `
-IsInTeamBuild $IsInTeamBuild `
-AssemblyVersion $script:AssemblyVersion `
-SourceFile $sourceConfigName `
-DestinationFile $configFilePathInProject `
-PackageConfigurationFileName $package.ConfigurationFileName `
-PackageXdts $package.PackageXdts
}
}
}
task Compile -depends Init {
$SolutionPathCache = @()
foreach ($package in $script:PackageItems)
{
if( !(Test-Path $package.BuildOutput) )
{
new-item $package.BuildOutput -itemType directory
}
if($package.ProjectType -eq [ProjectTypes]::XCopy) {
cp -rec ($package.RootFolder + '\*') $package.BuildOutput
}
if($package.ProjectType -eq [ProjectTypes]::Website) {
Write-Host "Packaging Website " + $package.SolutionName
$BuildOutput = $package.BuildOutput
$SolutionPath = $package.SolutionFilePath
if($SolutionPathCache -notcontains $SolutionPath){
exec {
msbuild $SolutionPath "/t:Clean;Rebuild" "/p:_PackageTempDir=$BuildOutput" `
"/p:Configuration=Release" "/p:DeployOnBuild=true" `
"/p:DeployTarget=PipelinePreDeployCopyAllFilesToOneFolder" `
"/p:AutoParameterizationWebConfigConnectionStrings=false" `
"/fl" "/flp:v=quiet"
}
}
Remove-Item "$BuildOutput\*" -include .pdb -recurse
}
if($package.ProjectType -eq [ProjectTypes]::Library) {
Write-Host "Packaging Library " + $package.SolutionName
$BuildOutput = $package.BuildOutput
$SolutionPath = $package.SolutionFilePath
if($SolutionPathCache -notcontains $SolutionPath){
exec {
msbuild $SolutionPath "/t:Clean;Rebuild" "/p:Configuration=Release" `
"/fl" "/flp:v=quiet"
}
}
cp -rec ($package.RootFolder + '\bin\Release\*') $package.BuildOutput
Remove-Item "$BuildOutput\*" -include .pdb -recurse
}
if($package.ProjectType -eq [ProjectTypes]::DesktopApplication) {
Write-Host "Packaging DesktopApplication " + $package.SolutionName
$BuildOutput = $package.BuildOutput
$SolutionPath = $package.SolutionFilePath
if($SolutionPathCache -notcontains $SolutionPath){
exec {
msbuild $SolutionPath "/t:Clean;Rebuild" "/p:Configuration=Release" `
"/fl" "/flp:v=quiet"
}
}
cp -rec ($package.RootFolder + '\bin\Release\*') $package.BuildOutput
Remove-Item "$BuildOutput\*" -include .pdb -recurse
}
if($package.ProjectType -eq [ProjectTypes]::WindowService) {
Write-Host "Packaging WindowService " + $package.SolutionName
$BuildOutput = $package.BuildOutput
$ReleaseFolder = $package.RootFolder + '\bin\Release'
$SolutionPath = $package.SolutionFilePath
if($SolutionPathCache -notcontains $SolutionPath){
exec {
msbuild $SolutionPath "/t:Clean;Rebuild" "/p:Configuration=Release" `
"/fl" "/flp:v=quiet"
}
}
Remove-Item "$ReleaseFolder\*" -include .pdb -recurse
if( Test-Path "$ReleaseFolder\_PublishedWebsites" )
{
Remove-Item "$ReleaseFolder\_PublishedWebsites" -recurse
}
cp -rec ($ReleaseFolder + '\*') $package.BuildOutput
}
if(($package.ProjectType -eq [ProjectTypes]::XCopy) -or ($package.ProjectType -eq [ProjectTypes]::Library) -or ($package.ProjectType -eq [ProjectTypes]::None)) {
continue;
}
#wait till artifacts are created in Deploy folder
if($IsInTeamBuild -eq $true)
{
# create specific devenvironment destination config from baseline; compile once and promote to any supported env
foreach ($stagingEnvironment in $script:StagingEnvironments)
{
$destinationConfigName = $package.BuildOutput + '\' + $stagingEnvironment.EnvironmentName + '-' + $package.ConfigurationFileName
$sourceConfigName = $script:BuildFolder + '\Configuration\' + $package.SolutionName + '.' + $package.ConfigurationFileName
Generate-Config -XDTTask $script:XDTTask `
-FlexibleConfigTask $script:FlexibleConfigTask `
-EnvironmentMetaBase $script:EnvironmentMetaBase `
-BuildFolder $script:BuildFolder `
-RootFolder $package.RootFolder `
-TemporaryFolder $script:TemporaryFolder `
-SolutionName $package.SolutionName `
-NuspecId $package.NuspecId `
-DevProduct $DevProduct `
-DevBranch $script:DevBranch `
-DevEnvironment $stagingEnvironment.EnvironmentName `
-DevTask $script:DevTask `
-DevId 'BuildAgent' `
-IsInTeamBuild $IsInTeamBuild `
-AssemblyVersion $script:AssemblyVersion `
-SourceFile $sourceConfigName `
-DestinationFile $destinationConfigName `
-PackageConfigurationFileName $package.ConfigurationFileName `
-PackageXdts $package.PackageXdts
}
}
$SolutionPathCache += $package.SolutionFilePath
}
}
task Test -depends Compile {
if($script:IsTestingEnabled -eq $true)
{
}
}
task Package -depends Compile, Test {
foreach ($package in $script:PackageItems)
{
$nuspecFileNameExpected = $package.BuildOutput + '\' + $package.NuspecId + '.nuspec'
if( !(Test-Path $nuspecFileNameExpected) )
{
Generate-Nuspec -NuspecId $package.NuspecId `
-NuspecTitle $package.NuspecTitle `
-NuspecAuthors $package.NuspecAuthors `
-NuspecOwners $package.NuspecOwners `
-NuspecLicenseUrl $package.NuspecLicenseUrl `
-NuspecProjectUrl $package.NuspecProjectUrl `
-NuspecDescription $package.NuspecDescription `
-AssemblyVersion $script:AssemblyVersion `
-PackageConfig $package.PackageConfiguration `
-file $nuspecFileNameExpected
}
if(($package.ProjectType -ne [ProjectTypes]::XCopy) -and ($package.ProjectType -ne [ProjectTypes]::Library) -and ($package.ProjectType -ne [ProjectTypes]::None)) {
cp ($script:DevOpsFolder + '\Octopus\OctopusDeployment.psm1') $package.BuildOutput
cp ($script:DevOpsFolder + '\Octopus\DeployFailed.ps1') $package.BuildOutput
}
if($package.ProjectType -eq [ProjectTypes]::Website) {
cp ($script:DevOpsFolder + '\Octopus\Step.Www\*.*') $package.BuildOutput
}
if($package.ProjectType -eq [ProjectTypes]::WindowService) {
cp ($script:DevOpsFolder + '\Octopus\Step.WindowService\*.*') $package.BuildOutput
}
& $package.OnPackageScriptBlock
$args = @('pack', ('"{0}"' -f $nuspecFileNameExpected), '-OutputDirectory', ('"{0}"' -f $script:DeployFolder), '-Version', ('"{0}"' -f $script:AssemblyVersion), '-NoPackageAnalysis')
& "$script:NugetTask" $args | Write-Host
if (-not $?) {
throw "Error: Failed to execute NugetTask pack command"
}
}
}
task Push -depends Package {
foreach ($package in $script:PackageItems)
{
& $package.OnPushScriptBlock
$fileName = ("{0}\{1}.{2}.nupkg" -f $script:DeployFolder, $package.NuspecId, $script:AssemblyVersion)
WaitForFile($fileName)
Retry-Command -cmd {
$args = @('push', ('"{0}"' -f $fileName), '-s', ('"{0}"' -f $script:NugetDeployUrl), ('"{0}"' -f $script:NugetDeployApiKey) )
& "$script:NugetTask" $args | Write-Host
} `
-errorMessage ('Error pushing nuget package: {0}' -f $fileName) `
-retries 15 -secondsDelay 45
}
}
task Deploy -depends Push, Package {
}
task RunInitialSetup {
$DevId = Read-Host 'What is your DevId? e.g [ leblanc | {username} ]'
$DevEnvironment = Read-Host 'What is your DevEnvironment? e.g [ home.desktop | home.laptop | work | testdev | test | futuredev | futuretest | generaldev | generaltest | uat | production | training | dr | modeloffice ]'
$DevTask = Read-Host 'What is your DevTask? e.g [ integration | ui ]'
$tmp = "no"
if ($IsInTeamBuild -eq $true) {
$tmp = "yes"
}
$response = "Your response: DevProduct:{0}, DevId:{1}, DevEnvironment:{2}, DevTask:{3}, IsInTeamBuild:{4}" -f $DevProduct, $DevId, $DevEnvironment, $DevTask, $tmp
[Environment]::SetEnvironmentVariable($VariablePrefix + 'DevId', $DevId, "User")
[Environment]::SetEnvironmentVariable($VariablePrefix + 'DevEnvironment', $DevEnvironment, "User")
[Environment]::SetEnvironmentVariable($VariablePrefix + 'DevTask', $DevTask, "User")
Write-Host ''
Write-Host $response
}
task RunAfterUpdate -depends Init {
if($script:Databases.Count > 0)
{
$DbChangeManagementConfig = $script:BuildFolder + '\Configuration\DbChangeManagement.config'
$tmpFile = $script:TemporaryFolder + '\db.config'
exec {
msbuild $script:FlexibleConfigTask "/t:DoTask" `
("/p:BuildDirectory={0}" -f $script:BuildFolder) `
("/p:RootDirectory={0}" -f $script:ProjectDirectoryRoot) `
("/p:TemporaryDirectory={0}" -f $script:TemporaryFolder) `
("/p:DevProduct={0}" -f $DevProduct) `
("/p:DevBranch={0}" -f $script:DevBranch) `
("/p:DevEnvironment={0}" -f $script:DevEnvironment) `
("/p:DevTask={0}" -f $script:DevTask) `
("/p:DevId={0}" -f $script:DevId) `
("/p:IsInTeamBuild={0}" -f $IsInTeamBuild) `
("/p:AssemblyVersion={0}" -f $script:AssemblyVersion) `
("/p:SourceFile={0}" -f $DbChangeManagementConfig) `
("/p:DestinationFile={0}" -f $tmpFile) `
"/fl" `
("/flp:v=diag;logfile={0}\FlexibleConfigTask.log" -f $script:TemporaryFolder)
}
foreach ($database in $script:Databases)
{
exec {
msbuild $script:DatabaseChangeManagementTask "/t:DoTask" `
("/p:BuildDirectory={0}" -f $script:BuildFolder) `
("/p:RootDirectory={0}" -f $script:ProjectDirectoryRoot) `
("/p:TemporaryDirectory={0}" -f $script:TemporaryFolder) `
("/p:DevProduct={0}" -f $DevProduct) `
("/p:DevBranch={0}" -f $script:DevBranch) `
("/p:DevEnvironment={0}" -f $script:DevEnvironment) `
("/p:DevTask={0}" -f $script:DevTask) `
("/p:DevId={0}" -f $script:DevId) `
("/p:IsInTeamBuild={0}" -f $IsInTeamBuild) `
("/p:AssemblyVersion={0}" -f $script:AssemblyVersion) `
("/p:TaskConfiguration={0}" -f $tmpFile) `
("/p:PrefixedName={0}" -f $database.PrefixedName) `
("/p:DatabaseName={0}" -f $database.DatabaseName) `
("/p:IsDbRestoreEnabled={0}" -f $IsDbRestoreEnabled) `
("/p:VcsPath={0}" -f $script:VcsPath) `
"/fl" `
("/flp:v=diag;logfile={0}\DatabaseChangeManagementTask.log" -f $script:TemporaryFolder)
}
}
}
remove-item -force -recurse $script:TemporaryFolder -ErrorAction SilentlyContinue
#InfraChangesSync,
# throw "I failed on purpose!"
}
task ? -Description "Helper to display task info" {
Write-Documentation
}