Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…into users/krbar/webSiteModule
  • Loading branch information
krbar committed Jan 12, 2024
2 parents 647c5e2 + 1bc5d40 commit c860a49
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 4 deletions.
35 changes: 33 additions & 2 deletions .github/actions/templates/avm-validateModuleDeployment/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ runs:
creds: ${{ env.AZURE_CREDENTIALS }}
enable-AzPSSession: true

# [Set Deployment Location] task(s)
# ---------------------------
- name: "Get Deployment Location"
id: get-deployment-location
uses: azure/powershell@v1
with:
azPSVersion: "latest"
inlineScript: |
# Grouping task logs
Write-Output '::group::Get Recommended Regions'
# Load used functions
. (Join-Path $env:GITHUB_WORKSPACE 'avm' 'utilities' 'pipelines' 'e2eValidation' 'regionSelector' 'Get-AzDeploymentRegion.ps1')
# Set fucntion input parameters
$functionInput = @{
usePairedRegionsOnly = $true
useKnownRegionsOnly = $true
}
Write-Verbose "Invoke function with" -Verbose
Write-Verbose ($functionInput | ConvertTo-Json | Out-String) -Verbose
$location = Get-AzDeploymentRegion @functionInput
$deploymentLocation = @{}
Write-Verbose ('{0}-{1}' -f 'deploymentLocation', $location) -Verbose
Write-Output ('{0}={1}' -f 'deploymentLocation', $location) >> $env:GITHUB_OUTPUT
Write-Output '::endgroup::'
# [Token replacement] task(s)
# ---------------------------
- name: "Replace tokens in template file"
Expand Down Expand Up @@ -139,7 +170,7 @@ runs:
# Prepare general parameters
# --------------------------
# Fetching parameters
$location = '${{ inputs.location }}'
$location = '${{ steps.get-deployment-location.outputs.deploymentLocation }}'
$subscriptionId = '${{ inputs.subscriptionId }}'
$managementGroupId = '${{ inputs.managementGroupId }}'
Expand Down Expand Up @@ -189,7 +220,7 @@ runs:
# Prepare general parameters
# --------------------------
$location = '${{ inputs.location }}'
$location ='${{ steps.get-deployment-location.outputs.deploymentLocation }}'
$subscriptionId = '${{ inputs.subscriptionId }}'
$managementGroupId = '${{ inputs.managementGroupId }}'
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/avm.template.module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ jobs:
(fromJson(inputs.workflowInput)).deploymentValidation == 'true' && needs.job_module_static_validation.result != 'failure'
needs:
- job_module_static_validation
# - job_psrule_test # Ignoring dependency whilst PSRule gets bedded in, in this project
strategy:
fail-fast: false
matrix:
Expand All @@ -103,7 +102,6 @@ jobs:
uses: ./.github/actions/templates/avm-validateModuleDeployment
with:
templateFilePath: "${{ inputs.modulePath }}/${{ matrix.testCases.path }}"
location: "WestEurope"
subscriptionId: "${{ secrets.ARM_SUBSCRIPTION_ID }}"
managementGroupId: "${{ secrets.ARM_MGMTGROUP_ID }}"
removeDeployment: "${{ fromJson(inputs.workflowInput).removeDeployment }}"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<#
.SYNOPSIS
Set the deployment region for the module deployment
.DESCRIPTION
This script is used to set the deployment region for the module deployment.
.PARAMETER excludedRegions
Optional. The regions to exclude from the recommended regions list.
.PARAMETER usePairedRegionsOnly
Optional. If set, only paired regions will be returned.
.PARAMETER useKnownRegionsOnly
Optional. If set, only known regions will be returned.
.PARAMETER knownRegionList
Optional. The list of known regions to use when useKnownRegionsOnly is set. ARM Deployment are limited to 10 active deployment regions per subscription.
.EXAMPLE
Get-AzDeploymentRegion -excludedRegions @("West Europe", "Brazil South", "West US 2", "East US 2", "South Central US") -usePairedRegionsOnly -useKnownRegionsOnly
Get the recommended regions using known regions excluding the regions specified in the excludedRegions parameter and only paired regions.
.EXAMPLE
Get-AzDeploymentRegion -excludedRegions @("West Europe", "Brazil South", "West US 2", "East US 2", "South Central US") -usePairedRegionsOnly
Get the recommended regions excluding the regions specified in the excludedRegions parameter and only paired regions.
.EXAMPLE
Get-AzDeploymentRegion -excludedRegions @("West Europe", "Brazil South", "West US 2", "East US 2", "South Central US")
Get the recommended regions excluding the regions specified in the excludedRegions parameter.
.EXAMPLE
Get-AzDeploymentRegion -usePairedRegionsOnly
Get the recommended paired regions.
#>

function Get-AzDeploymentRegion {
param (
[Parameter(Mandatory = $false)]
[array] $excludedRegions = @(
"westeurope",
"westus2",
"eastus2",
"centralus",
"southcentralus",
"brazilsouth",
"koreacentral",
"qatarcentral"
),

[Parameter(Mandatory = $false)]
[cmdletbinding()]
[switch] $usePairedRegionsOnly,

[Parameter(Mandatory = $false)]
[cmdletbinding()]
[switch] $useKnownRegionsOnly,

[Parameter(Mandatory = $false)]
[array] $knownRegionList = @(
"northeurope",
"eastus",
"polandcentral",
"germanywestcentral",
"francecentral",
"westus3",
"australiaeast",
"uksouth",
"westeurope"
)
)

$allRegions = Get-AzLocation -ExtendedLocation $true

# Filter regions where Location is in the known regions list
if ($useKnownRegionsOnly) {
$regionList = $allRegions | Where-Object { $_.Location -in $knownRegionList }
}
else {
$regionList = $allRegions
}


$exclusions = @()
$exclusions = $excludedRegions | Where-Object { $_ -in ($_ -like '* *' ? $regionList.DisplayName : $regionList.Location) }
Write-Verbose ('Excluding regions: {0}' -f ($exclusions | ConvertTo-Json)) -Verbose


# Filter regions where RegionCategory is 'Recommended' and not in the excluded list
$recommendedRegions = $regionList | Where-Object { $_.RegionCategory -eq "Recommended" -and $_.Location -notin $exclusions }
Write-Verbose "Recommended regions: $($recommendedRegions.Location | ConvertTo-Json)"

if ($usePairedRegionsOnly) {
# Filter regions where PairedRegionName is not null
$recommendedRegions = $recommendedRegions | Where-Object { $null -ne $_.PairedRegion }
Write-Verbose "Paired regions only: $($recommendedRegions.Location | ConvertTo-Json)"
}

# Display indexed regions
Write-Verbose "Recommended regions: $($recommendedRegions.Location | ConvertTo-Json)" -Verbose

$index = Get-Random -Maximum ($recommendedRegions.Count)
Write-Verbose "Generated random index [$index]"

$location = $recommendedRegions[$index].Location
Write-Verbose "Selected location [$location]" -Verbose

return $location
}

0 comments on commit c860a49

Please sign in to comment.