Skip to content

Commit ba5ce16

Browse files
authored
Sync eng/common directory with azure-sdk-tools repository for Tools PR Azure/azure-sdk-tools#850 (#14278)
1 parent e93e70e commit ba5ce16

File tree

4 files changed

+115
-110
lines changed

4 files changed

+115
-110
lines changed

eng/common/TestResources/New-TestResources.ps1

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ param (
1515
[ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')]
1616
[string] $BaseName,
1717

18+
[ValidatePattern('^[-\w\._\(\)]+$')]
19+
[string] $ResourceGroupName,
20+
1821
[Parameter(Mandatory = $true)]
1922
[string] $ServiceDirectory,
2023

@@ -115,6 +118,7 @@ $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
115118
$root = [System.IO.Path]::Combine($repositoryRoot, "sdk", $ServiceDirectory) | Resolve-Path
116119
$templateFileName = 'test-resources.json'
117120
$templateFiles = @()
121+
$environmentVariables = @{}
118122

119123
Write-Verbose "Checking for '$templateFileName' files under '$root'"
120124
Get-ChildItem -Path $root -Filter $templateFileName -Recurse | ForEach-Object {
@@ -194,14 +198,18 @@ $serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) {
194198
$ServiceDirectory
195199
}
196200

197-
# Format the resource group name based on resource group naming recommendations and limitations.
198-
$resourceGroupName = if ($CI) {
199-
$BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16)
200-
Write-Verbose "Generated base name '$BaseName' for CI build"
201+
if ($CI) {
202+
$BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16)
203+
Write-Verbose "Generated base name '$BaseName' for CI build"
204+
}
201205

202-
"rg-{0}-$BaseName" -f ($serviceName -replace '[\\\/:]', '-').Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-')
206+
$ResourceGroupName = if ($ResourceGroupName) {
207+
$ResourceGroupName
208+
} elseif ($CI) {
209+
# Format the resource group name based on resource group naming recommendations and limitations.
210+
"rg-{0}-$BaseName" -f ($serviceName -replace '[\\\/:]', '-').Substring(0, [Math]::Min($serviceName.Length, 90 - $BaseName.Length - 4)).Trim('-')
203211
} else {
204-
"rg-$BaseName"
212+
"rg-$BaseName"
205213
}
206214

207215
# Tag the resource group to be deleted after a certain number of hours if specified.
@@ -225,13 +233,14 @@ if ($CI) {
225233
}
226234

227235
# Set the resource group name variable.
228-
Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $resourceGroupName"
229-
Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$resourceGroupName"
236+
Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName"
237+
Write-Host "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName"
238+
$environmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName
230239
}
231240

232-
Log "Creating resource group '$resourceGroupName' in location '$Location'"
241+
Log "Creating resource group '$ResourceGroupName' in location '$Location'"
233242
$resourceGroup = Retry {
234-
New-AzResourceGroup -Name "$resourceGroupName" -Location $Location -Tag $tags -Force:$Force
243+
New-AzResourceGroup -Name "$ResourceGroupName" -Location $Location -Tag $tags -Force:$Force
235244
}
236245

237246
if ($resourceGroup.ProvisioningState -eq 'Succeeded') {
@@ -295,7 +304,7 @@ foreach ($templateFile in $templateFiles) {
295304
$preDeploymentScript = $templateFile | Split-Path | Join-Path -ChildPath 'test-resources-pre.ps1'
296305
if (Test-Path $preDeploymentScript) {
297306
Log "Invoking pre-deployment script '$preDeploymentScript'"
298-
&$preDeploymentScript -ResourceGroupName $resourceGroupName @PSBoundParameters
307+
&$preDeploymentScript -ResourceGroupName $ResourceGroupName @PSBoundParameters
299308
}
300309

301310
Log "Deploying template '$templateFile' to resource group '$($resourceGroup.ResourceGroupName)'"
@@ -364,6 +373,7 @@ foreach ($templateFile in $templateFiles) {
364373
foreach ($key in $deploymentOutputs.Keys)
365374
{
366375
$value = $deploymentOutputs[$key]
376+
$environmentVariables[$key] = $value
367377

368378
if ($CI) {
369379
# Treat all ARM template output variables as secrets since "SecureString" variables do not set values.
@@ -386,12 +396,14 @@ foreach ($templateFile in $templateFiles) {
386396
$postDeploymentScript = $templateFile | Split-Path | Join-Path -ChildPath 'test-resources-post.ps1'
387397
if (Test-Path $postDeploymentScript) {
388398
Log "Invoking post-deployment script '$postDeploymentScript'"
389-
&$postDeploymentScript -ResourceGroupName $resourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
399+
&$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
390400
}
391401
}
392402

393403
$exitActions.Invoke()
394404

405+
return $environmentVariables
406+
395407
<#
396408
.SYNOPSIS
397409
Deploys live test resources defined for a service directory to Azure.
@@ -422,6 +434,10 @@ the ARM template. See also https://docs.microsoft.com/azure/architecture/best-pr
422434
Note: The value specified for this parameter will be overriden and generated
423435
by New-TestResources.ps1 if $CI is specified.
424436
437+
.PARAMETER ResourceGroupName
438+
Set this value to deploy directly to a Resource Group that has already been
439+
created.
440+
425441
.PARAMETER ServiceDirectory
426442
A directory under 'sdk' in the repository root - optionally with subdirectories
427443
specified - in which to discover ARM templates named 'test-resources.json'.

0 commit comments

Comments
 (0)