-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSummit2018.build.ps1
110 lines (86 loc) · 3.66 KB
/
Summit2018.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
# Include: Settings.
. './Summit2018.settings.ps1'
# Include: build_utils.
. './build_utils.ps1'
#Synopsis: Run/Publish Tests and Fail Build on Error.
task Test Clean, InstallDependencies, RunTests, ConfirmTestsPassed
#Synopsis: Run full Pipeline.
task . Test, PublishNuget
#Synopsis: Install dependencies.
task InstallDependencies {
if (Get-Command nuget.exe -ErrorAction SilentlyContinue) {
nuget restore -source SummitBin -outputdirectory packages
}
if (!(Test-Path $ModulePath\lib)) {
mkdir $ModulePath\lib
}
get-item .\packages\**\lib\* | copy-item -Destination $ModulePath\lib
}
#Synopsis: Clean Artifact directory.
task Clean {
if (Test-Path -Path $Artifacts) {
Remove-Item "$Artifacts/*" -Recurse -Force
}
New-Item -ItemType Directory -Path $Artifacts -Force
}
#Synopsis: Analyze code.
task Analyze {
$scriptAnalyzerParams = @{
Path = $ModulePath
ExcludeRule = @('PSPossibleIncorrectComparisonWithNull', 'PSUseToExportFieldsInManifest')
Severity = @('Error', 'Warning')
Recurse = $true
Verbose = $false
}
$saResults = Invoke-ScriptAnalyzer @scriptAnalyzerParams
# Save the results.
$saResults | ConvertTo-Json | Set-Content (Join-Path $Artifacts "ScriptAnalysisResults.json")
}
#Synopsis: Run tests.
task RunTests {
$invokePesterParams = @{
OutputFile = (Join-Path $Artifacts "TestResults.xml")
OutputFormat = "NUnitXml"
Strict = $true
PassThru = $true
Verbose = $false
EnableExit = $false
CodeCoverage = (Get-ChildItem -Path "$ModulePath\*.ps1" -Exclude "*.Tests.*" -Recurse).FullName
CodeCoverageOutputFile = (Join-Path $Artifacts "CodeCoverageResults.xml")
}
$testResults = Invoke-Pester @invokePesterParams
$testResults | ConvertTo-Json -Depth 5 | Set-Content (Join-Path $Artifacts "PesterResults.json")
}
#Synopsis: Confirm that tests passed.
task ConfirmTestsPassed {
# Fail Build after reports are created, this allows CI to publish test results before failing
[xml]$xml = Get-Content (Join-Path $Artifacts "TestResults.xml")
$numberFails = $xml."test-results".failures
assert($numberFails -eq 0) ('Failed "{0}" unit tests.' -f $numberFails)
# Fail Build if Coverage is under requirement
$json = Get-Content (Join-Path $Artifacts "PesterResults.json") | ConvertFrom-Json
$overallCoverage = [Math]::Floor(($json.CodeCoverage.NumberOfCommandsExecuted / $json.CodeCoverage.NumberOfCommandsAnalyzed) * 100)
assert($OverallCoverage -gt $PercentCompliance) ('A Code Coverage of "{0}" does not meet the build requirement of "{1}"' -f $overallCoverage, $PercentCompliance)
}
#Synopsis: Publish to SMB File Share.
task Publish {
$moduleInfo = @{
RepositoryName = $Settings.SMBRepositoryName
RepositoryPath = $Settings.SMBRepositoryPath
ModuleName = $ModuleName
ModulePath = "$ModulePath\$ModuleName.psd1"
BuildNumber = $BuildNumber
}
Publish-PSModule @moduleInfo -Verbose
}
task PublishNuget {
$newVersion = New-Object version -ArgumentList 1, 0, 0, $BuildNumber
"Version is $newVersion"
$Public = @(Get-ChildItem -Path $ModulePath\Public\*.ps1 -ErrorAction SilentlyContinue)
$Functions = $public.basename
Update-ModuleManifest -Path $ModulePath\$ModuleName.psd1 -ModuleVersion $newVersion -FunctionsToExport $Functions
$t = [xml] (Get-Content .\$ModuleName.nuspec)
$t.package.metadata.version = $newVersion.ToString()
$t.Save(".\$ModuleName.nuspec")
nuget pack $ModuleName.nuspec -basepath $ModulePath -NoPackageAnalysis -outputdirectory $ENV:Build_ArtifactStagingDirectory
}