This repository was archived by the owner on Nov 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·201 lines (154 loc) · 5.51 KB
/
deploy.sh
File metadata and controls
executable file
·201 lines (154 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
set -e
# function to echo in green
function info() {
echo -e "\033[0;32m$1\033[0m"
}
# function to add a secret to a key vault
function addSecret() {
local secretName=$1
local secretValue=$2
local prefix=$3
az keyvault secret set \
--vault-name ${prefix}keyvault \
--name $secretName \
--value $secretValue
}
function getFunctionKey() {
local functionName=$1
info "Doing up to 20 attempts to retrieve the function key for $functionName..."
local attempts=20
local sleepTime=30
set +e
while [ $attempts -gt 0 ]; do
functionKey=$(az functionapp keys list --resource-group ${RESOURCE_GROUP_NAME} --name $functionName --query "functionKeys.default" --output tsv)
if [ -z "$functionKey" ]; then
info "Function key not found, retrying in ${sleepTime}s..."
sleep $sleepTime
attempts=$((attempts-1))
else
info "Function key found"
break
fi
done
set -e
}
# require RESOURCE_GROUP_NAME, LOCATION and APPLIANCE_RESOURCE_PROVIDER_OBJECT_ID
if [ -z "$RESOURCE_GROUP_NAME" ]; then
echo "RESOURCE_GROUP_NAME is not set"
exit 1
fi
if [ -z "$LOCATION" ]; then
echo "LOCATION is not set"
exit 1
fi
if [ -z "$APPLIANCE_RESOURCE_PROVIDER_OBJECT_ID" ]; then
echo "APPLIANCE_RESOURCE_PROVIDER_OBJECT_ID is not set"
exit 1
fi
azSubInfo=$(az account show)
info "Current azure subscription:"
info "$azSubInfo"
if [ -z "$NO_REDEPLOY" ]; then
# get self principalId
principalId=$(az ad signed-in-user show --query id -o tsv)
tag=$(date +%s)
echo $RESOURCE_GROUP_NAME > .resourceGroupName
info "Deploying bicep file..."
az deployment sub create \
--no-prompt \
--name "ama-update-sample" \
--location $LOCATION \
--template-file ./publisher/iac/main.bicep \
--parameters \
tag=$tag \
principalId=$principalId \
resourceGroupName=$RESOURCE_GROUP_NAME \
applianceResourceProviderObjectId=$APPLIANCE_RESOURCE_PROVIDER_OBJECT_ID
fi
# get output from deployment
prefix=$(az deployment sub show --name "ama-update-sample" --query properties.outputs.prefix.value -o tsv)
info "Prefix: $prefix"
tag=$(az deployment sub show --name "ama-update-sample" --query properties.outputs.tag.value -o tsv)
info "Tag: $tag"
acrName="${prefix}acr"
if [ -z "$NO_REDEPLOY" ]; then
info "Waiting 60s for resources to be available..."
sleep 60
info "Logging in to ACR..."
az acr login --name $acrName
info "Building and pushing docker image..."
DOCKER_IMAGE_TAG=$tag ACR_NAME=$acrName DOCKER_PUSH=true ./build-docker.sh
fi
webhookFunctionName="${prefix}webhook"
functionKey=""
getFunctionKey $webhookFunctionName
webhookFunctionKey=$functionKey
webhookFunctionUrl="https://${webhookFunctionName}.azurewebsites.net/api?code=${webhookFunctionKey}"
setCommandUrlFunctionName="${prefix}setcommandurl"
functionKey=""
getFunctionKey $setCommandUrlFunctionName
setCommandUrlFunctionKey=$functionKey
info "Adding the key to key vault..."
secretValue="https://${setCommandUrlFunctionName}.azurewebsites.net/api/setcommandurl?code=${setCommandUrlFunctionKey}"
addSecret "setcommandurlurl" $secretValue $prefix
eventsFunctionName="${prefix}events"
functionKey=""
getFunctionKey $eventsFunctionName
eventsFunctionKey=$functionKey
info "Adding the key to key vault..."
secretValue="https://${eventsFunctionName}.azurewebsites.net/api/events?code=${eventsFunctionKey}"
addSecret "eventsurl" $secretValue $prefix
info "Creating acr pull token..."
pullTokenName="pullToken"
pullToken=$(az acr token create \
--name pullToken \
--registry $acrName \
--scope-map "_repositories_pull" \
--query "credentials.passwords[0].value" \
--output tsv)
info "Adding the pull token to key vault..."
acrServer=$(az acr show --name $acrName --query loginServer --output tsv)
addSecret "registry" $acrServer $prefix
addSecret "registryUsername" $pullTokenName $prefix
addSecret "registryPassword" $pullToken $prefix
info "Building the Azure Managed App definition..."
DOCKER_IMAGE_TAG=${tag} PUBLISHER_PREFIX=${prefix} PUBLISHER_RESOURCE_GROUP=${RESOURCE_GROUP_NAME} ./build-ama-definition.sh
info "Getting the 'Owner' role id..."
ownerRoleId=$(az role definition list --name Owner --query [].name --output tsv)
info "Getting app definition URL from blob storage..."
containerName="appdefinition"
blobName="appDefinition.zip"
accountName="${prefix}storage"
appDefinitionUrl=$(az storage blob url \
--account-name ${accountName} \
--container-name ${containerName} \
--auth-mode login \
--name ${blobName} \
--output tsv)
# tomorrow's date
os=$(uname -s)
if [ "$os" == "Darwin" ]; then
expiryDate=$(date -v+1d '+%Y-%m-%dT%H:%M:%SZ')
else
expiryDate=$(date -d "+1 day" '+%Y-%m-%dT%H:%M:%SZ')
fi
blobQuerystring=$(az storage blob generate-sas \
--account-name ${accountName} \
--container-name ${containerName} \
--name ${blobName} \
--as-user \
--auth-mode login \
--permissions r \
--expiry ${expiryDate} \
--output tsv)
appDefinitionFullUrl="${appDefinitionUrl}?${blobQuerystring}"
info "Deploying the Azure Managed App..."
az deployment group create \
--name "ama-update-sample-definition" \
--resource-group $RESOURCE_GROUP_NAME \
--template-file ./ama-definition-deploy.json \
--parameters \
applicationName="ama-update-sample" \
_artifactsLocation=$appDefinitionFullUrl \
notificationUrl=$webhookFunctionUrl