functions/Invoke-DBOPackageCI.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
function Invoke-DBOPackageCI {
    <#
    .SYNOPSIS
        Prepares the scripts from the source folder to be deployed using DBOps packaging system
 
    .DESCRIPTION
        For a newly defined process, creates a new DBOps package from all the files in the specified folder
        For existing process, updates the current version of the package by creating
        a new build from the scripts that were recently added/modified in the specified folder.
 
        Compares all the contents of the package with contents of the folder using absolute/relative paths
        depending on the ability to build a relative path to the script folder.
 
        Each build will be assigned with a version using [System.Version] object. Each new build will have
        an increase in the build number, however, Major/Minor versions will stay the same unless the
        function is explicitly called with the new -Version
 
    .PARAMETER ScriptPath
        A collection of folders to scan. Accepts Get-Item/Get-ChildItem objects and wildcards.
        Will recursively add all of the subfolders inside folders. See examples if you want only custom files to be added.
        During deployment, scripts will be following this deployment order:
         - Item order provided in the ScriptPath parameter
           - Files inside each child folder (both folders and files in alphabetical order)
             - Files inside the root folder (in alphabetical order)
 
        Aliases: SourcePath
 
    .PARAMETER Path
        Path to the existing DBOpsPackage.
        Aliases: Name, FileName, Package
 
    .PARAMETER Version
        A string that is indended to represent the Major/Minor versions of the current package.
        Optional if the package already exists.
 
        Will be used to construct the new build version: specifying '2.4' will result in build '2.4.1' for new packages
 
        For existing packages will compare the versions and continue to increase the build number by 1, but only if
        Major/Minor versions are consistent.
        Example:
        Current package version = '2.1.3', Version = '2.1', Output => '2.1.4'
        Current package version = '2.1.3', Version = '3.0', Output => '3.0.1'
 
    .PARAMETER Type
        Adds only files that were not added to the package yet. The following options are available:
        * New: add new files based on their source path (can be relative)
        * Modified: adds files only if they have been modified since they had last been added to the package
        * Unique: adds unique files to the build based on their hash values. Compares hashes accross the whole package
        * All: add all files regardless of their previous involvement
 
        More than one value can be specified at the same time.
 
        Default value: New, and it's strongly recommended to keep it that way!
 
    .PARAMETER Absolute
        All the files in -Path will be added using their absolute paths instead of relative.
 
    .PARAMETER Relative
        Use current location to build relative paths instead of starting from the folder in -Path.
 
    .PARAMETER NoRecurse
        Only process the first level of the target -Path.
 
    .PARAMETER Match
        Runs a regex verification against provided file names using the provided Match string.
        Example: .*\.sql
 
    .PARAMETER Confirm
        Prompts to confirm certain actions
 
    .PARAMETER WhatIf
        Shows what would happen if the command would execute, but does not actually perform the command
 
    .EXAMPLE
        # Will invoke CI build for version 1.0.x of the package MyPackage.zip using scripts from the source folder .\Scripts
        Invoke-DBOPackageCI -Path MyPackage.zip -ScriptPath .\Scripts -Version 1.0
 
    .NOTES
        See 'Get-Help New-DBOPackage' for additional info about packages.
#>

    [CmdletBinding(SupportsShouldProcess)]
    param
    (
        [Parameter(Mandatory = $true,
            Position = 1)]
        [Alias('FileName', 'Name', 'Package')]
        [string]$Path,
        [Parameter(Mandatory = $true,
            ValueFromPipeline = $true,
            Position = 2)]
        [Alias('SourcePath')]
        [object[]]$ScriptPath,
        [Version]$Version,
        [ValidateSet('New', 'Modified', 'Unique', 'All')]
        [string[]]$Type = 'New',
        [switch]$Absolute,
        [switch]$Relative,
        [switch]$NoRecurse,
        [string[]]$Match
    )

    begin {

    }
    process {
        try {
            $pkg = Get-DBOPackage $Path -ErrorAction Stop
            $pkgVersion = [Version]$pkg.Version
        }
        catch [System.Management.Automation.ItemNotFoundException] {
            $pkgVersion = [Version]'1.0'
            $pkg = $null
            Write-PSFMessage -Message "Assuming a new version $pkgVersion for the build" -Level Verbose
        }
        catch {
            Stop-PSFFunction -ErrorRecord $_ -EnableException $true -Message $_.Exception.Message
            return
        }

        if ($Version) {
            if ($Version.CompareTo([version]::new($pkgVersion.Major, $pkgVersion.Minor)) -ne 0) {
                Write-PSFMessage -Message "Assuming a new version $Version for the build instead of $pkgVersion" -Level Verbose
                $pkgVersion = $Version
            }
        }
        # Ensure version has three parts
        if ($pkgVersion.Build -eq -1) {
            $pkgVersion = [version]::new($pkgVersion.Major, $pkgVersion.Minor, 0)
        }
        #Increasing build version
        $pkgVersion = [Version]::new($pkgVersion.Major, $pkgVersion.Minor, $pkgVersion.Build + 1)
        Write-PSFMessage -Message "Building version $pkgVersion" -Level Verbose

        $ciSplat = @{
            Absolute   = $Absolute
            Relative   = $Relative
            NoRecurse  = $NoRecurse
            Match      = $Match
            ScriptPath = $ScriptPath
            Build      = $pkgVersion.ToString(3)
        }
        if ($pkg) {
            if ($PSCmdlet.ShouldProcess($pkg, "Adding new build to existing package")) {
                Add-DBOBuild @ciSplat -Package $pkg -Type $Type
            }
        }
        else {
            if ($PSCmdlet.ShouldProcess($Path, "Creating new package")) {
                New-DBOPackage @ciSplat -Name $Path
            }
        }
    }
    end {

    }
}