Skip to content

Commit

Permalink
Merge pull request #4 from tsteenbakkers/DEV
Browse files Browse the repository at this point in the history
PS CI / CD and Pester added. Reworked folder structure
  • Loading branch information
tsteenbakkers authored May 14, 2018
2 parents 2830e54 + 8c3ebc5 commit 3574cf4
Show file tree
Hide file tree
Showing 20 changed files with 596 additions and 17 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/output/**

localbuild\.ps1
File renamed without changes
File renamed without changes
43 changes: 43 additions & 0 deletions Example/CICD-Kicker.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Param(
$Function,
$targetserver,
$targetdatabase,
$targetuser,
$targetpw
)

if(-not(Get-Module MSSQLCICDHelper)){
try{
$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') })
if($isGitInstalled){
git clone https://github.com/tsteenbakkers/MSSQL-CICD-Helper.git
}
else{
Write-Error "Git is not installed. Make sure it is installed and try again!"
}

$curdir = Get-Location
$modulefile = "{0}\MSSQL-CICD-Helper\MSSQLCICDHelper.psd1" -f $curdir

Import-Module -name $modulefile -Verbose
}
catch{
write-error "something wnet wrong cloning or importing the MSSQL-CICD-helper module. please check and retry"
exit 1;
}


}

switch ($function) {
"build"{
Invoke-MSSQLCICDHelperMSBuild -Verbose -keeplogfiles
}
"Deploy"{
Invoke-MSSQLCICDHelperSQLPackage -Verbose -keeplogfiles -tsn $targetserver -tdn $targetdatabase -tu $targetuser -tp $targetpw
}
Default {
Write-Error "invalid function chosen."
break;
}
}
18 changes: 18 additions & 0 deletions Example/GitLabCI/Non-Docker/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
stages:
- build
- deploy

msbuild:
stage: build
artifacts:
untracked: true
script:
- powershell -File .\CICD-Kicker.ps1 -function Build
tags: [yourrunnertaghere]
SSDTDeploy:
stage: deploy
artifacts:
untracked: true
script:
- powershell -File .\CICD-Kicker.ps1 -function Deploy -TargetServer %yourciservervar% -TargetDatabase %yourcidbvar% -TargetUser %yourciuservar% -TargetPW %yourcipwvar%
tags: [yourrunnertaghere]
270 changes: 270 additions & 0 deletions MSSQL-CICD-Helper.build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
#requires -Modules InvokeBuild, PSDeploy, BuildHelpers, PSScriptAnalyzer, PlatyPS, Pester
$script:ModuleName = 'MSSQL-CICD-Helper'

$script:Source = Join-Path $BuildRoot $ModuleName
$script:Output = Join-Path $BuildRoot output
$script:Destination = Join-Path $Output $ModuleName
$script:ModulePath = "$Destination\$ModuleName.psm1"
$script:ManifestPath = "$Destination\$ModuleName.psd1"
$script:Imports = ( 'Private', 'Public' )
$script:TestFile = "$PSScriptRoot\output\TestResults_PS$PSVersion`_$TimeStamp.xml"
$script:HelpRoot = Join-Path $Output 'help'

function TaskX($Name, $Parameters) {task $Name @Parameters -Source $MyInvocation}

Task Default Clean, Build, Pester, UpdateSource
Task Build CopyToOutput, BuildPSM1, BuildPSD1
Task Pester Build, ImportModule, UnitTests, FullTests
Task Local Build, Pester, UpdateSource

Task Clean {

If (Test-Path $Output)
{
$null = Remove-Item $Output -Recurse -ErrorAction Ignore
}
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore
}

Task UnitTests {
$TestResults = Invoke-Pester -Path Tests\*unit* -PassThru -Tag Build -ExcludeTag Slow
if ($TestResults.FailedCount -gt 0)
{
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
}
}

Task FullTests {
$TestResults = Invoke-Pester -Path Tests -PassThru -OutputFormat NUnitXml -OutputFile $testFile -Tag Build
if ($TestResults.FailedCount -gt 0)
{
Write-Error "Failed [$($TestResults.FailedCount)] Pester tests"
}
}

Task CopyToOutput {

" Create Directory [$Destination]"
$null = New-Item -Type Directory -Path $Destination -ErrorAction Ignore

Get-ChildItem $source -File |
where name -NotMatch "$ModuleName\.ps[dm]1" |
Copy-Item -Destination $Destination -Force -PassThru |
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')}

Get-ChildItem $source -Directory |
where name -NotIn $imports |
Copy-Item -Destination $Destination -Recurse -Force -PassThru |
ForEach-Object { " Create [.{0}]" -f $_.fullname.replace($PSScriptRoot, '')}
}

TaskX BuildPSM1 @{
Inputs = (Get-Item "$source\*\*.ps1")
Outputs = $ModulePath
Jobs = {
[System.Text.StringBuilder]$stringbuilder = [System.Text.StringBuilder]::new()
foreach ($folder in $imports )
{
[void]$stringbuilder.AppendLine( "Write-Verbose 'Importing from [$Source\$folder]'" )
if (Test-Path "$source\$folder")
{
$fileList = Get-ChildItem $source\$folder\ -Recurse -include *.ps1 | Where Name -NotLike '*.Tests.ps1'
foreach ($file in $fileList)
{
$shortName = $file.fullname.replace($PSScriptRoot, '')
" Importing [.$shortName]"
[void]$stringbuilder.AppendLine( "# .$shortName" )
[void]$stringbuilder.AppendLine( [System.IO.File]::ReadAllText($file.fullname) )
}
}
}

" Creating module [$ModulePath]"
Set-Content -Path $ModulePath -Value $stringbuilder.ToString()
}
}

TaskX BuildPSD1 @{
Inputs = (Get-ChildItem $Source -Recurse -File)
Outputs = $ManifestPath
Jobs = {

Write-Output " Update [$ManifestPath]"
Copy-Item "$source\$ModuleName.psd1" -Destination $ManifestPath

$functions = Get-ChildItem $ModuleName\Public -Recurse -include *.ps1 | Where-Object { $_.name -notmatch 'Tests'} | Select-Object -ExpandProperty basename
Set-ModuleFunctions -Name $ManifestPath -FunctionsToExport $functions

Write-Output " Detecting semantic versioning"

Import-Module ".\$ModuleName"
$commandList = Get-Command -Module $ModuleName
Remove-Module $ModuleName

Write-Output " Calculating fingerprint"
$fingerprint = foreach ($command in $commandList )
{
foreach ($parameter in $command.parameters.keys)
{
'{0}:{1}' -f $command.name, $command.parameters[$parameter].Name
$command.parameters[$parameter].aliases | Foreach-Object { '{0}:{1}' -f $command.name, $_}
}
}

$fingerprint = $fingerprint | Sort-Object

if (Test-Path .\fingerprint)
{
$oldFingerprint = Get-Content .\fingerprint
}

$bumpVersionType = 'Patch'
' Detecting new features'
$fingerprint | Where {$_ -notin $oldFingerprint } | % {$bumpVersionType = 'Minor'; " $_"}
' Detecting breaking changes'
$oldFingerprint | Where {$_ -notin $fingerprint } | % {$bumpVersionType = 'Major'; " $_"}

Set-Content -Path .\fingerprint -Value $fingerprint

# Bump the module version
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')

if ( $version -lt ([version]'1.0.0') )
{
# Still in beta, don't bump major version
if ( $bumpVersionType -eq 'Major' )
{
$bumpVersionType = 'Minor'
}
else
{
$bumpVersionType = 'Patch'
}
}

if(-not(Test-Path "$output\version.xml")){
" PSXML not found. Determining version with Get-NextPSGalleryVersion and creating PSXML"
$galleryVersion = Get-NextPSGalleryVersion -Name $ModuleName
$galleryVersion | Export-Clixml -Path "$output\version.xml"
}else{
" PSXML found. Grabbing value from previous build."
$galleryVersion = Import-Clixml -Path "$output\version.xml"
}

if ( $version -lt $galleryVersion )
{
$version = $galleryVersion
}

Write-Output " Stepping [$bumpVersionType] version [$version]"
$version = [version] (Step-Version $version -Type $bumpVersionType)
Write-Output " Using version: $version"

Update-Metadata -Path $ManifestPath -PropertyName ModuleVersion -Value $version
}
}

Task UpdateSource {
Copy-Item $ManifestPath -Destination "$source\$ModuleName.psd1"
}

Task ImportModule {
if ( -Not ( Test-Path $ManifestPath ) )
{
" Modue [$ModuleName] is not built, cannot find [$ManifestPath]"
Write-Error "Could not find module manifest [$ManifestPath]. You may need to build the module first"
}
else
{
if (Get-Module $ModuleName)
{
" Unloading Module [$ModuleName] from previous import"
Remove-Module $ModuleName
}
" Importing Module [$ModuleName] from [$ManifestPath]"
Import-Module $ManifestPath -Force
}
}

TaskX CreateHelp @{
Partial = $true
Inputs = {Get-ChildItem "$ModuleName\Public\*.ps1"}
Outputs = {
process
{
Get-ChildItem $_ | % {'{0}\{1}.md' -f $HelpRoot, $_.basename}
}
}
Jobs = 'ImportModule', {
process
{
$null = New-Item -Path $HelpRoot -ItemType Directory -ErrorAction SilentlyContinue
$mdHelp = @{
#Module = $script:ModuleName
OutputFolder = $HelpRoot
AlphabeticParamsOrder = $true
Verbose = $true
Force = $true
Command = Get-Item $_ | % basename
}
New-MarkdownHelp @mdHelp | % fullname
}
}
}

TaskX PackageHelp @{
Inputs = {Get-ChildItem $HelpRoot -Recurse -File}
Outputs = "$Destination\en-us\$ModuleName-help.xml"
Jobs = 'CreateHelp', {
New-ExternalHelp -Path $HelpRoot -OutputPath "$Destination\en-us" -force | % fullname
}
}

task Install Uninstall, {
$version = [version] (Get-Metadata -Path $manifestPath -PropertyName 'ModuleVersion')

$path = $env:PSModulePath.Split(';').Where( {
$_ -like 'C:\Users\*'
}, 'First', 1)

if ($path -and (Test-Path -Path $path))
{
"Using [$path] as base path..."
$path = Join-Path -Path $path -ChildPath $ModuleName
$path = Join-Path -Path $path -ChildPath $version

"Creating directory at [$path]..."
New-Item -Path $path -ItemType 'Directory' -Force -ErrorAction 'Ignore'

"Copying items from [$Destination] to [$path]..."
Copy-Item -Path "$Destination\*" -Destination $path -Recurse -Force
}
}

task Uninstall {
'Unloading Modules...'
Get-Module -Name $ModuleName -ErrorAction 'Ignore' | Remove-Module

'Uninstalling Module packages...'
$modules = Get-Module $ModuleName -ErrorAction 'Ignore' -ListAvailable
foreach ($module in $modules)
{
Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -ErrorAction 'Ignore'
}

'Cleaning up manually installed Modules...'
$path = $env:PSModulePath.Split(';').Where( {
$_ -like 'C:\Users\*'
}, 'First', 1)

$path = Join-Path -Path $path -ChildPath $ModuleName
if ($path -and (Test-Path -Path $path))
{
'Removing files... (This may fail if any DLLs are in use.)'
Get-ChildItem -Path $path -File -Recurse |
Remove-Item -Force | ForEach-Object 'FullName'

'Removing folders... (This may fail if any DLLs are in use.)'
Remove-Item $path -Recurse -Force
}
}
20 changes: 6 additions & 14 deletions MSSQLCICDHelper.psd1 → MSSQL-CICD-Helper/MSSQL-CICD-Helper.psd1
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#
# Module manifest for module 'PSGitLab'
# Module manifest for module 'MSSQLCICDHelper'
#
# Generated by: Nicholas M. Getchell
# Generated by: Tobi Steenbakkers
#
# Generated on: 7/1/2015
# Generated on: 2018-04-01
#

@{

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

# Version number of this module.
ModuleVersion = '0.0.2'
ModuleVersion = '0.0.4'

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

# Functions to export from this module
FunctionsToExport = @(
#'ImportConfig',
'Get-MSSQLCICDHelperPaths',
'Save-MSSQLCICDHelperConfiguration',
'Get-MSSQLCICDHelperConfiguration',
'Get-MSSQLCICDHelperFiletoBuildDeploy',
'Invoke-MSSQLCICDHelperMSBuild',
'Invoke-MSSQLCICDHelperSQLPackage'
)
FunctionsToExport = @('Get-MSSQLCICDHelperFiletoBuildDeploy','Invoke-MSSQLCICDHelperMSBuild','Invoke-MSSQLCICDHelperSQLPackage','Get-MSSQLCICDHelperConfiguration','Get-MSSQLCICDHelperPaths','Save-MSSQLCICDHelperConfiguration')

# Cmdlets to export from this module
#CmdletsToExport = '*'
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 3574cf4

Please sign in to comment.