-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsakeBuild.ps1
207 lines (186 loc) · 8.5 KB
/
psakeBuild.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
properties {
# $unitTests = "$PSScriptRoot\Tests\unit"
$unitTests = Get-ChildItem .\Tests\*Unit_Tests.ps1
$mofTests = Get-ChildItem .\Tests\*MOF_Generation_Tests.ps1
$DSCResources = Get-ChildItem *.psd1,*.psm1 -Recurse
# originalPath is the one containing the .psm1 and .psd1
$originalPath = $PSScriptRoot
# pathInModuleDir is the path where the symbolic link will be created which points to your repo
$pathInModuleDir = 'C:\Program Files\WindowsPowerShell\Modules\InstallHubot'
$ProjectRoot = $ENV:APPVEYOR_BUILD_FOLDER
if(-not $ProjectRoot)
{
$ProjectRoot = Resolve-Path "$PSScriptRoot\.."
}
}
task default -depends Analyze, Test, MOFTestDeploy, MOFTest, BuildArtifact, Deploy
task TestProperties {
Assert ($build_version -ne $null) "build_version should not be null"
}
task Analyze {
ForEach ($resource in $DSCResources)
{
try
{
Write-Output "Running ScriptAnalyzer on $($resource)"
if ($env:APPVEYOR)
{
Add-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Running
$timer = [System.Diagnostics.Stopwatch]::StartNew()
}
$saResults = Invoke-ScriptAnalyzer -Path $resource.FullName -Verbose:$false
if ($saResults) {
$saResults | Format-Table
$saResultsString = $saResults | Out-String
if ($saResults.Severity -contains 'Error' -or $saResults.Severity -contains 'Warning')
{
if ($env:APPVEYOR)
{
Add-AppveyorMessage -Message "PSScriptAnalyzer output contained one or more result(s) with 'Error or Warning' severity.`
Check the 'Tests' tab of this build for more details." -Category Error
Update-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Failed -ErrorMessage $saResultsString
}
Write-Error -Message "One or more Script Analyzer errors/warnings where found in $($resource). Build cannot continue!"
}
else
{
Write-Output "All ScriptAnalyzer tests passed"
if ($env:APPVEYOR)
{
Update-AppveyorTest -Name "PsScriptAnalyzer" -Outcome Passed -StdOut $saResultsString -Duration $timer.ElapsedMilliseconds
}
}
}
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output $ErrorMessage
Write-Output $FailedItem
Write-Error "The build failed when working with $($resource)."
}
}
}
task Test {
ForEach ($unitTest in $unitTests)
{
$testResults = .\Tests\appveyor.pester.ps1 -Test -TestPath $unitTest
if ($testResults.FailedCount -gt 0) {
$testResults | Format-List
Write-Error -Message 'One or more Pester unit tests failed. Build cannot continue!'
}
}
}
task MOFTestDeploy -depends Analyze, Test {
try
{
if ($env:APPVEYOR)
{
# copy into the userprofile in appveyor so the module can be loaded
Start-Process -FilePath 'robocopy.exe' -ArgumentList "$PSScriptRoot $env:USERPROFILE\Documents\WindowsPowerShell\Modules\InstallHubot /S /R:1 /W:1" -Wait -NoNewWindow
}
else
{
# on a local system just create a symlink
New-Item -ItemType SymbolicLink -Path $pathInModuleDir -Target $originalPath -Force | Out-Null
}
}
catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output $ErrorMessage
Write-Output $FailedItem
throw "The build failed when trying prepare files for MOF tests."
}
}
task MOFTest -depends Analyze, Test, MOFTestDeploy {
ForEach ($moftest in $mofTests)
{
$testResults = .\Tests\appveyor.pester.ps1 -Test -TestPath $moftest
if ($testResults.FailedCount -gt 0) {
$testResults | Format-List
Write-Error -Message 'One or more Pester unit tests failed. Build cannot continue!'
}
}
}
task BuildArtifact -depends Analyze, Test, MOFTestDeploy, MOFTest {
# Create a clean to build the artifact
New-Item -Path "$PSScriptRoot\Artifact" -ItemType Directory -Force
# Copy the correct items into the artifacts directory, filtering out the junk
Start-Process -FilePath 'robocopy.exe' -ArgumentList "`"$($PSScriptRoot)`" `"$($PSScriptRoot)\Artifact\InstallHubot`" /S /R:1 /W:1 /XD Artifact .kitchen .git /XF .gitignore build.ps1 psakeBuild.ps1 *.yml *.xml" -Wait -NoNewWindow
# Create a zip file artifact
Compress-Archive -Path $PSScriptRoot\Artifact\InstallHubot -DestinationPath $PSScriptRoot\Artifact\InstallHubot-$build_version.zip -Force
if ($env:APPVEYOR)
{
# Push the artifact into appveyor
$zip = Get-ChildItem -Path $PSScriptRoot\Artifact\*.zip | ForEach-Object { Push-AppveyorArtifact $_.FullName -FileName $_.Name }
}
}
task Deploy -depends BuildArtifact {
Try {
#Create the new version value and the dsc resource list.
$ManifestPath = "$PSScriptRoot\InstallHubot\InstallHubot.psd1"
$Manifest = Test-ModuleManifest -Path $ManifestPath
[System.Version]$version = $Manifest.Version
[System.Version]$oldversion = $Manifest.Version
Write-Output "Old Version: $version"
[String]$newVersion = New-Object -TypeName System.Version -ArgumentList ($version.Major, $version.Minor, ($version.Build+1))
If ($newVersion -ne $env:appveyor_build_version) {
$newVersion = $env:appveyor_build_version
Write-Output "New Version: $newVersion"
} Else {
Write-Output "New Version: $newVersion"
}
$env:newVersion = $newVersion
$DscResources = $Manifest.ExportedDscResources
$NoOfDscResources = $DscResources.Count
$Count = 0
ForEach ($DscResource in $DscResources) {
$Count = $Count + 1
If ($Count -eq 1 -and $Count -eq $NoOfDscResources) {
$DscResourceList = "@('$DscResource')"
} ElseIf ($Count -eq 1 -and $Count -ne $NoOfDscResources) {
$DscResourceList = "@('$DscResource'"
} ElseIf ($Count -eq $NoOfDscResources) {
$DscResourceList = $DscResourceList + ", '$DscResource')"
} Else {
$DscResourceList = $DscResourceList + ", '$DscResource'"
}
}
#Updates the module with the new version and fixes string replace bug.
Update-ModuleManifest -Path $manifestPath -ModuleVersion $newVersion -DscResourcesToExport $DscResources
(Get-Content -Path "$PSScriptRoot\DSCConfigurations\dsc_configuration.ps1") -replace $oldversion, $newversion | Set-Content -Path "$PSScriptRoot\DSCConfigurations\dsc_configuration.ps1" -Force
(Get-Content -Path $manifestPath) -replace 'PSGet_InstallHubot', 'InstallHubot' | Set-Content -Path $ManifestPath
(Get-Content -Path $manifestPath) -replace 'NewManifest', 'InstallHubot' | Set-Content -Path $ManifestPath
$Line = Get-Content $ManifestPath | Select-String "DscResourcesToExport =" | Select-Object -ExpandProperty Line
(Get-Content -Path $manifestPath) -replace $Line, "DscResourcesToExport = $DscResourceList" | Set-Content -Path $ManifestPath -Force
} Catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output $ErrorMessage
Write-Output $FailedItem
throw "Incrementing version failed. Build can not continue."
}
Try {
Write-Host "Module Path : $ENV:BHProjectPath"
Write-Host "Build System: $env:BHBuildSystem"
Write-Host "Branch Name: $env:BHBranchName"
Write-Host "Commit Message: $env:BHCommitMessage"
$Params = @{
Path = $ProjectRoot
Force = $true
Recurse = $false # We keep psdeploy.ps1 test artifacts, avoid deploying those : )
}
Invoke-PSDeploy @Params -Verbose:$true -ErrorVariable $DeployError
} Catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Output $ErrorMessage
Write-Output $FailedItem
Write-Output $DeployError
Write-Output $_.InvocationInfo.Line
throw "Can't publish to gallery."
}
}