/
build_utils.ps1
320 lines (281 loc) · 9.71 KB
/
build_utils.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
<#
.SYNOPSIS
Publishes a PowerShell Module to a Network Share.
.Example
$ModuleInfo = @{
RepoName = 'PoshRepo'
RepoPath = '\\server\PoshRepo'
ModuleName = 'BuildHelpersTest'
ModulePath = '.\BuildHelpersTest.psd1'
}
Publish-SMBModule @ModuleInfo
#>
function Publish-SMBModule
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
[string] $RepoName,
[Parameter(Mandatory=$true)]
[string] $RepoPath,
[Parameter(Mandatory=$true)]
[string] $ModuleName,
[Parameter(Mandatory=$true)]
[string] $ModulePath,
[Parameter(Mandatory=$true)]
[int] $BuildNumber
)
# Just force it x.x
Install-Nuget
# Resister SMB Share as Repository
Write-Verbose ("Checking if Repo: {0} is registered" -f $RepoName)
if(!(Get-PSRepository -Name $RepoName -ErrorAction SilentlyContinue))
{
Write-Verbose ("Registering Repo: {0}" -f $RepoName)
Register-PSRepository -Name $RepoName -SourceLocation $RepoPath -InstallationPolicy Trusted
}
# Update Existing Manifest
# - Source Manifest controls Major/Minor
# - Jenkins Controls Build Number.
Write-Verbose ("Checking if Module: {0} is registered" -f $ModuleName)
if(Find-Module -Repository $RepoName -Name $ModuleName -ErrorAction SilentlyContinue)
{
Write-Verbose ("Updating Manifest for: {0}" -f $ModuleName)
$version = (Get-Module -FullyQualifiedName $ModulePath -ListAvailable).Version | Select-Object Major, Minor
$newVersion = New-Object Version -ArgumentList $version.major, $version.minor, $BuildNumber
Update-ModuleManifest -Path $ModulePath -ModuleVersion $newVersion
}
# Publish ModuleInfo
# - Fails if nuget install needs confirmation in NonInteractive Mode.
Write-Verbose ("Publishing Module: {0}" -f $ModuleName)
try
{
$env:PSModulePath += ";$PSScriptRoot"
Publish-Module -Repository $RepoName -Name $ModuleName
}
catch [System.Exception]
{
# Write-Error "Publish Failed"
throw($_.Exception)
}
}
<#
.SYNOPSIS
Create a Zip Archive Build Artifact
#>
function Publish-ArtifactZip
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
[string] $ModuleName,
[Parameter(Mandatory=$true)]
[int] $BuildNumber
)
# Creating project artifact
$artifactDirectory = Join-Path $pwd "artifacts"
$moduleDirectory = Join-Path $pwd "$ModuleName"
$manifest = Join-Path $moduleDirectory "$ModuleName.psd1"
$zipFilePath = Join-Path $artifactDirectory "$ModuleName.zip"
$version = (Get-Module -FullyQualifiedName $manifest -ListAvailable).Version | Select-Object Major, Minor
$newVersion = New-Object Version -ArgumentList $version.major, $version.minor, $BuildNumber
Update-ModuleManifest -Path $manifest -ModuleVersion $newVersion
Add-Type -assemblyname System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($moduleDirectory, $zipFilePath)
}
<#
.SYNOPSIS
Create a Nuget Package for the Build Artifact
Simple wrapper around DscResourceTestHelper Module
#>
function Publish-NugetPackage
{
param
(
[Parameter(Mandatory=$true)]
[string] $packageName,
[Parameter(Mandatory=$true)]
[string] $author,
[Parameter(Mandatory=$true)]
[int] $BuildNumber,
[Parameter(Mandatory=$true)]
[string] $owners,
[string] $licenseUrl,
[string] $projectUrl,
[string] $iconUrl,
[string] $packageDescription,
[string] $releaseNotes,
[string] $tags,
[Parameter(Mandatory=$true)]
[string] $destinationPath
)
$CurrentVersion = (Get-Module -FullyQualifiedName "./$ModuleName" -ListAvailable).Version | Select-Object Major, Minor
$version = New-Object Version -ArgumentList $CurrentVersion.major, $CurrentVersion.minor, $BuildNumber
$moduleInfo = @{
packageName = $packageName
version = ($version.ToString())
author = $author
owners = $owners
licenseUrl = $licenseUrl
projectUrl = $projectUrl
packageDescription = $packageDescription
tags = $tags
destinationPath = $destinationPath
}
# Creating NuGet package artifact
Import-Module -Name DscResourceTestHelper
New-Nuspec @moduleInfo
$nuget = "$env:ProgramData\Microsoft\Windows\PowerShell\PowerShellGet\nuget.exe"
. $nuget pack "$destinationPath\$packageName.nuspec" -outputdirectory $destinationPath
}
<#
.SYNOPSIS
Used to address problem running Publish-Module in NonInteractive Mode when Nuget is not present.
.NOTES
https://github.com/OneGet/oneget/issues/173
https://github.com/PowerShell/PowerShellGet/issues/79
If Build Agent does not have permission to ProgramData Folder, may want to use the user specific folder.
Package Provider Expected Locations (x86):
C:\Program Files (x86)\PackageManagement\ProviderAssemblies
C:\Users\{USER}\AppData\Local\PackageManagement\ProviderAssemblies
#>
function Install-Nuget
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$false)]
[switch] $Force = $false
)
# Force Update Provider
Install-PackageProvider Nuget -Force
$sourceNugetExe = "http://nuget.org/nuget.exe"
$powerShellGetDir = "$env:ProgramData\Microsoft\Windows\PowerShell\PowerShellGet"
if(!(Test-Path -Path $powerShellGetDir))
{
New-Item -ItemType Directory -Path $powerShellGetDir -Force
}
if(!(Test-Path -Path "$powerShellGetDir\nuget.exe") -or $Force)
{
Invoke-WebRequest $sourceNugetExe -OutFile "$powerShellGetDir\nuget.exe"
}
}
<#
.SYNOPSIS
Quick Attempt to create a HTML page to publish during CI builds
#>
function Publish-CoverageHTML
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)]
[PSCustomObject] $TestResults,
[Parameter(Mandatory=$true)]
[int] $BuildNumber,
[Parameter(Mandatory=$true)]
[string] $Repository,
[Parameter(Mandatory=$true)]
[int] $PercentCompliance,
[Parameter(Mandatory=$true)]
[string] $OutputFile
)
$HitTable = $TestResults.CodeCoverage.HitCommands | ConvertTo-Html -Fragment
$MissedTable = $TestResults.CodeCoverage.MissedCommands | ConvertTo-Html -Fragment
$CommandsAnalyzed = $TestResults.CodeCoverage.NumberOfCommandsAnalyzed
$FilesAnalyzed = $TestResults.CodeCoverage.NumberOfFilesAnalyzed
$CommandsExecuted = $TestResults.CodeCoverage.NumberOfCommandsExecuted
$CommandsMissed = $TestResults.CodeCoverage.NumberOfCommandsMissed
$CoveragePercent = [math]::Round(($CommandsExecuted / $CommandsAnalyzed * 100), 2)
$Date = (Get-Date)
$AnalyzedFiles = "";
foreach ($file in $TestResults.CodeCoverage.analyzedfiles)
{
$AnalyzedFiles += "<li>$file</li>"
}
$BuildColor = 'green'
if($CoveragePercent -lt $PercentCompliance)
{
$BuildColor = 'red'
}
$html = "
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='UTF-8'>
<title>Document</title>
<style>
body{font-family: sans-serif;}
h2{margin: 0;}
table{width: 100%;text-align: left;border-collapse: collapse;margin-top: 10px;}
table th, table td {padding: 2px 16px 2px 0;border-bottom: 1px solid #9e9e9e;}
table thead th {border-width: 2px;}
.green{color: green;}
.red{color: red;}
.container {margin: 0 auto;width: 70%;}
.analyzed, .coverage, .hit, .missed {padding: 10px;margin-bottom: 20px;border-radius: 6px;}
.analyzed{ background: #bde6ff;}
.coverage{ background: #ececec;}
.hit{ background: #beffc1; }
.hit table td, .hit table th{ border-color: #5d925f;}
.missed{ background: #ffc9c9;}
.missed table td, .missed table th{ border-color: #804646;}
</style>
</head>
<body>
<div class='container'>
<h1>Pester Coverage Report</h1>
<!-- CI Meta -->
<ul>
<li><strong>Build:</strong> $($BuildNumber)</li>
<li><strong>Repo:</strong> $($Repository)</li>
<li><strong>Generated on:</strong> $($Date)</li>
</ul>
<!-- Overview -->
<div class='coverage'>
<h2>Coverage: <span class='$($BuildColor)'>$($CoveragePercent) %</span></h2>
<table>
<thead>
<tr>
<th>Metric</th>
<th>Value</th>
</tr>
</thead>
<tr>
<td>Commands Analyzed</td>
<td>$($CommandsAnalyzed)</td>
</tr>
<tr>
<td>Files Analyzed</td>
<td>$($FilesAnalyzed)</td>
</tr>
<tr>
<td>Commands Executed</td>
<td>$($CommandsExecuted)</td>
</tr>
<tr>
<td>Commands Missed</td>
<td>$($CommandsMissed)</td>
</tr>
</table>
</div>
<div class='analyzed'>
<h2>Files Analyzed: $($FilesAnalyzed)</h2>
<ul>$($AnalyzedFiles)</ul>
</div>
<div class='hit'>
<h2>Hit: $($CommandsExecuted)</h2>
$($HitTable)
</div>
<div class='missed'>
<h2>Missed: $($CommandsMissed)</h2>
$($MissedTable)
</div>
</div>
</body>
</html>
";
Set-Content -Path $OutputFile -Value $html
}