Add support for custom sort order (#121)

* Add support for custom sort order

* Move script to external file
This commit is contained in:
Simon Wåhlin 2022-09-07 09:53:41 +02:00 коммит произвёл GitHub
Родитель 3b6f983d8d
Коммит 6edcdafc58
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 72 добавлений и 2 удалений

Просмотреть файл

@ -34,6 +34,18 @@ steps:
exit 1
}
#
# CustomSorting
# If CustomSorting is enabled, sort files in diff by the .order file in each directory
#
- task: PowerShell@2
displayName: "CustomSorting"
condition: eq(variables['AZOPS_CUSTOM_SORT_ORDER'],'true')
inputs:
targetType: "filePath"
filePath: ".scripts/customSorting.ps1"
#
# Validate or Deploy
# If parameter "deploy" is set to true, then deploy the changes,
@ -49,15 +61,16 @@ steps:
targetType: "inline"
script: |
$Env:PSModulePath = $Env:PSModulePath, '$(modulesFolder)' -join [IO.Path]::PathSeparator
$CustomSortOrder = $Env:AZOPS_CUSTOM_SORT_ORDER -eq 'true'
$RunWhatIf = -not ('${{parameters.deploy}}' -eq 'true')
Import-PSFConfig -Path settings.json -Schema MetaJson -EnableException
Initialize-AzOpsEnvironment
$diff = Get-Content -Path /tmp/diff.txt
if(Test-Path -Path "/tmp/diffdeletedfiles.txt") {
$diffdeletedfiles = Get-Content -Path /tmp/diffdeletedfiles.txt
Invoke-AzOpsPush -ChangeSet $diff -DeleteSetContents $diffdeletedfiles -WhatIf:$RunWhatIf
Invoke-AzOpsPush -ChangeSet $diff -DeleteSetContents $diffdeletedfiles -WhatIf:$RunWhatIf -CustomSortOrder:$CustomSortOrder
}
else {
Invoke-AzOpsPush -ChangeSet $diff -WhatIf:$RunWhatIf
Invoke-AzOpsPush -ChangeSet $diff -WhatIf:$RunWhatIf -CustomSortOrder:$CustomSortOrder
}
Get-Job | Remove-Job -Force

Просмотреть файл

@ -11,15 +11,22 @@ variables:
# Set AZOPS_MODULE_VERSION to the desired version of the
# AzOps Module to enable version pinning. No value will cache the latest release.
#
# Set AZOPS_CUSTOM_SORT_ORDER to true to enable custom sorting.
# Custom sorting will check for a file named .order in each folder.
# Files listed in the .order file will be deployed before other files and in the
# order they are listed.
#
# - ARM_TENANT_ID
# - ARM_SUBSCRIPTION_ID
# - ARM_CLIENT_ID
# - ARM_CLIENT_SECRET
# - ARM_ENVIRONMENT
# - AZOPS_MODULE_VERSION
# - AZOPS_CUSTOM_SORT_ORDER
#
- group: credentials
- group: azops
#
# modulesFolder

Просмотреть файл

@ -0,0 +1,50 @@
param(
$DiffFilePath = '/tmp/diff.txt'
)
# IF $ENV:CI exists, we assume we are in GitHub, otherwise Azure DevOps
$StartGroup = $ENV:CI ? '::group::' : '##[group]'
$EndGroup = $ENV:CI ? '::endgroup::' : '##[endgroup]'
$diff = Get-Content -Path $DiffFilePath
Write-Host "${StartGroup}Files found in diff:"
$diff | Write-Host
$diffTable = @{}
$diff | ForEach-Object -Process {
$change = $_
$path = ($change -split "`t")[-1]
$entry = [pscustomobject]@{
fileName = Split-Path -Path $path -Leaf
directory = Split-Path -Path $path -Parent
diffString = $change
}
if ($null -eq $diffTable[$entry.directory]) {
$diffTable[$entry.directory] = @{}
}
if ($entry.fileName -ne '.order') {
$diffTable[$entry.directory][$entry.fileName] = $entry
}
}
$sortedDiff = foreach ($directoryPath in ($diffTable.Keys | Sort-Object)) {
$orderPath = Join-Path -Path $directoryPath -ChildPath '.order'
if (Test-Path -Path $orderPath) {
$order = Get-Content -Path $orderPath | ForEach-Object { $_.Trim() }
foreach ($orderName in $order) {
if ($null -ne $diffTable.$directoryPath.$orderName) {
Write-Output -InputObject $diffTable.$directoryPath.$orderName.diffString
$diffTable.$directoryPath.Remove($orderName)
}
}
}
Write-Output ($diffTable.$directoryPath.Values.diffString | Sort-Object)
}
Write-Host "$EndGroup"
Write-Host "${StartGroup}Sorted files:"
$sortedDiff | Write-Host
Write-Host "$EndGroup"
$sortedDiff | Out-File -Path $DiffFilePath