You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Updated resource provisioning script with creation of app registration for our function app, where the client id is automatically set in the appsettings of said function using config set
Static web app built with the React framework. The [application](client/src/App.js) allows users to upload CSV files to a storage blob via an HTTP-triggered function.
4
-
The uploadedfiles are then processed by a blob-triggered function, which stores the results in a separate container. Aforementioned functions
5
-
are present in the [function_app.py](hvalfangst_function/function_app.py) python script - which is the main entrypoint of our Azure Function App instance.
3
+
This [static web app](client/src/App.js), built with the React framework, enables users to upload CSV files containing demographic and financial data about individuals to a designated storage blob (**in**) via an HTTP-triggered Azure Function.
4
+
Once uploaded, a blob-triggered Azure Function processes the files to calculate correlations between various variables, such as experience, state, gender, and income. The computed statistics are then stored in a separate storage blob (**out**).
5
+
These functions are implemented in the python script [function_app.py](hvalfangst_function/function_app.py); which is the main entrypoint of our Azure Function App instance.
6
6
7
-
A pipeline has been set up to deploy the function app and the static web app to Azure using GitHub Actions. The pipeline is triggered by a push to the main branch or by manually running the workflow.
7
+
A branch-triggered pipeline has been set up to deploy the function app and the static web app to Azure using a GitHub Actions Workflow [script](.github/workflows/deploy_to_azure.yml). A service principal has been created as part of the resource provisioning script, which is used
8
+
to authenticate our requests in said pipeline script. It is therefore important to set the associated GitHub secrets in the repository settings.
8
9
9
10
10
11
@@ -18,8 +19,8 @@ A pipeline has been set up to deploy the function app and the static web app to
18
19
19
20
## Allocate resources
20
21
21
-
The shell script [allocate_resources](infra/allocate_resources.sh) creates Azure resources specified in a
It will create the following hierarchy of resources:
25
26
@@ -41,20 +42,11 @@ graph TD
41
42
B -->|Contains| F
42
43
```
43
44
44
-
## Deallocate resources
45
-
46
-
The shell script [deallocate_resources](infra/deallocate_resources.sh) deletes our Azure resources.
47
-
48
-
# CI/CD
49
-
50
-
A CI/CD pipeline for deploying our [Function App](hvalfangst_function/function_app.py) to Azure has been set up using a GitHub Actions workflows [script](.github/workflows/deploy_to_azure.yml). The pipeline is either triggered by a push to the main branch or by manually running the workflow.
51
-
In order for the pipeline to work, the following secrets must be set in the repository settings:
52
-
53
-

54
-
55
-
The associated values of the aforementioned secret can be retrieved from the Azure portal, under our deployed Function App.
56
-
Click on the **Get publish profile** button and copy/paste the file content into the secret value field.
57
-
58
-

59
-
45
+
## GitHub secrets
46
+
As touched upon earlier, the GitHub secret **AZURE_CREDENTIALS** must be set in the repository settings. This secret comprises a JSON object containing the service principal credentials.
47
+
It is generated by the Azure CLI command `az ad sp create-for-rbac`, which
48
+
was executed as part of our [resource provisioning script](infra/allocate_resources.sh). The resulting terminal output of said command needs to be copy/pasted to the secret field.
49
+
Again, it is used to authenticate our requests in the [GitHub Actions Workflow script](.github/workflows/deploy_to_azure.yml) as contributor access to the resource group is necessary
50
+
in order to deploy our function and static web app. There are many ways to do this, but this is obviously a simple example.
Copy file name to clipboardExpand all lines: infra/allocate_resources.sh
+42-10Lines changed: 42 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,23 @@
1
1
#!/bin/bash
2
2
3
-
#Define colors for console output
3
+
#Colors for console output
4
4
GREEN="\e[32m"
5
5
RED="\e[31m"
6
6
BLUE="\e[34m"
7
7
CYAN="\e[36m"
8
8
YELLOW="\e[33m"
9
9
RESET="\e[0m"
10
10
11
-
# Variables
12
-
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
11
+
# Constants
13
12
RESOURCE_GROUP="hvalfangstresourcegroup"
14
13
STORAGE_ACCOUNT_NAME="hvalfangststorageaccount"
15
14
FUNCTION_APP_NAME="hvalfangstlinuxfunctionapp"
16
15
LOCATION="westeurope"
17
16
BICEP_FILE="infra/main.bicep"
18
17
18
+
# Set environment variable to prevent path conversion in MSYS (https://github.com/Azure/azure-cli/blob/dev/doc/use_cli_with_git_bash.md#auto-translation-of-resource-ids)
19
+
export MSYS_NO_PATHCONV=1;
20
+
19
21
# Function to handle errors
20
22
handle_error() {
21
23
echo -e "${RED}Error occurred in script at line: ${BASH_LINENO[0]}. Exiting...${RESET}"
@@ -25,7 +27,7 @@ handle_error() {
25
27
# Set trap to catch errors and execute handle_error
26
28
trap'handle_error' ERR
27
29
28
-
# Check if logged in to Azure
30
+
# Check if you are logged in to Azure
29
31
echo -e "${YELLOW}Checking if logged in to Azure...${RESET}"
30
32
az account show
31
33
@@ -34,9 +36,13 @@ if [ $? -ne 0 ]; then
34
36
exit 1
35
37
fi
36
38
39
+
# Variables retrieved from Azure CLI
40
+
SUBSCRIPTION_ID=$(az account show --query id --output tsv)
41
+
TENANT_ID=$(az account show --query tenantId --output tsv)
42
+
37
43
# Create Resource Group
38
-
echo -e "${YELLOW}Creating resource group $RESOURCE_GROUP in $LOCATION${RESET}"
39
-
az group create --name $RESOURCE_GROUP --location $LOCATION
44
+
echo -e "${YELLOW}Creating resource group ${RESOURCE_GROUP} in ${LOCATION}${RESET}"
45
+
az group create --name ${RESOURCE_GROUP} --location ${LOCATION}
40
46
if [ $?-ne 0 ];then
41
47
echo -e "${RED}Failed to create resource group.${RESET}"
42
48
exit 1
@@ -51,13 +57,15 @@ if [ $? -ne 0 ]; then
51
57
exit 1
52
58
fi
53
59
60
+
# Create service principal used by GitHub Actions, the returned JSON is stored as secret in the GitHub repository
54
61
echo -e "${YELLOW}Creating service principal...${RESET}"
55
-
az ad sp create-for-rbac --name hvalfangst --role contributor --scopes /subscriptions/$SUBSCRIPTION_ID/resourceGroups/$RESOURCE_GROUP --sdk-auth
62
+
az ad sp create-for-rbac --name hvalfangst-github-actions-sp --role contributor --scopes /subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}
56
63
if [ $?-ne 0 ];then
57
64
echo -e "${RED}Failed to create service principal.${RESET}"
58
65
exit 1
59
66
fi
60
67
68
+
# Set up our storage container to serve static website with default index and 404 page
61
69
echo -e "${YELLOW}Setting up static website...${RESET}"
62
70
az storage blob service-properties update \
63
71
--account-name ${STORAGE_ACCOUNT_NAME} \
@@ -69,12 +77,36 @@ if [ $? -ne 0 ]; then
69
77
exit 1
70
78
fi
71
79
80
+
# Set up CORS for our Function App, which is used for our HTTP-triggered function
72
81
echo -e "${YELLOW}Setting up CORS for function app...${RESET}"
73
-
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group $RESOURCE_GROUP --allowed-origins http://localhost:3000
74
-
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group $RESOURCE_GROUP --allowed-origins https://hvalfangststorageaccount.z6.web.core.windows.net
82
+
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group ${RESOURCE_GROUP} --allowed-origins http://localhost:3000
83
+
az functionapp cors add --name ${FUNCTION_APP_NAME} --resource-group ${RESOURCE_GROUP} --allowed-origins https://hvalfangststorageaccount.z6.web.core.windows.net
75
84
if [ $?-ne 0 ];then
76
85
echo -e "${RED}Failed to set up CORS for function app.${RESET}"
77
86
exit 1
78
87
fi
79
88
80
-
echo -e "${GREEN}All resources have been provisioned.${RESET}"
89
+
# Set up app registration for function app
90
+
echo -e "${YELLOW}Setting up app registration for function app...${RESET}"
91
+
FUNCTION_APP_CLIENT_ID=$(az ad app create \
92
+
--display-name "hvalfangst-function-app" \
93
+
--query appId -o tsv)
94
+
95
+
if [ $?-ne 0 ] || [ -z"$FUNCTION_APP_CLIENT_ID" ];then
96
+
echo -e "${RED}Failed to set up app registration or retrieve the app ID.${RESET}"
97
+
exit 1
98
+
fi
99
+
100
+
# Set up app settings for the function app
101
+
echo -e "${YELLOW}Setting up app settings for function app...${RESET}"
0 commit comments