Setting the file. One moment.
Set Aspire Aca Env · Azure Validate · microsoft/azure-skills · Skills Docs
ContentsBack to the top of the page 28.8
Errors · azd
(opens in a new tab)
references/recipes/azd/scripts/ set-aspire-aca-env.ps1
PowerShell · 102 lines · 4 KB
15
AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID <- az identity list ... [0].id
16 MANAGED_IDENTITY_CLIENT_ID <- az identity list ... [0].clientId
17
18 . PARAMETER Environment
19 Optional azd environment name (forwarded to `azd env` calls).
20 Defaults to the current/default azd environment.
21
22 . EXAMPLE
23 ./set-aspire-aca-env.ps1
24
25 . EXAMPLE
26 ./set-aspire-aca-env.ps1 -Environment my-azd-env
27 #>
28
29 param (
30 [ string ]$Environment
31 )
32
33 # Shared `-e <name>` argument list for azd calls (empty when no env name given).
34 $azdEnvArgs = @ ()
35 if ($Environment) {
36 $azdEnvArgs = @ ( '-e' , $Environment)
37 }
38
39 # Capture azd environment values and verify the call succeeded. PowerShell does not treat a
40 # native command's non-zero exit code as a terminating error, so check $LASTEXITCODE explicitly
41 # rather than relying on $ErrorActionPreference.
42 $azdOutput = azd env get-values @azdEnvArgs
43 if ( $LASTEXITCODE -ne 0 ) {
44 Write-Error "'azd env get-values' failed with exit code $LASTEXITCODE . Run 'azd provision' before this script so the azd environment is available."
45 exit 1
46 }
47
48 # Load azd environment values into a hashtable.
49 $azdValues = @ {}
50 foreach ($line in $azdOutput) {
51 if ( -not $line) { continue }
52 $name , $value = $line.Split( '=' , 2 )
53 $azdValues[$name] = $value.Trim( '"' )
54 }
55
56 $rgName = $azdValues[ 'AZURE_RESOURCE_GROUP' ]
57 if ( -not $rgName) {
58 Write-Error "AZURE_RESOURCE_GROUP is not set in the azd environment. Run 'azd provision' before this script so the resource group is available."
59 exit 1
60 }
61
62 function Set-IfMissing {
63 param (
64 [ string ]$VarName ,
65 [ string ]$Description ,
66 [ scriptblock ]$ValueCommand
67 )
68
69 $existing = $azdValues[$VarName]
70 if ($existing) {
71 Write-Host " ${VarName} : already present ( $existing )"
72 return
73 }
74
75 $value = ( & $ValueCommand)
76 if ( -not $value) {
77 Write-Error "Could not resolve $VarName ( $Description ) in resource group ' $rgName '. Confirm 'azd provision' completed and the resource exists."
78 exit 1
79 }
80
81 azd env set @azdEnvArgs $VarName $value
82 Write-Host " ${VarName} : set to $value "
83 }
84
85 Write-Host "Resource group: $rgName "
86
87 Set-IfMissing `
88 - VarName 'AZURE_CONTAINER_REGISTRY_ENDPOINT' `
89 - Description 'container registry login server' `
90 - ValueCommand { az acr list -- resource - group $rgName -- query "[0].loginServer" - o tsv }
91
92 Set-IfMissing `
93 - VarName 'AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID' `
94 - Description 'managed identity resource id' `
95 - ValueCommand { az identity list -- resource - group $rgName -- query "[0].id" - o tsv }
96
97 Set-IfMissing `
98 - VarName 'MANAGED_IDENTITY_CLIENT_ID' `
99 - Description 'managed identity client id' `
100 - ValueCommand { az identity list -- resource - group $rgName -- query "[0].clientId" - o tsv }
101
102 Write-Host "Aspire Container Apps environment variables are ready for 'azd deploy'."