Skip to content

Commit 3574cf4

Browse files
Merge pull request #4 from tsteenbakkers/DEV
PS CI / CD and Pester added. Reworked folder structure
2 parents 2830e54 + 8c3ebc5 commit 3574cf4

20 files changed

+596
-17
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
/output/**
3+
4+
localbuild\.ps1
File renamed without changes.
File renamed without changes.

Example/CICD-Kicker.ps1

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Param(
2+
$Function,
3+
$targetserver,
4+
$targetdatabase,
5+
$targetuser,
6+
$targetpw
7+
)
8+
9+
if(-not(Get-Module MSSQLCICDHelper)){
10+
try{
11+
$isGitInstalled = $null -ne ( (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*) + (Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*) | Where-Object { $null -ne $_.DisplayName -and $_.Displayname.Contains('Git') })
12+
if($isGitInstalled){
13+
git clone https://github.com/tsteenbakkers/MSSQL-CICD-Helper.git
14+
}
15+
else{
16+
Write-Error "Git is not installed. Make sure it is installed and try again!"
17+
}
18+
19+
$curdir = Get-Location
20+
$modulefile = "{0}\MSSQL-CICD-Helper\MSSQLCICDHelper.psd1" -f $curdir
21+
22+
Import-Module -name $modulefile -Verbose
23+
}
24+
catch{
25+
write-error "something wnet wrong cloning or importing the MSSQL-CICD-helper module. please check and retry"
26+
exit 1;
27+
}
28+
29+
30+
}
31+
32+
switch ($function) {
33+
"build"{
34+
Invoke-MSSQLCICDHelperMSBuild -Verbose -keeplogfiles
35+
}
36+
"Deploy"{
37+
Invoke-MSSQLCICDHelperSQLPackage -Verbose -keeplogfiles -tsn $targetserver -tdn $targetdatabase -tu $targetuser -tp $targetpw
38+
}
39+
Default {
40+
Write-Error "invalid function chosen."
41+
break;
42+
}
43+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
stages:
2+
- build
3+
- deploy
4+
5+
msbuild:
6+
stage: build
7+
artifacts:
8+
untracked: true
9+
script:
10+
- powershell -File .\CICD-Kicker.ps1 -function Build
11+
tags: [yourrunnertaghere]
12+
SSDTDeploy:
13+
stage: deploy
14+
artifacts:
15+
untracked: true
16+
script:
17+
- powershell -File .\CICD-Kicker.ps1 -function Deploy -TargetServer %yourciservervar% -TargetDatabase %yourcidbvar% -TargetUser %yourciuservar% -TargetPW %yourcipwvar%
18+
tags: [yourrunnertaghere]

MSSQL-CICD-Helper.build.ps1

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
#requires -Modules InvokeBuild, PSDeploy, BuildHelpers, PSScriptAnalyzer, PlatyPS, Pester
2+
$script:ModuleName = 'MSSQL-CICD-Helper'
3+
4+
$script:Source = Join-Path $BuildRoot $ModuleName
5+
$script:Output = Join-Path $BuildRoot output
6+
$script:Destination = Join-Path $Output $ModuleName
7+
$script:ModulePath = "$Destination\$ModuleName.psm1"
8+
$script:ManifestPath = "$Destination\$ModuleName.psd1"
9+
$script:Imports = ( 'Private', 'Public' )
10+
$script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion`_$TimeStamp.xml"
11+
$script:HelpRoot = Join-Path $Output 'help'
12+
13+
function TaskX($Name, $Parameters) {task $Name @Parameters -Source $MyInvocation}
14+
15+
Task Default Clean, Build, Pester, UpdateSource
16+
Task Build CopyToOutput, BuildPSM1, BuildPSD1
17+
Task Pester Build, ImportModule, UnitTests, FullTests
18+
Task Local Build, Pester, UpdateSource
19+
20+
Task Clean {
21+
22+
If (Test-Path $Output)
23+
{
24+
$null = Remove-Item $Output -Recurse -ErrorAction Ignore
25+
}
26+
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore
27+
}
28+
29+
Task UnitTests {
30+
$TestResults = Invoke-Pester -Path Tests\*unit* -PassThru -Tag Build -ExcludeTag Slow
31+
if ($TestResults.FailedCount -gt 0)
32+
{
33+
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
34+
}
35+
}
36+
37+
Task FullTests {
38+
$TestResults = Invoke-Pester -Path Tests -PassThru -OutputFormat NUnitXml -OutputFile $testFile -Tag Build
39+
if ($TestResults.FailedCount -gt 0)
40+
{
41+
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
42+
}
43+
}
44+
45+
Task CopyToOutput {
46+
47+
" Create Directory [$Destination]"
48+
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore
49+
50+
Get-ChildItem $source -File |
51+
where name -NotMatch "$ModuleName\.ps[dm]1" |
52+
Copy-Item -Destination $Destination -Force -PassThru |
53+
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
54+
55+
Get-ChildItem $source -Directory |
56+
where name -NotIn $imports |
57+
Copy-Item -Destination $Destination -Recurse -Force -PassThru |
58+
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
59+
}
60+
61+
TaskX BuildPSM1 @{
62+
Inputs = (Get-Item "$source\*\*.ps1")
63+
Outputs = $ModulePath
64+
Jobs = {
65+
[System.Text.StringBuilder]$stringbuilder = [System.Text.StringBuilder]::new()
66+
foreach ($folder in $imports )
67+
{
68+
[void]$stringbuilder.AppendLine( "Write-Verbose 'Importing from [$Source\$folder]'" )
69+
if (Test-Path "$source\$folder")
70+
{
71+
$fileList = Get-ChildItem $source\$folder\ -Recurse -include *.ps1 | Where Name -NotLike '*.Tests.ps1'
72+
foreach ($file in $fileList)
73+
{
74+
$shortName = $file.fullname.replace($PSScriptRoot, '')
75+
" Importing [.$shortName]"
76+
[void]$stringbuilder.AppendLine( "# .$shortName" )
77+
[void]$stringbuilder.AppendLine( [System.IO.File]::ReadAllText($file.fullname) )
78+
}
79+
}
80+
}
81+
82+
" Creating module [$ModulePath]"
83+
Set-Content -Path $ModulePath -Value $stringbuilder.ToString()
84+
}
85+
}
86+
87+
TaskX BuildPSD1 @{
88+
Inputs = (Get-ChildItem $Source -Recurse -File)
89+
Outputs = $ManifestPath
90+
Jobs = {
91+
92+
Write-Output " Update [$ManifestPath]"
93+
Copy-Item "$source\$ModuleName.psd1" -Destination $ManifestPath
94+
95+
$functions = Get-ChildItem $ModuleName\Public -Recurse -include *.ps1 | Where-Object { $_.name -notmatch 'Tests'} | Select-Object -ExpandProperty basename
96+
Set-ModuleFunctions -Name $ManifestPath -FunctionsToExport $functions
97+
98+
Write-Output " Detecting semantic versioning"
99+
100+
Import-Module ".\$ModuleName"
101+
$commandList = Get-Command -Module $ModuleName
102+
Remove-Module $ModuleName
103+
104+
Write-Output " Calculating fingerprint"
105+
$fingerprint = foreach ($command in $commandList )
106+
{
107+
foreach ($parameter in $command.parameters.keys)
108+
{
109+
'{0}:{1}' -f $command.name, $command.parameters[$parameter].Name
110+
$command.parameters[$parameter].aliases | Foreach-Object { '{0}:{1}' -f $command.name, $_}
111+
}
112+
}
113+
114+
$fingerprint = $fingerprint | Sort-Object
115+
116+
if (Test-Path .\fingerprint)
117+
{
118+
$oldFingerprint = Get-Content .\fingerprint
119+
}
120+
121+
$bumpVersionType = 'Patch'
122+
' Detecting new features'
123+
$fingerprint | Where {$_ -notin $oldFingerprint } | % {$bumpVersionType = 'Minor'; " $_"}
124+
' Detecting breaking changes'
125+
$oldFingerprint | Where {$_ -notin $fingerprint } | % {$bumpVersionType = 'Major'; " $_"}
126+
127+
Set-Content -Path .\fingerprint -Value $fingerprint
128+
129+
# Bump the module version
130+
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')
131+
132+
if ( $version -lt ([version]'1.0.0') )
133+
{
134+
# Still in beta, don't bump major version
135+
if ( $bumpVersionType -eq 'Major' )
136+
{
137+
$bumpVersionType = 'Minor'
138+
}
139+
else
140+
{
141+
$bumpVersionType = 'Patch'
142+
}
143+
}
144+
145+
if(-not(Test-Path "$output\version.xml")){
146+
" PSXML not found. Determining version with Get-NextPSGalleryVersion and creating PSXML"
147+
$galleryVersion = Get-NextPSGalleryVersion -Name $ModuleName
148+
$galleryVersion | Export-Clixml -Path "$output\version.xml"
149+
}else{
150+
" PSXML found. Grabbing value from previous build."
151+
$galleryVersion = Import-Clixml -Path "$output\version.xml"
152+
}
153+
154+
if ( $version -lt $galleryVersion )
155+
{
156+
$version = $galleryVersion
157+
}
158+
159+
Write-Output " Stepping [$bumpVersionType] version [$version]"
160+
$version = [version] (Step-Version $version -Type $bumpVersionType)
161+
Write-Output " Using version: $version"
162+
163+
Update-Metadata -Path $ManifestPath -PropertyName ModuleVersion -Value $version
164+
}
165+
}
166+
167+
Task UpdateSource {
168+
Copy-Item $ManifestPath -Destination "$source\$ModuleName.psd1"
169+
}
170+
171+
Task ImportModule {
172+
if ( -Not ( Test-Path $ManifestPath ) )
173+
{
174+
" Modue [$ModuleName] is not built, cannot find [$ManifestPath]"
175+
Write-Error "Could not find module manifest [$ManifestPath]. You may need to build the module first"
176+
}
177+
else
178+
{
179+
if (Get-Module $ModuleName)
180+
{
181+
" Unloading Module [$ModuleName] from previous import"
182+
Remove-Module $ModuleName
183+
}
184+
" Importing Module [$ModuleName] from [$ManifestPath]"
185+
Import-Module $ManifestPath -Force
186+
}
187+
}
188+
189+
TaskX CreateHelp @{
190+
Partial = $true
191+
Inputs = {Get-ChildItem "$ModuleName\Public\*.ps1"}
192+
Outputs = {
193+
process
194+
{
195+
Get-ChildItem $_ | % {'{0}\{1}.md' -f $HelpRoot, $_.basename}
196+
}
197+
}
198+
Jobs = 'ImportModule', {
199+
process
200+
{
201+
$null = New-Item -Path $HelpRoot -ItemType Directory -ErrorAction SilentlyContinue
202+
$mdHelp = @{
203+
#Module = $script:ModuleName
204+
OutputFolder = $HelpRoot
205+
AlphabeticParamsOrder = $true
206+
Verbose = $true
207+
Force = $true
208+
Command = Get-Item $_ | % basename
209+
}
210+
New-MarkdownHelp @mdHelp | % fullname
211+
}
212+
}
213+
}
214+
215+
TaskX PackageHelp @{
216+
Inputs = {Get-ChildItem $HelpRoot -Recurse -File}
217+
Outputs = "$Destination\en-us\$ModuleName-help.xml"
218+
Jobs = 'CreateHelp', {
219+
New-ExternalHelp -Path $HelpRoot -OutputPath "$Destination\en-us" -force | % fullname
220+
}
221+
}
222+
223+
task Install Uninstall, {
224+
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')
225+
226+
$path = $env:PSModulePath.Split(';').Where( {
227+
$_ -like 'C:\Users\*'
228+
}, 'First', 1)
229+
230+
if ($path -and (Test-Path -Path $path))
231+
{
232+
"Using [$path] as base path..."
233+
$path = Join-Path -Path $path -ChildPath $ModuleName
234+
$path = Join-Path -Path $path -ChildPath $version
235+
236+
"Creating directory at [$path]..."
237+
New-Item -Path $path -ItemType 'Directory' -Force -ErrorAction 'Ignore'
238+
239+
"Copying items from [$Destination] to [$path]..."
240+
Copy-Item -Path "$Destination\*" -Destination $path -Recurse -Force
241+
}
242+
}
243+
244+
task Uninstall {
245+
'Unloading Modules...'
246+
Get-Module -Name $ModuleName -ErrorAction 'Ignore' | Remove-Module
247+
248+
'Uninstalling Module packages...'
249+
$modules = Get-Module $ModuleName -ErrorAction 'Ignore' -ListAvailable
250+
foreach ($module in $modules)
251+
{
252+
Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -ErrorAction 'Ignore'
253+
}
254+
255+
'Cleaning up manually installed Modules...'
256+
$path = $env:PSModulePath.Split(';').Where( {
257+
$_ -like 'C:\Users\*'
258+
}, 'First', 1)
259+
260+
$path = Join-Path -Path $path -ChildPath $ModuleName
261+
if ($path -and (Test-Path -Path $path))
262+
{
263+
'Removing files... (This may fail if any DLLs are in use.)'
264+
Get-ChildItem -Path $path -File -Recurse |
265+
Remove-Item -Force | ForEach-Object 'FullName'
266+
267+
'Removing folders... (This may fail if any DLLs are in use.)'
268+
Remove-Item $path -Recurse -Force
269+
}
270+
}

MSSQLCICDHelper.psd1 renamed to MSSQL-CICD-Helper/MSSQL-CICD-Helper.psd1

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
#
2-
# Module manifest for module 'PSGitLab'
2+
# Module manifest for module 'MSSQLCICDHelper'
33
#
4-
# Generated by: Nicholas M. Getchell
4+
# Generated by: Tobi Steenbakkers
55
#
6-
# Generated on: 7/1/2015
6+
# Generated on: 2018-04-01
77
#
88

99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = 'MSSQLCICDHelper.psm1'
12+
RootModule = 'MSSQL-CICD-Helper.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.0.2'
15+
ModuleVersion = '0.0.4'
1616

1717
# ID used to uniquely identify this module
1818
GUID = '2287837f-86ec-43ef-97a8-fee9f33a7c33'
@@ -66,15 +66,7 @@ Description = 'Set of Powershell functions to aid in CI/CD processes to build wi
6666
# NestedModules = @()
6767

6868
# Functions to export from this module
69-
FunctionsToExport = @(
70-
#'ImportConfig',
71-
'Get-MSSQLCICDHelperPaths',
72-
'Save-MSSQLCICDHelperConfiguration',
73-
'Get-MSSQLCICDHelperConfiguration',
74-
'Get-MSSQLCICDHelperFiletoBuildDeploy',
75-
'Invoke-MSSQLCICDHelperMSBuild',
76-
'Invoke-MSSQLCICDHelperSQLPackage'
77-
)
69+
FunctionsToExport = @('Get-MSSQLCICDHelperFiletoBuildDeploy','Invoke-MSSQLCICDHelperMSBuild','Invoke-MSSQLCICDHelperSQLPackage','Get-MSSQLCICDHelperConfiguration','Get-MSSQLCICDHelperPaths','Save-MSSQLCICDHelperConfiguration')
7870

7971
# Cmdlets to export from this module
8072
#CmdletsToExport = '*'
File renamed without changes.

0 commit comments

Comments
 (0)