diff --git a/.pipelines/.templates/validate-deploy.yml b/.pipelines/.templates/validate-deploy.yml index c38e859..6258cdd 100644 --- a/.pipelines/.templates/validate-deploy.yml +++ b/.pipelines/.templates/validate-deploy.yml @@ -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 diff --git a/.pipelines/.templates/vars.yml b/.pipelines/.templates/vars.yml index 8744961..08d4087 100644 --- a/.pipelines/.templates/vars.yml +++ b/.pipelines/.templates/vars.yml @@ -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 diff --git a/.scripts/customSorting.ps1 b/.scripts/customSorting.ps1 new file mode 100644 index 0000000..313b1f3 --- /dev/null +++ b/.scripts/customSorting.ps1 @@ -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 \ No newline at end of file