Skip to content

Commit

Permalink
SqlServerDsc: Fix pipeline script (dsccommunity#1875)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Mar 25, 2023
1 parent 67aa742 commit fe00b68
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 39 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed the regular expression `features?` from the GitVersion configuration.
Before, if a fix commit mentioned the word feature but means a SQL Server
feature GitVersion would bump minor instead of patch number.
- Update pipeline script that is used to resolve dependencies.
- When running in Azure Pipelines any existing SqlServer module is removed
before running integration tests, so the tests can update to latest version.
- `Get-SqlDscAudit`
Expand Down
124 changes: 85 additions & 39 deletions Resolve-Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,32 @@ catch

Write-Progress -Activity 'Bootstrap:' -PercentComplete 0 -CurrentOperation 'NuGet Bootstrap'

# TODO: This should handle the parameter $AllowOldPowerShellGetModule.
$powerShellGetModule = Import-Module -Name 'PowerShellGet' -MinimumVersion '2.0' -ErrorAction 'SilentlyContinue' -PassThru
$importModuleParameters = @{
Name = 'PowerShellGet'
MinimumVersion = '2.0'
ErrorAction = 'SilentlyContinue'
PassThru = $true
}

if ($AllowOldPowerShellGetModule)
{
$importModuleParameters.Remove('MinimumVersion')
}

$powerShellGetModule = Import-Module @importModuleParameters

# Install the package provider if it is not available.
$nuGetProvider = Get-PackageProvider -Name 'NuGet' -ListAvailable | Select-Object -First 1
$nuGetProvider = Get-PackageProvider -Name 'NuGet' -ListAvailable -ErrorAction 'SilentlyContinue' |
Select-Object -First 1

if (-not $powerShellGetModule -and -not $nuGetProvider)
{
$providerBootstrapParameters = @{
Name = 'nuget'
Name = 'NuGet'
Force = $true
ForceBootstrap = $true
ErrorAction = 'Stop'
Scope = 'CurrentUser'
}

switch ($PSBoundParameters.Keys)
Expand All @@ -196,7 +209,7 @@ if (-not $powerShellGetModule -and -not $nuGetProvider)

'Scope'
{
$providerBootstrapParameters.Add('Scope', $Scope)
$providerBootstrapParameters.Scope = $Scope
}
}

Expand All @@ -205,15 +218,15 @@ if (-not $powerShellGetModule -and -not $nuGetProvider)
$providerBootstrapParameters.Add('AllowPrerelease', $true)
}

Write-Information -MessageData 'Bootstrap: Installing NuGet Package Provider from the web (Make sure Microsoft addresses/ranges are allowed).'
Write-Information -MessageData 'Bootstrap: Installing NuGet Package Provider from the web (Make sure Microsoft addresses/ranges are allowed).' -InformationAction 'Continue'

$null = Install-PackageProvider @providerBootstrapParams
$null = Install-PackageProvider @providerBootstrapParameters

$nuGetProvider = Get-PackageProvider -Name 'NuGet' -ListAvailable | Select-Object -First 1

$nuGetProviderVersion = $nuGetProvider.Version.ToString()

Write-Information -MessageData "Bootstrap: Importing NuGet Package Provider version $nuGetProviderVersion to current session."
Write-Information -MessageData "Bootstrap: Importing NuGet Package Provider version $nuGetProviderVersion to current session." -InformationAction 'Continue'

$Null = Import-PackageProvider -Name 'NuGet' -RequiredVersion $nuGetProviderVersion -Force
}
Expand All @@ -237,47 +250,79 @@ try
# Versions below 2.0 are considered old, unreliable & not recommended
if (-not $powerShellGetVersion -or ($powerShellGetVersion -lt [System.Version] '2.0' -and -not $AllowOldPowerShellGetModule))
{
Write-Progress -Activity 'Bootstrap:' -PercentComplete 40 -CurrentOperation 'Installing newer version of PowerShellGet'

$installPowerShellGetParameters = @{
Name = 'PowerShellGet'
Force = $True
SkipPublisherCheck = $true
AllowClobber = $true
Scope = $Scope
Repository = $Gallery
}
Write-Progress -Activity 'Bootstrap:' -PercentComplete 40 -CurrentOperation 'Fetching newer version of PowerShellGet'

switch ($PSBoundParameters.Keys)
# PowerShellGet module not found, installing or saving it.
if ($PSDependTarget -in 'CurrentUser', 'AllUsers')
{
'Proxy'
{
$installPowerShellGetParameters.Add('Proxy', $Proxy)
Write-Debug -Message "PowerShellGet module not found. Attempting to install from Gallery $Gallery."

Write-Warning -Message "Installing PowerShellGet in $PSDependTarget Scope."

$installPowerShellGetParameters = @{
Name = 'PowerShellGet'
Force = $True
SkipPublisherCheck = $true
AllowClobber = $true
Scope = $Scope
Repository = $Gallery
}

'ProxyCredential'
switch ($PSBoundParameters.Keys)
{
$installPowerShellGetParameters.Add('ProxyCredential', $ProxyCredential)
'Proxy'
{
$installPowerShellGetParameters.Add('Proxy', $Proxy)
}

'ProxyCredential'
{
$installPowerShellGetParameters.Add('ProxyCredential', $ProxyCredential)
}

'GalleryCredential'
{
$installPowerShellGetParameters.Add('Credential', $GalleryCredential)
}
}

'GalleryCredential'
{
$installPowerShellGetParameters.Add('Credential', $GalleryCredential)
Write-Progress -Activity 'Bootstrap:' -PercentComplete 60 -CurrentOperation 'Installing newer version of PowerShellGet'

Install-Module @installPowerShellGetParameters
}
else
{
Write-Debug -Message "PowerShellGet module not found. Attempting to Save from Gallery $Gallery to $PSDependTarget"

$saveModuleParameters = @{
Name = 'PowerShellGet'
MaximumVersion = '2.99.99'
Repository = $Gallery
Path = $PSDependTarget
Force = $true
}

Write-Progress -Activity 'Bootstrap:' -PercentComplete 60 -CurrentOperation "Saving PowerShellGet from $Gallery to $Scope"

Save-Module @saveModuleParameters
}

Write-Progress -Activity 'Bootstrap:' -PercentComplete 60 -CurrentOperation 'Installing newer version of PowerShellGet'
Write-Debug -Message 'Removing previous versions of PowerShellGet and PackageManagement from session'

Get-Module -Name 'PowerShellGet' -All | Remove-Module -Force -ErrorAction 'SilentlyContinue'
Get-Module -Name 'PackageManagement' -All | Remove-Module -Force

Install-Module @installPowerShellGetParameters
Write-Progress -Activity 'Bootstrap:' -PercentComplete 65 -CurrentOperation 'Loading latest version of PowerShellGet'

Remove-Module -Name 'PowerShellGet' -Force -ErrorAction 'SilentlyContinue'
Remove-Module -Name 'PackageManagement' -Force
Write-Debug -Message 'Importing latest PowerShellGet and PackageManagement versions into session'

$powerShellGetModule = Import-Module PowerShellGet -Force -PassThru
Import-Module -Name 'PackageManagement' -MinimumVersion '1.4.8.1' -Force

$powerShellGetModule = Import-Module -Name 'PowerShellGet' -MinimumVersion '2.2.5' -Force -PassThru

$powerShellGetVersion = $powerShellGetModule.Version.ToString()

Write-Information -MessageData "Bootstrap: PowerShellGet version loaded is $powerShellGetVersion"
Write-Information -MessageData "Bootstrap: PowerShellGet version loaded is $powerShellGetVersion" -InformationAction 'Continue'
}

# Try to import the PSDepend module from the available modules.
Expand Down Expand Up @@ -343,7 +388,7 @@ try
$saveModuleParameters.add('MinimumVersion', $MinimumPSDependVersion)
}

Write-Progress -Activity 'Bootstrap:' -PercentComplete 75 -CurrentOperation "Saving & Importing PSDepend from $Gallery to $Scope"
Write-Progress -Activity 'Bootstrap:' -PercentComplete 75 -CurrentOperation "Saving PSDepend from $Gallery to $Scope"

Save-Module @saveModuleParameters
}
Expand Down Expand Up @@ -386,7 +431,7 @@ try
}
else
{
Write-Verbose "PowerShell-Yaml is already available"
Write-Verbose 'PowerShell-Yaml is already available'
}

Write-Progress -Activity 'Bootstrap:' -PercentComplete 88 -CurrentOperation 'Importing PowerShell module PowerShell-Yaml'
Expand All @@ -396,7 +441,7 @@ try

Write-Progress -Activity 'Bootstrap:' -PercentComplete 90 -CurrentOperation 'Invoke PSDepend'

Write-Progress -Activity "PSDepend:" -PercentComplete 0 -CurrentOperation "Restoring Build Dependencies"
Write-Progress -Activity 'PSDepend:' -PercentComplete 0 -CurrentOperation 'Restoring Build Dependencies'

if (Test-Path -Path $DependencyFile)
{
Expand All @@ -409,13 +454,14 @@ try
Invoke-PSDepend @psDependParameters
}

Write-Progress -Activity "PSDepend:" -PercentComplete 100 -CurrentOperation "Dependencies restored" -Completed
Write-Progress -Activity 'PSDepend:' -PercentComplete 100 -CurrentOperation 'Dependencies restored' -Completed

Write-Progress -Activity 'Bootstrap:' -PercentComplete 100 -CurrentOperation "Bootstrap complete" -Completed
Write-Progress -Activity 'Bootstrap:' -PercentComplete 100 -CurrentOperation 'Bootstrap complete' -Completed
}
finally
{
# Reverting the Installation Policy for the given gallery
Set-PSRepository -Name $Gallery -InstallationPolicy $previousGalleryInstallationPolicy
Write-Verbose -Message "Project Bootstrapped, returning to Invoke-Build"

Write-Verbose -Message 'Project Bootstrapped, returning to Invoke-Build'
}

0 comments on commit fe00b68

Please sign in to comment.