-
Notifications
You must be signed in to change notification settings - Fork 1
/
Build.ps1
111 lines (90 loc) · 3.14 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
[CmdletBinding()]
Param()
$ModuleName = "PSSplunkSearch"
$Description = "Run Splunk log searches from PowerShell"
#
# Grab nuget bits, install modules, start build.
#
Write-Verbose -Verbose -Message "$(Get-Date): Preparing environment"
$Stopwatch = [system.diagnostics.stopwatch]::StartNew()
Get-PackageProvider -Name NuGet -ForceBootstrap | Out-Null
Import-Module PowerShellGet -ErrorAction Stop
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted
Install-Module Pester,PSScriptAnalyzer,PSModuleBuild
#
# Build the module
#
$ModuleInformation = @{
Path = "$ENV:APPVEYOR_BUILD_FOLDER\Source"
TargetPath = "$ENV:APPVEYOR_BUILD_FOLDER\$ModuleName"
ModuleName = $ModuleName
ReleaseNotes = (git log -1 --pretty=%s) | Out-String
Author = "Martin Pugh (@TheSurlyAdm1n)"
ModuleVersion = $ENV:APPVEYOR_BUILD_VERSION
Company = "www.thesurlyadmin.com"
Description = $Description
ProjectURI = "https://github.com/martin9700/$ModuleName"
LicenseURI = "https://github.com/martin9700/$ModuleName/blob/main/LICENSE"
Include = "Includes"
PassThru = $true
}
Invoke-PSModuleBuild @ModuleInformation
#
# Analyze source
#
Write-Verbose -Verbose -Message "$(Get-Date): Analyzing code"
$Results = Invoke-ScriptAnalyzer -Path "$ENV:APPVEYOR_BUILD_FOLDER\Source" -Recurse
$Results | Format-Table
$Errors = $Results | Where-Object Severity -eq "Error"
If ($Errors)
{
Write-Error "$(Get-Date): One or more Script Analyzer errors/warnings where found. Build cannot continue!" -ErrorAction Stop
}
Write-Verbose -Verbose -Message "$(Get-Date): Script passed"
#
# Run tests
#
$TestsPath = Join-Path -Path "$ENV:APPVEYOR_BUILD_FOLDER\$ModuleName" -ChildPath Tests
If (Test-Path -Path $TestsPath)
{
$TestResults = Invoke-Pester -PassThru -OutputFormat NUnitXml -OutputFile ".\TestResults.xml"
(New-Object 'System.Net.WebClient').UploadFile("https://ci.appveyor.com/api/testresults/nunit/$($env:APPVEYOR_JOB_ID)",(Resolve-Path ".\TestResults.xml"))
If ($TestResults.FailedCount -gt 0)
{
$TestResults | Format-List *
Write-Error "$(Get-Date): Failed '$($TestResults.FailedCount)' tests, build failed" -ErrorAction Stop
}
}
Else
{
Write-Verbose -Verbose -Message "$(Get-Date): No tests detected, skipping"
}
#
# Publish module
#
If ($ENV:PSGalleryAPIKey)
{
$PublishInformation = @{
Path = "$ENV:APPVEYOR_BUILD_FOLDER\$ModuleName"
Force = $true
NuGetApiKey = $ENV:PSGalleryAPIKey
}
Try {
Write-Verbose "$(Get-Date):" -Verbose
Write-Verbose "$(Get-Date): Merge detected, publishing $ModuleName to Microsoft" -Verbose
Publish-Module @PublishInformation -ErrorAction Stop -Verbose
Write-Host "`n`nPublish to PSGallery successful" -ForegroundColor Green
}
Catch {
Write-Error "Publish to PSGallery failed because ""$_""" -ErrorAction Stop
}
}
Else
{
Write-Warning "$(Get-Date): Pull request detected, no publish needed"
}
#
# Completed
#
$Stopwatch.Stop()
Write-Verbose -Verbose -Message "$(Get-Date): Build completed in $($Stopwatch.Elapsed)"