-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.ps1
129 lines (113 loc) · 5.39 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
[cmdletbinding(DefaultParameterSetName = 'task')]
param(
[parameter(ParameterSetName = 'task', Position = 0)]
[ValidateSet('Init','Clean','Compile','Import','Test','Deploy')]
[string[]]
$Task = @('Init','Clean','Compile','Import'),
[parameter(ParameterSetName = 'help')]
[switch]$Help,
[switch]$UpdateModules
)
$env:BuildProjectName = 'PEMEncrypt'
. .\ci\BuildHelpers.ps1
$env:_BuildStart = Get-Date -Format 'o'
$env:BuildScriptPath = $PSScriptRoot
Add-EnvironmentSummary "Build started"
Set-BuildVariables
Add-Heading "Setting package feeds"
$modHash = @{
PackageManagement = '1.3.1'
PowerShellGet = '2.1.2'
}
foreach ($module in $modHash.Keys | Sort-Object) {
Write-BuildLog "Updating $module module if needed"
if ($null -eq (Get-Module $module -ListAvailable | Where-Object {[System.Version]$_.Version -ge [System.Version]($modHash[$module])})) {
Write-BuildLog "$module is below the minimum required version! Updating"
Install-Module $module -MinimumVersion $modHash[$module] -Force -AllowClobber -SkipPublisherCheck -Scope CurrentUser -Verbose:$false
}
}
Invoke-CommandWithLog {Get-PackageProvider -Name Nuget -ForceBootstrap -Verbose:$false}
Invoke-CommandWithLog {Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -Verbose:$false}
Invoke-CommandWithLog {$PSDefaultParameterValues = @{
'*-Module:Verbose' = $false
'Import-Module:ErrorAction' = 'Stop'
'Import-Module:Force' = $true
'Import-Module:Verbose' = $false
'Install-Module:AllowClobber' = $true
'Install-Module:ErrorAction' = 'Stop'
'Install-Module:Force' = $true
'Install-Module:Scope' = 'CurrentUser'
'Install-Module:Verbose' = $false
}}
$update = @{}
$verbose = @{}
if ($PSBoundParameters.ContainsKey('UpdateModules')) {
$update['UpdateModules'] = $PSBoundParameters['UpdateModules']
}
if ($PSBoundParameters.ContainsKey('Verbose')) {
$verbose['Verbose'] = $PSBoundParameters['Verbose']
}
if ($Help) {
Add-Heading "Getting help"
Write-BuildLog -c '"psake" | Resolve-Module @update -Verbose'
'psake' | Resolve-Module @update -Verbose
Get-PSakeScriptTasks -buildFile "$PSScriptRoot\psake.ps1" |
Sort-Object -Property Name |
Format-Table -Property Name, Description, Alias, DependsOn
}
else {
Add-Heading "Finalizing build prerequisites"
if (
$Task -eq 'Deploy' -and (
$ENV:BUILD_BUILDURI -notlike 'vstfs:*' -or
$env:BUILD_SOURCEBRANCH -like '*pull*' -or
$env:BUILD_SOURCEVERSIONMESSAGE -notmatch '!deploy' -or
$env:BUILD_SOURCEBRANCHNAME -ne 'master' -or
$PSVersionTable.PSVersion.Major -ne 5 -or
$null -eq $env:NugetApiKey
)
) {
"Task is 'Deploy', but conditions are not correct for deployment:`n" +
" + Current build system is VSTS : $($env:BUILD_BUILDURI -like 'vstfs:*') [$env:BUILD_BUILDURI]`n" +
" + Current branch is master : $($env:BUILD_SOURCEBRANCHNAME -eq 'master') [$env:BUILD_SOURCEBRANCHNAME]`n" +
" + Source is not a pull request : $($env:BUILD_SOURCEBRANCH -notlike '*pull*') [$env:BUILD_SOURCEBRANCH]`n" +
" + Commit message matches '!deploy' : $($env:BUILD_SOURCEVERSIONMESSAGE -match '!deploy') [$env:BUILD_SOURCEVERSIONMESSAGE]`n" +
" + Current PS major version is 5 : $($PSVersionTable.PSVersion.Major -eq 5) [$($PSVersionTable.PSVersion.ToString())]`n" +
" + NuGet API key is not null : $($null -ne $env:NugetApiKey)`n" +
"Skipping psake for this job!" | Write-Host -ForegroundColor Yellow
exit 0
}
else {
if ($Task -eq 'Deploy') {
"Task is 'Deploy' and conditions are correct for deployment:`n" +
" + Current build system is VSTS : $($env:BUILD_BUILDURI -like 'vstfs:*') [$env:BUILD_BUILDURI]`n" +
" + Current branch is master : $($env:BUILD_SOURCEBRANCHNAME -eq 'master') [$env:BUILD_SOURCEBRANCHNAME]`n" +
" + Source is not a pull request : $($env:BUILD_SOURCEBRANCH -notlike '*pull*') [$env:BUILD_SOURCEBRANCH]`n" +
" + Commit message matches '!deploy' : $($env:BUILD_SOURCEVERSIONMESSAGE -match '!deploy') [$env:BUILD_SOURCEVERSIONMESSAGE]`n" +
" + Current PS major version is 5 : $($PSVersionTable.PSVersion.Major -eq 5) [$($PSVersionTable.PSVersion.ToString())]`n" +
" + NuGet API key is not null : $($null -ne $env:NugetApiKey)`n" | Write-Host -ForegroundColor Green
}
Write-BuildLog "Resolving necessary modules"
'psake' | Resolve-Module @update -Verbose
Write-BuildLog "Modules successfully resolved..."
Add-Heading "Invoking psake with task list: [ $($Task -join ', ') ]"
$psakeParams = @{
nologo = $true
buildFile = "$PSScriptRoot\psake.ps1"
taskList = $Task
}
if ($Task -eq 'TestOnly') {
$global:ExcludeTag = @('Module')
}
else {
$global:ExcludeTag = $null
}
Invoke-psake @psakeParams @verbose
if ($Task -contains 'Import' -and $psake.build_success) {
Add-Heading "Importing $env:BuildProjectName to local scope"
Import-Module ([System.IO.Path]::Combine($env:BHBuildOutput,$env:BHProjectName)) -Verbose:$false
}
Add-EnvironmentSummary "Build finished"
exit ( [int]( -not $psake.build_success ) )
}
}