Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync eng/common directory with azure-sdk-tools for PR 10099 #40169

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions eng/common/TestResources/New-TestResources.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ param (
})]
[array] $AllowIpRanges = @(),

# Instead of running the post script, create a wrapped file to run it with parameters
# so that CI can run it in a subsequent step with a refreshed azure login
[Parameter()]
[string] $SelfContainedPostScript,

[Parameter()]
[switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID),

Expand Down Expand Up @@ -625,9 +630,41 @@ try {
SetResourceNetworkAccessRules -ResourceGroupName $ResourceGroupName -AllowIpRanges $AllowIpRanges -CI:$CI

$postDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path -ChildPath "$ResourceType-resources-post.ps1"

if ($SelfContainedPostScript -and !(Test-Path $postDeploymentScript)) {
throw "-SelfContainedPostScript is not supported if there is no 'test-resources-post.ps1' script in the deployment template directory"
}

if (Test-Path $postDeploymentScript) {
Log "Invoking post-deployment script '$postDeploymentScript'"
&$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
if ($SelfContainedPostScript) {
Log "Creating invokable post-deployment script '$SelfContainedPostScript' from '$postDeploymentScript'"

$deserialized = @{}
foreach ($parameter in $PSBoundParameters.GetEnumerator()) {
if ($parameter.Value -is [System.Management.Automation.SwitchParameter]) {
$deserialized[$parameter.Key] = $parameter.Value.ToBool()
} else {
$deserialized[$parameter.Key] = $parameter.Value
}
}
$deserialized['ResourceGroupName'] = $ResourceGroupName
$deserialized['DeploymentOutputs'] = $deploymentOutputs
$serialized = $deserialized | ConvertTo-Json

$outScript = @"
`$parameters = `@'
$serialized
'`@ | ConvertFrom-Json -AsHashtable
# Set global variables that aren't always passed as parameters
`$ResourceGroupName = `$parameters.ResourceGroupName
`$DeploymentOutputs = `$parameters.DeploymentOutputs
$postDeploymentScript `@parameters
"@
$outScript | Out-File $SelfContainedPostScript
} else {
Log "Invoking post-deployment script '$postDeploymentScript'"
&$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
}
}

if ($templateFile.jsonFilePath.EndsWith('.compiled.json')) {
Expand Down
24 changes: 24 additions & 0 deletions eng/common/TestResources/deploy-test-resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ parameters:
ResourceType: test
UseFederatedAuth: true
PersistOidcToken: false
SelfContainedPostScript: self-contained-test-resources-post.ps1

# SubscriptionConfiguration will be splatted into the parameters of the test
# resources script. It should be JSON in the form:
Expand Down Expand Up @@ -89,6 +90,8 @@ steps:
# Write the new SubscriptionConfiguration to be used by the remove test resources
Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$($subscriptionConfiguration | ConvertTo-Json -Compress)"

$postScriptPath = $${{ parameters.PersistOidcToken }} ? '$(Agent.TempDirectory)/${{ parameters.SelfContainedPostScript }}' : $null

# The subscriptionConfiguration may have ArmTemplateParameters defined, so
# pass those in via the ArmTemplateParameters flag, and handle any
# additional parameters from the pipelines via AdditionalParameters
Expand All @@ -100,10 +103,31 @@ steps:
@subscriptionConfiguration `
-AdditionalParameters ${{ parameters.ArmTemplateParameters }} `
-AllowIpRanges ('$(azsdk-corp-net-ip-ranges)' -split ',') `
-SelfContainedPostScript $postScriptPath `
-CI `
-Force `
-Verbose | Out-Null

- ${{ if eq(parameters.PersistOidcToken, true) }}:
# ARM deployments that take longer than 10-15 minutes (e.g. HSM) can
# cause post scripts to fail with expired credentials.
# Add a new task with a refreshed token as a workaround to this issue.
- task: AzureCLI@2
displayName: Test Resources Post with refreshed login
env:
${{ insert }}: ${{ parameters.EnvVars }}
inputs:
azureSubscription: ${{ parameters.ServiceConnection }}
addSpnToEnvironment: true
scriptLocation: inlineScript
scriptType: pscore
inlineScript: |
$env:ARM_OIDC_TOKEN = $env:idToken
$scriptPath = '$(Agent.TempDirectory)/${{ parameters.SelfContainedPostScript }}'
Write-Host "Executing self contained test resources post script '$scriptPath'"
& $scriptPath
Remove-Item $scriptPath # avoid any possible complications when we run multiple deploy templates

- ${{ else }}:
- pwsh: |
eng/common/scripts/Import-AzModules.ps1
Expand Down
Loading