From 55bfe386c0966f141b5f4f7f8597203a06430a06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=A5hlin?= Date: Fri, 10 Jun 2022 13:05:18 +0200 Subject: [PATCH 1/2] Add support for custom sort order --- .pipelines/.templates/validate-deploy.yml | 57 ++++++++++++++++++++++- .pipelines/.templates/vars.yml | 7 +++ 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/.pipelines/.templates/validate-deploy.yml b/.pipelines/.templates/validate-deploy.yml index 24a25e6a..c5e4ad02 100644 --- a/.pipelines/.templates/validate-deploy.yml +++ b/.pipelines/.templates/validate-deploy.yml @@ -27,6 +27,58 @@ steps: exit 1 fi + # + # 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: "inline" + script: | + $diff = Get-Content -Path /tmp/diff.txt + + Write-Host '##[section]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 '##[section]Sorted files:' + $sortedDiff | Write-Host + + $sortedDiff | Out-File -Path '/tmp/diff.txt' + # # Validate or Deploy # If parameter "deploy" is set to true, then deploy the changes, @@ -42,15 +94,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 87449616..08d40872 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 From cf7110e45c9d24fa7570a0d45102daaac9c41f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20W=C3=A5hlin?= Date: Mon, 15 Aug 2022 22:29:00 +0200 Subject: [PATCH 2/2] Move script to external file --- .pipelines/.templates/validate-deploy.yml | 44 +------------------- .scripts/customSorting.ps1 | 50 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 .scripts/customSorting.ps1 diff --git a/.pipelines/.templates/validate-deploy.yml b/.pipelines/.templates/validate-deploy.yml index e5e754b8..6258cdd2 100644 --- a/.pipelines/.templates/validate-deploy.yml +++ b/.pipelines/.templates/validate-deploy.yml @@ -43,48 +43,8 @@ steps: displayName: "CustomSorting" condition: eq(variables['AZOPS_CUSTOM_SORT_ORDER'],'true') inputs: - targetType: "inline" - script: | - $diff = Get-Content -Path /tmp/diff.txt - - Write-Host '##[section]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 '##[section]Sorted files:' - $sortedDiff | Write-Host - - $sortedDiff | Out-File -Path '/tmp/diff.txt' + targetType: "filePath" + filePath: ".scripts/customSorting.ps1" # # Validate or Deploy diff --git a/.scripts/customSorting.ps1 b/.scripts/customSorting.ps1 new file mode 100644 index 00000000..313b1f37 --- /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