-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy-azure-functions-code.sh
More file actions
72 lines (54 loc) · 2.8 KB
/
deploy-azure-functions-code.sh
File metadata and controls
72 lines (54 loc) · 2.8 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
#!/bin/bash
SUBSCRIPTION_ID=$1
RESOURCE_GROUP_NAME=$2
FUNCTION_APP_NAME=$3
if [[ -z $SUBSCRIPTION_ID ]] || [[ -z $RESOURCE_GROUP_NAME ]] || [[ -z $FUNCTION_APP_NAME ]]
then
echo "Parameters missing."
echo "Usage: deploy-azure-functions-code.sh subscription_id resource_group_name function_app_name"
echo "Try: deploy-azure-functions-code.sh XXX-XXX-XXX myRG myFunctionApp"
exit
fi
# Generate random storage account name
STORAGE_ACCOUNT_RANDOM_SUFFIX=$(echo $RANDOM | md5sum | head -c 15)
STORAGE_ACCOUNT_NAME="storage$STORAGE_ACCOUNT_RANDOM_SUFFIX"
STORAGE_CONTAINER_NAME="app-code"
# Pull location from function app
LOCATION=$(az functionapp show --resource-group "$RESOURCE_GROUP_NAME" --name "$FUNCTION_APP_NAME" --query location -o tsv) || exit 1
# Create storage account
echo "Creating storage account . . ."
az storage account create --location "$LOCATION" --name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --allow-blob-public-access false --https-only true --kind Storage --sku Standard_GRS || exit 1
# Create a container
az storage container create --name $STORAGE_CONTAINER_NAME --account-name "$STORAGE_ACCOUNT_NAME"
# Assign function app identity to storage account
echo "Creating function app managed identity and assigning to storage account . . ."
export MSYS_NO_PATHCONV=1
az functionapp identity assign \
--resource-group "$RESOURCE_GROUP_NAME" \
--name "$FUNCTION_APP_NAME" \
--role "Storage Blob Data Reader" \
--scope "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP_NAME/providers/Microsoft.Storage/storageAccounts/$STORAGE_ACCOUNT_NAME"
# Sleep for a few seconds to let AAD permissions update.
echo "Sleeping for 30 seconds . . . "
sleep 30
echo "Awake . . . resuming."
# Build code
cd ../src/http-trigger/ || exit
dotnet publish --configuration Release
# zip code
cd ./bin/Release/net6.0/publish || exit
zip -r code.zip .
# Copy the zipped file to the http-trigger folder.
cp code.zip ../../../../code.zip
# Change directory back to the http-trigger folder.
cd ../../../.. || exit
# Upload zipped code to storage account
echo "Uploading code to storage account . . . "
az storage blob upload --account-name "$STORAGE_ACCOUNT_NAME" --container $STORAGE_CONTAINER_NAME --file ./code.zip --name code.zip --auth-mode key
# Get URI for uploaded blob -
BLOB_PACKAGE_URI="https://$STORAGE_ACCOUNT_NAME.blob.core.windows.net/$STORAGE_CONTAINER_NAME/code.zip"
cd ../../scripts || exit
echo "Adding package setting to function app and restarting . . . "
az functionapp config appsettings set --resource-group "$RESOURCE_GROUP_NAME" --name "$FUNCTION_APP_NAME" --settings WEBSITE_RUN_FROM_PACKAGE="$BLOB_PACKAGE_URI"
# May only need to restart if a new package was uploaded.
az functionapp restart --resource-group "$RESOURCE_GROUP_NAME" --name "$FUNCTION_APP_NAME"