Skip to content

Commit 572b3ce

Browse files
benbpazure-sdk
authored andcommitted
Add option to create self contained test resources post script
1 parent cb9ed3f commit 572b3ce

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

eng/common/TestResources/New-TestResources.ps1

+35-2
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ param (
9393
})]
9494
[array] $AllowIpRanges = @(),
9595

96+
# Instead of running the post script, create a wrapped file to run it with parameters
97+
# so that CI can run it in a subsequent step with a refreshed azure login
98+
[Parameter()]
99+
[string] $SelfContainedPostScript,
100+
96101
[Parameter()]
97102
[switch] $CI = ($null -ne $env:SYSTEM_TEAMPROJECTID),
98103

@@ -626,8 +631,36 @@ try {
626631

627632
$postDeploymentScript = $templateFile.originalFilePath | Split-Path | Join-Path -ChildPath "$ResourceType-resources-post.ps1"
628633
if (Test-Path $postDeploymentScript) {
629-
Log "Invoking post-deployment script '$postDeploymentScript'"
630-
&$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
634+
if ($SelfContainedPostScript) {
635+
Log "Creating invokable post-deployment script '$SelfContainedPostScript' from '$postDeploymentScript'"
636+
637+
$deserialized = @{}
638+
foreach ($parameter in $PSBoundParameters.GetEnumerator()) {
639+
if ($parameter.Value -is [System.Management.Automation.SwitchParameter]) {
640+
$deserialized[$parameter.Key] = $parameter.Value.ToBool()
641+
} else {
642+
$deserialized[$parameter.Key] = $parameter.Value
643+
}
644+
}
645+
$deserialized['ResourceGroupName'] = $ResourceGroupName
646+
$deserialized['DeploymentOutputs'] = $deploymentOutputs
647+
$serialized = $deserialized | ConvertTo-Json
648+
649+
$outScript = @"
650+
`$parameters = `@'
651+
$serialized
652+
'`@ | ConvertFrom-Json -AsHashtable
653+
# Set global variables that aren't always passed as parameters
654+
`$ResourceGroupName = `$parameters.ResourceGroupName
655+
`$DeploymentOutputs = `$parameters.DeploymentOutputs
656+
$postDeploymentScript `@parameters
657+
"@
658+
$outScript | Out-File $SelfContainedPostScript
659+
LogVsoCommand "##vso[task.setvariable variable=SELF_CONTAINED_TEST_RESOURCES_POST_SCRIPT;]$selfContainedScript"
660+
} else {
661+
Log "Invoking post-deployment script '$postDeploymentScript'"
662+
&$postDeploymentScript -ResourceGroupName $ResourceGroupName -DeploymentOutputs $deploymentOutputs @PSBoundParameters
663+
}
631664
}
632665

633666
if ($templateFile.jsonFilePath.EndsWith('.compiled.json')) {

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

+25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ parameters:
99
ResourceType: test
1010
UseFederatedAuth: true
1111
PersistOidcToken: false
12+
SelfContainedPostScript: self-contained-test-resources-post.ps1
1213

1314
# SubscriptionConfiguration will be splatted into the parameters of the test
1415
# resources script. It should be JSON in the form:
@@ -89,6 +90,8 @@ steps:
8990
# Write the new SubscriptionConfiguration to be used by the remove test resources
9091
Write-Host "##vso[task.setvariable variable=SubscriptionConfiguration;]$($subscriptionConfiguration | ConvertTo-Json -Compress)"
9192
93+
$postScriptPath = $${{ parameters.PersistOidcToken }} ? '$(Agent.TempDirectory)/${{ parameters.SelfContainedPostScript }}' : $null
94+
9295
# The subscriptionConfiguration may have ArmTemplateParameters defined, so
9396
# pass those in via the ArmTemplateParameters flag, and handle any
9497
# additional parameters from the pipelines via AdditionalParameters
@@ -100,10 +103,32 @@ steps:
100103
@subscriptionConfiguration `
101104
-AdditionalParameters ${{ parameters.ArmTemplateParameters }} `
102105
-AllowIpRanges ('$(azsdk-corp-net-ip-ranges)' -split ',') `
106+
-SelfContainedPostScript $postScriptPath `
103107
-CI `
104108
-Force `
105109
-Verbose | Out-Null
106110
111+
- ${{ if eq(parameters.PersistOidcToken, true) }}:
112+
# ARM deployments that take longer than 10-15 minutes (e.g. HSM) can
113+
# cause post scripts to fail with expired credentials.
114+
# Add a new task with a refreshed token as a workaround to this issue.
115+
- task: AzurePowerShell@5
116+
displayName: Test Resources Post with refreshed login
117+
env:
118+
ARM_OIDC_TOKEN: $(ARM_OIDC_TOKEN)
119+
${{ insert }}: ${{ parameters.EnvVars }}
120+
inputs:
121+
azureSubscription: ${{ parameters.ServiceConnection }}
122+
azurePowerShellVersion: LatestVersion
123+
pwsh: true
124+
ScriptType: InlineScript
125+
Inline: |
126+
$scriptPath = $(Agent.TempDirectory)/${{ parameters.SelfContainedPostScript }}
127+
cat $scriptPath
128+
Write-Host "Executing self contained test resources post script"
129+
& $scriptPath
130+
Remove-Item $scriptPath # avoid any possible complications when we run multiple deploy templates
131+
107132
- ${{ else }}:
108133
- pwsh: |
109134
eng/common/scripts/Import-AzModules.ps1

0 commit comments

Comments
 (0)