-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·139 lines (109 loc) · 3.93 KB
/
build.sh
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
#!/bin/bash
set -e
# These 3 variables constant for all development projects
BACKEND_RESOURCE_GROUP_NAME=terraform-state-rg
BACKEND_STORAGE_CONTAINER_NAME=tfstate
SUBSCRIPTION_ID=$(az account show --query id -o tsv)
FUNCTION_NAME=visit_counter
if [ -z $GITHUB_ACTIONS ]; then
RESOURCE_GROUP_NAME="dev-$(date +%Y%m%d%H%M)-rg"
else
RESOURCE_GROUP_NAME="test-$(date +%Y%m%d%H%M)-rg"
fi
echo "Creating resource group if it does not exist"
if ! az group exists --name "$BACKEND_RESOURCE_GROUP_NAME"; then
az group create --name $BACKEND_RESOURCE_GROUP_NAME --location eastus2
else
echo "Resource group $BACKEND_RESOURCE_GROUP_NAME already exists."
fi
echo "Creating backend storage account"
while true; do
# Generate a new storage account name
BACKEND_STORAGE_ACCOUNT_NAME="devtfstate$RANDOM"
# Check if the storage account name already exists
account_check=$(az storage account check-name --name $BACKEND_STORAGE_ACCOUNT_NAME --query 'nameAvailable')
# If the name is available, break out of the loop
if [ $account_check = "true" ]; then
break
fi
done
az storage account create \
--resource-group $BACKEND_RESOURCE_GROUP_NAME \
--name $BACKEND_STORAGE_ACCOUNT_NAME \
--sku Standard_LRS
az storage container create \
--name $BACKEND_STORAGE_CONTAINER_NAME \
--account-name $BACKEND_STORAGE_ACCOUNT_NAME
echo "Initiating terraform backend"
cd terraform
terraform init \
-backend-config="resource_group_name=$BACKEND_RESOURCE_GROUP_NAME" \
-backend-config="storage_account_name=$BACKEND_STORAGE_ACCOUNT_NAME" \
-backend-config="container_name=$BACKEND_STORAGE_CONTAINER_NAME"
echo "Creating terraform plan"
terraform plan \
-var "rg_name=$RESOURCE_GROUP_NAME" \
-var "subscription_id=$AZURE_SUBSCRIPTION_ID" \
-out=tfplan
echo "Applying the terraform plan"
terraform apply -auto-approve tfplan
RESOURCE_GROUP_NAME=$(terraform output -raw resource_group_name)
COSMOSDB_CONNECTION_STRING=$(terraform output -raw cosmosdb_connection_string)
FUNCTION_APP_NAME=$(terraform output -raw function_app_name)
FUNCTION_APP_URL=$(terraform output -raw function_app_url)
STATIC_STORAGE_ACCOUNT_NAME=$(terraform output -raw static_website_storage_name)
echo "Function Setup"
cd ..
echo "Packaging function"
cd backend/api
zip -r ../../function.zip * -x "local.settings.json" ".venv/*" "visit_counter/__pycache__/*"
cd ../..
echo "Deploying function"
az functionapp deployment source config-zip \
--resource-group $RESOURCE_GROUP_NAME \
--name $FUNCTION_APP_NAME \
--src function.zip
sleep 90
echo "Getting function key"
FUNCTION_KEY=$(
az functionapp function keys list \
--name $FUNCTION_APP_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--function-name $FUNCTION_NAME \
--query "default" -o tsv
)
echo "Adding function url to visitcounter.js"
FUNCTION_URL="https://${FUNCTION_APP_URL}/api/visit_counter?code=${FUNCTION_KEY}"
# Replace TODO with the function url
#using -i.bak is a hack to enable sed to work on both mac and linux OS
sed -i.bak "s|TODO|$FUNCTION_URL|" ./frontend/js/visitcounter.js
echo "Uploading frontend contents"
az storage blob upload-batch \
--account-name $STATIC_STORAGE_ACCOUNT_NAME \
--auth-mode key \
--overwrite=true \
--destination '$web' \
--source frontend/
ENDPOINT_HOSTNAME="https://${STATIC_STORAGE_ACCOUNT_NAME}.azureedge.net"
echo "Enabling CORS"
az functionapp cors remove --name $FUNCTION_APP_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--allowed-origins
az functionapp cors add --name $FUNCTION_APP_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--allowed-origins "*"
variables=(
"BACKEND_RESOURCE_GROUP_NAME"
"BACKEND_STORAGE_ACCOUNT_NAME"
"RESOURCE_GROUP_NAME"
"ENDPOINT_HOSTNAME"
"FUNCTION_URL"
)
if [ -z $GITHUB_ACTIONS ]; then
file_name="dev.env"
else
file_name="test.env"
fi
for var in "${variables[@]}"; do
echo "${var}=${!var}" >>$file_name
done