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
references/recipes/azd/scripts/set-aspire-aca-env.sh
references/recipes/azd/scripts/ set-aspire-aca-env.sh
Shell · 106 lines · 4 KB
15 #
16 # The script only sets a variable if it is currently missing, and prints what it did so the
17 # result can be understood without re-inspecting `azd env get-values`:
18 # AZURE_CONTAINER_REGISTRY_ENDPOINT <- az acr list ... [0].loginServer
19 # AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID <- az identity list ... [0].id
20 # MANAGED_IDENTITY_CLIENT_ID <- az identity list ... [0].clientId
21
22 set -e
23
24 AZD_ENV_NAME = ""
25 while [ $# -gt 0 ]; do
26 case " $1 " in
27 -e | --environment )
28 AZD_ENV_NAME = " $2 "
29 shift 2
30 ;;
31 *)
32 echo "ERROR: Unknown argument: $1 " >&2
33 echo "USAGE: ./set-aspire-aca-env.sh [-e <azd-env-name>]" >&2
34 exit 1
35 ;;
36 esac
37 done
38
39 # Build the shared `-e <name>` argument list for azd calls (empty when no env name given).
40 AZD_ENV_ARGS = ""
41 if [ -n " $AZD_ENV_NAME " ]; then
42 AZD_ENV_ARGS = "-e $AZD_ENV_NAME "
43 fi
44
45 # Capture azd environment values via command substitution so `set -e` aborts if the
46 # `azd env get-values` call itself fails (rather than silently continuing with no values).
47 AZD_VALUES = $( azd env get-values $AZD_ENV_ARGS)
48
49 # get_env_value <KEY> — print the (unquoted) value of KEY from AZD_VALUES, empty if absent.
50 # Uses only POSIX-friendly tools so it works on the widely-available Bash 3.2 (e.g. macOS).
51 get_env_value () {
52 printf '%s\n' " $AZD_VALUES " \
53 | grep "^ $1 =" \
54 | head -n 1 \
55 | sed -e "s/^ $1 =//" -e 's/^"\(.*\)"$/\1/' -e "s/^'\(.*\)'$/\1/"
56 }
57
58 RG_NAME = $( get_env_value AZURE_RESOURCE_GROUP )
59 if [ -z " $RG_NAME " ]; then
60 echo "ERROR: AZURE_RESOURCE_GROUP is not set in the azd environment." >&2
61 echo "Run 'azd provision' before this script so the resource group is available." >&2
62 exit 1
63 fi
64
65 # set_if_missing <ENV_VAR_NAME> <description> <resolver command...>
66 # The resolver command is passed as arguments and run via "$@" — no eval.
67 set_if_missing () {
68 var_name = " $1 "
69 description = " $2 "
70 shift 2
71
72 existing = $( get_env_value " $var_name " )
73 if [ -n " $existing " ]; then
74 echo " $var_name : already present ( $existing )"
75 return 0
76 fi
77
78 value = $( " $@ " )
79 if [ -z " $value " ]; then
80 echo "ERROR: Could not resolve $var_name ( $description ) in resource group ' $RG_NAME '." >&2
81 echo "Confirm 'azd provision' completed and the resource exists." >&2
82 exit 1
83 fi
84
85 azd env set $AZD_ENV_ARGS " $var_name " " $value "
86 echo " $var_name : set to $value "
87 }
88
89 echo "Resource group: $RG_NAME "
90
91 set_if_missing \
92 "AZURE_CONTAINER_REGISTRY_ENDPOINT" \
93 "container registry login server" \
94 az acr list --resource-group " $RG_NAME " --query "[0].loginServer" -o tsv
95
96 set_if_missing \
97 "AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID" \
98 "managed identity resource id" \
99 az identity list --resource-group " $RG_NAME " --query "[0].id" -o tsv
100
101 set_if_missing \
102 "MANAGED_IDENTITY_CLIENT_ID" \
103 "managed identity client id" \
104 az identity list --resource-group " $RG_NAME " --query "[0].clientId" -o tsv
105
106 echo "Aspire Container Apps environment variables are ready for 'azd deploy'."