Repository navigation
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·155 lines (137 loc) · 5.08 KB
/
Copy pathdeploy.sh
File metadata and controls
executable file
·155 lines (137 loc) · 5.08 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
#!/bin/bash
# Variables
TEMPLATE="main.bicep"
PARAMETERS="main.bicepparam"
PREFIX="${PREFIX:-local}"
SUFFIX="${SUFFIX:-test}"
RESOURCE_GROUP_NAME="${PREFIX}-rg"
LOCATION="westeurope"
VALIDATE_TEMPLATE=1
USE_WHAT_IF=0
SUBSCRIPTION_NAME=$(az account show --query name --output tsv)
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
ZIPFILE="function_app.zip"
DEPLOY_RETRY_COUNT=3
DEPLOY_RETRY_SLEEP=30
SUBSCRIPTION_NAME=$(az account show --query name --output tsv)
# Change the current directory to the script's directory
cd "$CURRENT_DIR" || exit
# Validates if the resource group exists in the subscription, if not creates it
echo "Checking if resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]..."
az group show --name $RESOURCE_GROUP_NAME &>/dev/null
if [[ $? != 0 ]]; then
echo "No resource group [$RESOURCE_GROUP_NAME] exists in the subscription [$SUBSCRIPTION_NAME]"
echo "Creating resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]..."
# Create the resource group
az group create --name $RESOURCE_GROUP_NAME --location $LOCATION 1>/dev/null
if [[ $? == 0 ]]; then
echo "Resource group [$RESOURCE_GROUP_NAME] successfully created in the subscription [$SUBSCRIPTION_NAME]"
else
echo "Failed to create resource group [$RESOURCE_GROUP_NAME] in the subscription [$SUBSCRIPTION_NAME]"
exit 1
fi
else
echo "Resource group [$RESOURCE_GROUP_NAME] already exists in the subscription [$SUBSCRIPTION_NAME]"
fi
# Validates the Bicep template
if [[ $VALIDATE_TEMPLATE == 1 ]]; then
if [[ $USE_WHAT_IF == 1 ]]; then
# Execute a deployment What-If operation at resource group scope.
echo "Previewing changes deployed by Bicep template [$TEMPLATE]..."
az deployment group what-if \
--resource-group $RESOURCE_GROUP_NAME \
--template-file $TEMPLATE \
--parameters $PARAMETERS \
--parameters \
location=$LOCATION prefix="$PREFIX" suffix="$SUFFIX"
if [[ $? == 0 ]]; then
echo "Bicep template [$TEMPLATE] validation succeeded"
else
echo "Failed to validate Bicep template [$TEMPLATE]"
exit 1
fi
else
# Validate the Bicep template
echo "Validating Bicep template [$TEMPLATE]..."
output=$(az deployment group validate \
--resource-group $RESOURCE_GROUP_NAME \
--template-file $TEMPLATE \
--parameters $PARAMETERS \
--parameters \
location=$LOCATION prefix="$PREFIX" suffix="$SUFFIX")
if [[ $? == 0 ]]; then
echo "Bicep template [$TEMPLATE] validation succeeded"
else
echo "Failed to validate Bicep template [$TEMPLATE]"
echo "$output"
exit 1
fi
fi
fi
# Deploy the Bicep template
echo "Deploying Bicep template [$TEMPLATE]..."
if DEPLOYMENT_OUTPUTS=$(az deployment group create \
--resource-group $RESOURCE_GROUP_NAME \
--only-show-errors \
--template-file $TEMPLATE \
--parameters $PARAMETERS \
--parameters location=$LOCATION prefix="$PREFIX" suffix="$SUFFIX" \
--query 'properties.outputs' -o json); then
echo "Bicep template [$TEMPLATE] deployed successfully. Outputs:"
echo "$DEPLOYMENT_OUTPUTS" | jq .
FUNCTION_APP_NAME=$(echo "$DEPLOYMENT_OUTPUTS" | jq -r '.functionAppName.value')
STORAGE_ACCOUNT_NAME=$(echo "$DEPLOYMENT_OUTPUTS" | jq -r '.storageAccountName.value')
echo "Function App Name: $FUNCTION_APP_NAME"
echo "Storage Account Name: $STORAGE_ACCOUNT_NAME"
else
echo "Failed to deploy Bicep template [$TEMPLATE]"
exit 1
fi
if [[ -z "$FUNCTION_APP_NAME" || -z "$STORAGE_ACCOUNT_NAME" ]]; then
echo "Function App Name or Storage Account Name is empty. Exiting."
exit 1
fi
# Print the names of the application settings of the function app: their values hold secrets, such as the connection
# string of the storage account
echo "Retrieving the names of the application settings of the function app [$FUNCTION_APP_NAME]..."
az webapp config appsettings list \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$FUNCTION_APP_NAME" \
--query "[].name" \
--output tsv
# CD into the function app directory
cd ../src || exit
# Create the zip package of the function app: the remote build installs requirements.txt
rm -f $ZIPFILE
zip -r $ZIPFILE function_app.py host.json requirements.txt
# Deploy the function app using the zip file
echo "Deploying function app [$FUNCTION_APP_NAME]..."
# On Azure the first deployment to a new function app can time out (504) while its deployment site starts, so a failed
# attempt is retried
DEPLOYED=0
for ((ATTEMPT = 1; ATTEMPT <= DEPLOY_RETRY_COUNT; ATTEMPT++)); do
az functionapp deploy \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$FUNCTION_APP_NAME" \
--src-path "$ZIPFILE" \
--type zip \
--async true 1>/dev/null
if [[ $? == 0 ]]; then
DEPLOYED=1
break
fi
if [[ $ATTEMPT -lt $DEPLOY_RETRY_COUNT ]]; then
echo "Attempt $ATTEMPT of $DEPLOY_RETRY_COUNT to deploy function app [$FUNCTION_APP_NAME] failed. Waiting [$DEPLOY_RETRY_SLEEP] seconds before retry..."
sleep $DEPLOY_RETRY_SLEEP
fi
done
if [[ $DEPLOYED == 1 ]]; then
echo "Function app [$FUNCTION_APP_NAME] deployed successfully."
else
echo "Failed to deploy function app [$FUNCTION_APP_NAME]."
exit 1
fi
# Remove the zip package of the function app
if [ -f "$ZIPFILE" ]; then
rm "$ZIPFILE"
fi