@@ -15,6 +15,9 @@ param (
15
15
[ValidatePattern (' ^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$' )]
16
16
[string ] $BaseName ,
17
17
18
+ [ValidatePattern (' ^[-\w\._\(\)]+$' )]
19
+ [string ] $ResourceGroupName ,
20
+
18
21
[Parameter (Mandatory = $true )]
19
22
[string ] $ServiceDirectory ,
20
23
@@ -115,6 +118,7 @@ $repositoryRoot = "$PSScriptRoot/../../.." | Resolve-Path
115
118
$root = [System.IO.Path ]::Combine($repositoryRoot , " sdk" , $ServiceDirectory ) | Resolve-Path
116
119
$templateFileName = ' test-resources.json'
117
120
$templateFiles = @ ()
121
+ $environmentVariables = @ {}
118
122
119
123
Write-Verbose " Checking for '$templateFileName ' files under '$root '"
120
124
Get-ChildItem - Path $root - Filter $templateFileName - Recurse | ForEach-Object {
@@ -194,14 +198,18 @@ $serviceName = if (Split-Path -IsAbsolute $ServiceDirectory) {
194
198
$ServiceDirectory
195
199
}
196
200
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
+ }
201
205
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(' -' )
203
211
} else {
204
- " rg-$BaseName "
212
+ " rg-$BaseName "
205
213
}
206
214
207
215
# Tag the resource group to be deleted after a certain number of hours if specified.
@@ -225,13 +233,14 @@ if ($CI) {
225
233
}
226
234
227
235
# 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
230
239
}
231
240
232
- Log " Creating resource group '$resourceGroupName ' in location '$Location '"
241
+ Log " Creating resource group '$ResourceGroupName ' in location '$Location '"
233
242
$resourceGroup = Retry {
234
- New-AzResourceGroup - Name " $resourceGroupName " - Location $Location - Tag $tags - Force:$Force
243
+ New-AzResourceGroup - Name " $ResourceGroupName " - Location $Location - Tag $tags - Force:$Force
235
244
}
236
245
237
246
if ($resourceGroup.ProvisioningState -eq ' Succeeded' ) {
@@ -295,7 +304,7 @@ foreach ($templateFile in $templateFiles) {
295
304
$preDeploymentScript = $templateFile | Split-Path | Join-Path - ChildPath ' test-resources-pre.ps1'
296
305
if (Test-Path $preDeploymentScript ) {
297
306
Log " Invoking pre-deployment script '$preDeploymentScript '"
298
- & $preDeploymentScript - ResourceGroupName $resourceGroupName @PSBoundParameters
307
+ & $preDeploymentScript - ResourceGroupName $ResourceGroupName @PSBoundParameters
299
308
}
300
309
301
310
Log " Deploying template '$templateFile ' to resource group '$ ( $resourceGroup.ResourceGroupName ) '"
@@ -364,6 +373,7 @@ foreach ($templateFile in $templateFiles) {
364
373
foreach ($key in $deploymentOutputs.Keys )
365
374
{
366
375
$value = $deploymentOutputs [$key ]
376
+ $environmentVariables [$key ] = $value
367
377
368
378
if ($CI ) {
369
379
# Treat all ARM template output variables as secrets since "SecureString" variables do not set values.
@@ -386,12 +396,14 @@ foreach ($templateFile in $templateFiles) {
386
396
$postDeploymentScript = $templateFile | Split-Path | Join-Path - ChildPath ' test-resources-post.ps1'
387
397
if (Test-Path $postDeploymentScript ) {
388
398
Log " Invoking post-deployment script '$postDeploymentScript '"
389
- & $postDeploymentScript - ResourceGroupName $resourceGroupName - DeploymentOutputs $deploymentOutputs @PSBoundParameters
399
+ & $postDeploymentScript - ResourceGroupName $ResourceGroupName - DeploymentOutputs $deploymentOutputs @PSBoundParameters
390
400
}
391
401
}
392
402
393
403
$exitActions.Invoke ()
394
404
405
+ return $environmentVariables
406
+
395
407
<#
396
408
. SYNOPSIS
397
409
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
422
434
Note: The value specified for this parameter will be overriden and generated
423
435
by New-TestResources.ps1 if $CI is specified.
424
436
437
+ . PARAMETER ResourceGroupName
438
+ Set this value to deploy directly to a Resource Group that has already been
439
+ created.
440
+
425
441
. PARAMETER ServiceDirectory
426
442
A directory under 'sdk' in the repository root - optionally with subdirectories
427
443
specified - in which to discover ARM templates named 'test-resources.json'.
0 commit comments