-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.ps1
197 lines (170 loc) · 5.98 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
param (
[string]$task = "Deploy",
[string]$branch = (git rev-parse --abbrev-ref HEAD),
[string]$dbScriptVersion = "1db33b6ea826630b496e6b9a0d347eca425cf00b",
[string]$environment = "develop",
[string]$baseUrl = "http://localhost:51743"
)
# Constants for this solution:
$solutionName = "Uncas.BuildPipeline"
$serviceName = "Uncas.BuildPipeline.WindowsService"
$webProjectName = "Uncas.BuildPipeline.Web"
$nunitVersion = "2.6.0.12051"
$versionMajor = 1
$versionMinor = 0
$versionBuild = 0
$configuration = "Release"
$webserver = "localhost"
# Environment settings:
$databaseName = "BuildPipeline"
$webRootName = "BuildPipeline"
$destinationServiceName = $serviceName
if ($environment -ne "prod") {
$databaseName = "develop_$databaseName"
$webRootName = "develop.$webRootName"
$destinationServiceName = "develop-$serviceName"
}
# Working variables:
$baseDir = Resolve-Path .
$srcDir = "$baseDir\src"
$testDir = "$baseDir\test"
$outputDir = "$baseDir\output"
$collectDir = "$outputDir\collect"
$solutionFile = "$baseDir\$solutionName.sln"
$nunitFolder = "$baseDir\packages\NUnit.Runners.$nunitVersion\tools"
$nunitExe = "$nunitFolder\nunit-console.exe"
$year = (Get-Date).year
$connectionString = "Server=.\SqlExpress;Database=$databaseName;Integrated Security=true;"
$testDatabaseName = $databaseName + "_test"
$testConnectionString = "Server=.\SqlExpress;Database=$testDatabaseName;Integrated Security=true;"
$sourceWebFolder = "$collectDir\$webProjectName"
$version = "$versionMajor.$versionMinor.$versionBuild"
. "$baseDir\build_ext.ps1"
. "$baseDir\build_log.ps1"
$fullVersion = Get-FullVersion $version
function Clean {
Clean-Folder $collectDir
Clean-Folder $outputDir
}
function Init {
Clean
if (!(Test-Path $outputDir))
{
mkdir $outputDir
}
if (!(Test-Path $collectDir))
{
mkdir $collectDir
}
Generate-Assembly-Info `
-file "$baseDir\src\VersionInfo.cs" `
-company "Uncas" `
-product "$solutionName" `
-version $version `
-copyright "Copyright (c) $year, Uncas"
}
function Compile {
Init
msbuild $solutionFile /p:Configuration=$configuration /p:RunCodeAnalysis=false /t:Rebuild
ThrowIfErrorOnLastExecution
}
function UnitTest {
Compile
Run-Test "Uncas.BuildPipeline.Tests.Unit" $outputDir
}
function UpdateDb ($connectionString = $connectionString) {
$scriptSource = "https://raw.github.com/uncas/db-deployment/$dbScriptVersion/dbDeployment.ps1"
$script = "packages\dbDeployment-$dbScriptVersion.ps1"
#$script = "C:\Projects\OpenSource\db-deployment\dbDeployment.ps1"
if (!(Test-Path $script)) {
DownloadFile $scriptSource $script
}
. $script -connectionString $connectionString -scriptpath sql -clearScreenOnStart $false
}
function UpdateTestDb {
UpdateDb $testConnectionString
}
function UpdateDbs {
UpdateTestDb
UpdateDb
}
function IntegrationTest {
UnitTest
UpdateTestDb
$testProjectName = "Uncas.BuildPipeline.Tests.Integration"
ReplaceConnectionString "$testDir\$testProjectName\bin\$configuration\$testProjectName.dll.config" $testConnectionString
Run-Test $testProjectName $outputDir
}
function Collect {
IntegrationTest
Copy-WebApplication $srcDir $webProjectName $collectDir
copy $srcDir\$serviceName\bin\$configuration $collectDir\$serviceName -recurse
$sha = (git rev-parse HEAD)
$shortSha = $sha.Substring(0, 10)
$packageFileName = "BuildPipeline-$fullVersion-$shortSha.zip"
$packageFilePath = "$outputDir\$packageFileName"
Create-Zip $collectDir $packageFilePath
$params = @{branchName=$branch; packagePath=$packageFilePath; baseUrl=$baseUrl}
LogPackage @params
}
function DeployService (
$serviceName = $serviceName,
$sourceServiceFolder = "$collectDir\$serviceName",
$destinationMachine = ".",
$destinationServiceName = $destinationServiceName,
$destinationRootFolder = "C:\Services" ) {
$service = Get-Service -ComputerName $destinationMachine -Name "$destinationServiceName*"
if ($service -and ($service.Status -ne "Stopped")) {
# Stopping service:
$sw = GetAndStartStopwatch
$service.Stop()
StopAndWriteStopwatch $sw "Stop service"
# Waiting for the service to be completely shut down:
$sw = GetAndStartStopwatch
$waited = 0
$waitInterval = 1
$waitLimit = 120
while ($service -and ($service.Status -ne "Stopped")) {
Start-Sleep -s $waitInterval
$waited += $waitInterval
if ($waited -gt $waitLimit) { break }
$service = Get-Service -ComputerName $destinationMachine -Name $destinationServiceName
}
# Extra time to wait, such that files are not in use anymore:
Start-Sleep -s 5
StopAndWriteStopwatch $sw "Wait for service shut-down"
}
# Deploying files to service:
$destinationServiceFolder = "$destinationRootFolder\$destinationServiceName"
$sw = GetAndStartStopwatch
Sync-Folders $sourceServiceFolder $destinationServiceFolder
StopAndWriteStopwatch $sw "Deploy files to service"
# Installing service:
if (!$service) {
New-Service -name $destinationServiceName -binaryPathName "$destinationServiceFolder\$serviceName.exe"
$service = Get-Service -ComputerName $destinationMachine -Name $destinationServiceName
}
# Starting service:
$sw = GetAndStartStopwatch
$service.Start()
StopAndWriteStopwatch $sw "Start service"
}
function Deploy {
Collect
UpdateDb
if ($environment -eq "prod") {
ReplaceConnectionString "$collectDir\$serviceName\$serviceName.exe.config" $connectionString
DeployService
}
DeployWeb
}
if ($task -and (Test-Path "Function:\$task")) {
Write-Host "Invoking the task '$task' for branch '$branch'."
Invoke-Expression "$task"
}
else {
Write-Host "The task '$task' was not found."
}
if ($LASTEXITCODE -ne 0) {
throw "Build error - last exit code was $LASTEXITCODE!"
}