Skip to content

Commit 451c63a

Browse files
azure-sdkbenbp
andauthored
Sync eng/common directory with azure-sdk-tools for PR 3067 (Azure#23861)
* Infer live resource group name based on service directory name * Simplify service directory path splitting * Use common logic for username and basename generation * Rename GetServiceName to GetServiceLeafDirectoryName Co-authored-by: Ben Broderick Phillips <bebroder@microsoft.com>
1 parent 685d06f commit 451c63a

6 files changed

+67
-48
lines changed

eng/common/TestResources/New-TestResources.ps1

+9-24
Original file line numberDiff line numberDiff line change
@@ -368,18 +368,13 @@ try {
368368
exit
369369
}
370370

371-
$UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" }
372-
# Remove spaces, etc. that may be in $UserName
373-
$UserName = $UserName -replace '\W'
371+
$UserName = GetUserName
374372

375-
# Make sure $BaseName is set.
376373
if ($CI) {
377374
$BaseName = 't' + (New-Guid).ToString('n').Substring(0, 16)
378375
Log "Generated base name '$BaseName' for CI build"
379376
} elseif (!$BaseName) {
380-
# Handle service directories in nested directories, e.g. `data/aztables`
381-
$serviceDirectorySafeName = $ServiceDirectory -replace '[/\\]', ''
382-
$BaseName = "$UserName$serviceDirectorySafeName"
377+
$BaseName = GetBaseName $UserName $ServiceDirectory
383378
Log "BaseName was not set. Using default base name '$BaseName'"
384379
}
385380

@@ -517,13 +512,7 @@ try {
517512
$ProvisionerApplicationOid = $sp.Id
518513
}
519514

520-
# If the ServiceDirectory has multiple segments use the last directory name
521-
# e.g. D:\foo\bar -> bar or foo/bar -> bar
522-
$serviceName = if (Split-Path $ServiceDirectory) {
523-
Split-Path -Leaf $ServiceDirectory
524-
} else {
525-
$ServiceDirectory.Trim('/')
526-
}
515+
$serviceName = GetServiceLeafDirectoryName $ServiceDirectory
527516

528517
$ResourceGroupName = if ($ResourceGroupName) {
529518
$ResourceGroupName
@@ -553,16 +542,12 @@ try {
553542
BuildReason = "${env:BUILD_REASON}"
554543
}
555544

556-
# Set the resource group name variable.
557-
Write-Host "Setting variable 'AZURE_RESOURCEGROUP_NAME': $ResourceGroupName"
558-
LogVsoCommand "##vso[task.setvariable variable=AZURE_RESOURCEGROUP_NAME;]$ResourceGroupName"
559-
if ($EnvironmentVariables.ContainsKey('AZURE_RESOURCEGROUP_NAME') -and `
560-
$EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] -ne $ResourceGroupName)
561-
{
562-
Write-Warning ("Overwriting 'EnvironmentVariables.AZURE_RESOURCEGROUP_NAME' with value " +
563-
"'$($EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'])' " + "to new value '$($ResourceGroupName)'")
564-
}
565-
$EnvironmentVariables['AZURE_RESOURCEGROUP_NAME'] = $ResourceGroupName
545+
# Set an environment variable marking that resources have been deployed
546+
# This variable can be consumed as a yaml condition in later stages of the pipeline
547+
# to determine whether resources should be removed.
548+
Write-Host "Setting variable 'CI_HAS_DEPLOYED_RESOURCES': 'true'"
549+
LogVsoCommand "##vso[task.setvariable variable=CI_HAS_DEPLOYED_RESOURCES;]true"
550+
$EnvironmentVariables['CI_HAS_DEPLOYED_RESOURCES'] = $true
566551
}
567552

568553
Log "Creating resource group '$ResourceGroupName' in location '$Location'"

eng/common/TestResources/Remove-TestResources.ps1

+26-13
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ param (
1616
[ValidatePattern('^[-a-zA-Z0-9\.\(\)_]{0,80}(?<=[a-zA-Z0-9\(\)])$')]
1717
[string] $BaseName,
1818

19-
[Parameter(ParameterSetName = 'ResourceGroup', Mandatory = $true)]
20-
[Parameter(ParameterSetName = 'ResourceGroup+Provisioner', Mandatory = $true)]
19+
[Parameter(ParameterSetName = 'ResourceGroup')]
20+
[Parameter(ParameterSetName = 'ResourceGroup+Provisioner')]
2121
[string] $ResourceGroupName,
2222

2323
[Parameter(ParameterSetName = 'Default+Provisioner', Mandatory = $true)]
@@ -48,6 +48,10 @@ param (
4848
[ValidateSet('AzureCloud', 'AzureUSGovernment', 'AzureChinaCloud', 'Dogfood')]
4949
[string] $Environment = 'AzureCloud',
5050

51+
[Parameter(ParameterSetName = 'ResourceGroup')]
52+
[Parameter(ParameterSetName = 'ResourceGroup+Provisioner')]
53+
[switch] $CI,
54+
5155
[Parameter()]
5256
[switch] $Force,
5357

@@ -73,6 +77,7 @@ trap {
7377
$exitActions.Invoke()
7478
}
7579

80+
. $PSScriptRoot/SubConfig-Helpers.ps1
7681
# Source helpers to purge resources.
7782
. "$PSScriptRoot\..\scripts\Helpers\Resource-Helpers.ps1"
7883

@@ -126,18 +131,23 @@ if ($ProvisionerApplicationId) {
126131
$context = Get-AzContext
127132

128133
if (!$ResourceGroupName) {
129-
# Make sure $BaseName is set.
130-
if (!$BaseName) {
131-
$UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" }
132-
# Remove spaces, etc. that may be in $UserName
133-
$UserName = $UserName -replace '\W'
134-
135-
$BaseName = "$UserName$ServiceDirectory"
136-
Log "BaseName was not set. Using default base name '$BaseName'"
137-
}
134+
if ($CI) {
135+
$envVarName = (BuildServiceDirectoryPrefix (GetServiceLeafDirectoryName $ServiceDirectory)) + "RESOURCE_GROUP"
136+
$ResourceGroupName = [Environment]::GetEnvironmentVariable($envVarName)
137+
if (!$ResourceGroupName) {
138+
Write-Error "Could not find resource group name environment variable '$envVarName'"
139+
exit 1
140+
}
141+
} else {
142+
if (!$BaseName) {
143+
$UserName = GetUserName
144+
$BaseName = GetBaseName $UserName $ServiceDirectory
145+
Log "BaseName was not set. Using default base name '$BaseName'"
146+
}
138147

139-
# Format the resource group name like in New-TestResources.ps1.
140-
$ResourceGroupName = "rg-$BaseName"
148+
# Format the resource group name like in New-TestResources.ps1.
149+
$ResourceGroupName = "rg-$BaseName"
150+
}
141151
}
142152

143153
# If no subscription was specified, try to select the Azure SDK Developer Playground subscription.
@@ -282,6 +292,9 @@ specified - in which to discover pre removal script named 'remove-test-resources
282292
Name of the cloud environment. The default is the Azure Public Cloud
283293
('PublicCloud')
284294
295+
.PARAMETER CI
296+
Run script in CI mode. Infers various environment variable names based on CI convention.
297+
285298
.PARAMETER Force
286299
Force removal of resource group without asking for user confirmation
287300

eng/common/TestResources/Remove-TestResources.ps1.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ Remove-TestResources.ps1 -BaseName <String> -TenantId <String> [-SubscriptionId
3131
```
3232
Remove-TestResources.ps1 -ResourceGroupName <String> -TenantId <String> [-SubscriptionId <String>]
3333
-ProvisionerApplicationId <String> -ProvisionerApplicationSecret <String> [[-ServiceDirectory] <String>]
34-
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
34+
[-Environment <String>] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
3535
[<CommonParameters>]
3636
```
3737

3838
### ResourceGroup
3939
```
4040
Remove-TestResources.ps1 -ResourceGroupName <String> [-SubscriptionId <String>] [[-ServiceDirectory] <String>]
41-
[-Environment <String>] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
41+
[-Environment <String>] [-CI] [-Force] [-RemoveTestResourcesRemainingArguments <Object>] [-WhatIf] [-Confirm]
4242
[<CommonParameters>]
4343
```
4444

@@ -232,6 +232,9 @@ Accept pipeline input: False
232232
Accept wildcard characters: False
233233
```
234234
235+
### -CI
236+
Run script in CI mode. Infers various environment variable names based on CI convention.
237+
235238
### -Force
236239
Force removal of resource group without asking for user confirmation
237240

eng/common/TestResources/SubConfig-Helpers.ps1

+19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@ function BuildServiceDirectoryPrefix([string]$serviceName) {
22
return $serviceName.ToUpperInvariant() + "_"
33
}
44

5+
# If the ServiceDirectory has multiple segments use the last directory name
6+
# e.g. D:\foo\bar -> bar or foo/bar -> bar
7+
function GetServiceLeafDirectoryName([string]$serviceDirectory) {
8+
return Split-Path -Leaf $serviceDirectory
9+
}
10+
11+
function GetUserName() {
12+
$UserName = $env:USER ?? $env:USERNAME
13+
# Remove spaces, etc. that may be in $UserName
14+
$UserName = $UserName -replace '\W'
15+
return $UserName
16+
}
17+
18+
function GetBaseName([string]$user, [string]$serviceDirectoryName) {
19+
# Handle service directories in nested directories, e.g. `data/aztables`
20+
$serviceDirectorySafeName = $serviceDirectoryName -replace '[/\\]', ''
21+
return "$user$serviceDirectorySafeName"
22+
}
23+
524
function ShouldMarkValueAsSecret([string]$serviceName, [string]$key, [string]$value, [array]$allowedValues = @())
625
{
726
$logOutputNonSecret = @(

eng/common/TestResources/Update-TestResources.ps1

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ param (
3030
[int] $DeleteAfterHours = 48
3131
)
3232

33+
. $PSScriptRoot/SubConfig-Helpers.ps1
34+
3335
# By default stop for any error.
3436
if (!$PSBoundParameters.ContainsKey('ErrorAction')) {
3537
$ErrorActionPreference = 'Stop'
@@ -71,11 +73,8 @@ $exitActions = @({
7173
if (!$ResourceGroupName) {
7274
# Make sure $BaseName is set.
7375
if (!$BaseName) {
74-
$UserName = if ($env:USER) { $env:USER } else { "${env:USERNAME}" }
75-
# Remove spaces, etc. that may be in $UserName
76-
$UserName = $UserName -replace '\W'
77-
78-
$BaseName = "$UserName$ServiceDirectory"
76+
$UserName = GetUserName
77+
$BaseName = GetBaseName $UserName $ServiceDirectory
7978
Log "BaseName was not set. Using default base name '$BaseName'"
8079
}
8180

eng/common/TestResources/remove-test-resources.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Assumes steps in deploy-test-resources.yml was run previously. Requires
2-
# environment variable: AZURE_RESOURCEGROUP_NAME and Az PowerShell module
2+
# environment variable: <ServiceDirectory>_RESOURCE_GROUP and Az PowerShell module
33

44
parameters:
55
ServiceDirectory: ''
@@ -28,11 +28,11 @@ steps:
2828
"@ | ConvertFrom-Json -AsHashtable;
2929
3030
eng/common/TestResources/Remove-TestResources.ps1 `
31-
-ResourceGroupName "${env:AZURE_RESOURCEGROUP_NAME}" `
32-
-ServiceDirectory "${{ parameters.ServiceDirectory }}" `
3331
@subscriptionConfiguration `
32+
-ServiceDirectory "${{ parameters.ServiceDirectory }}" `
33+
-CI `
3434
-Force `
3535
-Verbose
3636
displayName: Remove test resources
37-
condition: ne(variables['AZURE_RESOURCEGROUP_NAME'], '')
37+
condition: eq(variables['CI_HAS_DEPLOYED_RESOURCES'], 'true')
3838
continueOnError: true

0 commit comments

Comments
 (0)