Skip to content

Commit 593897e

Browse files
authored
Update azd hooks (#5)
1 parent 7ae34f8 commit 593897e

13 files changed

+419
-150
lines changed

.devcontainer/devcontainer.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"GitHub.copilot-chat",
2626
"GitHub.vscode-github-actions",
2727
"GitHub.vscode-pull-request-github",
28+
"ms-azuretools.vscode-azure-github-copilot",
2829
"ms-azuretools.vscode-bicep",
2930
"ms-azuretools.vscode-docker",
3031
"ms-dotnettools.csharp",

.devcontainer/on-create.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
sudo apt-get update && \
22
sudo apt upgrade -y && \
3-
sudo apt-get install -y dos2unix libsecret-1-0 xdg-utils && \
3+
sudo apt-get install -y dos2unix libsecret-1-0 xdg-utils uuid-runtime && \
44
sudo apt clean -y && \
55
sudo rm -rf /var/lib/apt/lists/*
66

infra/hooks/deploy_swa.sh

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/bin/bash
2+
3+
# Runs the deploy_swa script
4+
# It does the following:
5+
# 1. Loads the azd environment variables
6+
# 2. Logs in to the Azure CLI if not running in a GitHub Action
7+
# 3. Build SWA app
8+
# 4. Deploy SWA app
9+
10+
set -e
11+
12+
# REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
13+
REPOSITORY_ROOT="$(dirname "$(realpath "$0")")/../.."
14+
15+
# Load the azd environment variables
16+
source "$REPOSITORY_ROOT/infra/hooks/load_azd_env.sh"
17+
18+
if [ -z "$GITHUB_WORKSPACE" ]; then
19+
# The GITHUB_WORKSPACE is not set, meaning this is not running in a GitHub Action
20+
source "$REPOSITORY_ROOT/infra/hooks/login.sh"
21+
fi
22+
23+
# Run only if GITHUB_WORKSPACE is NOT set - this is NOT running in a GitHub Action workflow
24+
if [ -z "$GITHUB_WORKSPACE" ]; then
25+
echo "Deploying to Azure Static Web Apps..."
26+
27+
RESOURCE_GROUP="rg-$AZURE_ENV_NAME"
28+
STATICAPP_NAME=$AZURE_RESOURCE_EASYAUTH_STATICAPP_NAME
29+
30+
# Build SWA app
31+
swa build
32+
33+
# Get deployment token
34+
deploymentToken=$(az staticwebapp secrets list \
35+
--resource-group "$RESOURCE_GROUP" \
36+
--name "$STATICAPP_NAME" \
37+
--query "properties.apiKey" -o tsv)
38+
39+
# Deploy SWA app
40+
swa deploy \
41+
--api-location src/EasyAuth.FunctionApp/bin/Release/net9.0 \
42+
--env Production \
43+
-d "$deploymentToken"
44+
45+
echo "...Done"
46+
else
47+
echo "Skipping to deploy the application Azure Static Web Apps..."
48+
fi

infra/hooks/load_azd_env.sh

+43-21
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
1-
# Loads the azd .env file into the current environment
2-
# It does the following:
3-
# 1. Loads the azd .env file from the current environment
4-
5-
Param(
6-
[switch]
7-
[Parameter(Mandatory=$false)]
8-
$ShowMessage
9-
)
10-
11-
if ($ShowMessage) {
12-
Write-Host "Loading azd .env file from current environment" -ForegroundColor Cyan
13-
}
14-
15-
foreach ($line in (& azd env get-values)) {
16-
if ($line -match "([^=]+)=(.*)") {
17-
$key = $matches[1]
18-
$value = $matches[2] -replace '^"|"$'
19-
[Environment]::SetEnvironmentVariable($key, $value)
20-
}
21-
}
1+
#!/bin/bash
2+
3+
set -e
4+
5+
SHOW_MESSAGE=false
6+
7+
if [[ $# -eq 0 ]]; then
8+
SHOW_MESSAGE=false
9+
fi
10+
11+
while [[ "$1" != "" ]]; do
12+
case $1 in
13+
-m | --show-message)
14+
SHOW_MESSAGE=true
15+
;;
16+
17+
*)
18+
usage
19+
exit 1
20+
;;
21+
esac
22+
23+
shift
24+
done
25+
26+
if [[ $SHOW_MESSAGE == true ]]; then
27+
echo -e "\033[0;36mLoading azd .env file from current environment...\033[0m"
28+
fi
29+
30+
# while IFS='=' read -r key value; do
31+
# value=$(echo "$value" | sed 's/^"//' | sed 's/"$//')
32+
# export "$key=$value"
33+
# done <<EOF
34+
# $(azd env get-values)
35+
# EOF
36+
37+
while IFS= read -r line; do
38+
if [[ $line =~ ^([^=]+)=(.*)$ ]]; then
39+
key="${BASH_REMATCH[1]}"
40+
value="${BASH_REMATCH[2]//\"}"
41+
export "$key"="$value"
42+
fi
43+
done < <(azd env get-values)

infra/hooks/login.sh

+46-43
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/bin/bash
2+
13
# Logs in to Azure through AZD and AZ CLI
24
# It does the following:
35
# 1. Checks if the user is logged in to Azure
@@ -8,61 +10,62 @@
810
# 6. Sets the active subscription to the selected subscription
911
# 7. Exits if the subscription is not found
1012

11-
# $REPOSITORY_ROOT = git rev-parse --show-toplevel
12-
$REPOSITORY_ROOT = "$(Split-Path $MyInvocation.MyCommand.Path)/../.."
13+
set -e
14+
15+
# REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
16+
REPOSITORY_ROOT="$(dirname "$(realpath "$0")")/../.."
1317

1418
# Load the azd environment variables
15-
& "$REPOSITORY_ROOT/infra/hooks/load_azd_env.ps1"
19+
"$REPOSITORY_ROOT/infra/hooks/load_azd_env.sh"
1620

1721
# AZD LOGIN
1822
# Check if the user is logged in to Azure
19-
$login_status = azd auth login --check-status
23+
login_status=$(azd auth login --check-status)
2024

2125
# Check if the user is not logged in
22-
if ($login_status -like "*Not logged in*") {
23-
Write-Host "Not logged in, initiating login process..."
24-
# Command to log in to Azure
25-
azd auth login
26-
}
26+
if [[ "$login_status" == *"Not logged in"* ]]; then
27+
echo "Not logged in, initiating login process..."
28+
# Command to log in to Azure
29+
azd auth login
30+
fi
2731

2832
# AZ LOGIN
29-
$EXPIRED_TOKEN = az ad signed-in-user show --query 'id' -o tsv 2>$null
33+
EXPIRED_TOKEN=$(az ad signed-in-user show --query 'id' -o tsv 2>/dev/null || true)
3034

31-
if ([string]::IsNullOrEmpty($EXPIRED_TOKEN)) {
35+
if [[ -z "$EXPIRED_TOKEN" ]]; then
3236
az login --scope https://graph.microsoft.com/.default -o none
33-
}
37+
fi
3438

35-
if ([string]::IsNullOrEmpty($env:AZURE_SUBSCRIPTION_ID)) {
36-
$ACCOUNT = az account show --query '[id,name]'
37-
Write-Host "You can set the `AZURE_SUBSCRIPTION_ID` environment variable with `azd env set AZURE_SUBSCRIPTION_ID`."
38-
Write-Host $ACCOUNT
39+
if [[ -z "${AZURE_SUBSCRIPTION_ID:-}" ]]; then
40+
ACCOUNT=$(az account show --query '[id,name]')
41+
echo "You can set the \`AZURE_SUBSCRIPTION_ID\` environment variable with \`azd env set AZURE_SUBSCRIPTION_ID\`."
42+
echo $ACCOUNT
3943

40-
$response = Read-Host "Do you want to use the above subscription? (Y/n) "
41-
$response = if ([string]::IsNullOrEmpty($response)) { "Y" } else { $response }
42-
switch ($response) {
43-
{ $_ -match "^[yY](es)?$" } {
44-
# Do nothing
45-
break
46-
}
47-
default {
48-
Write-Host "Listing available subscriptions..."
49-
$SUBSCRIPTIONS = az account list --query 'sort_by([], &name)' --output json
50-
Write-Host "Available subscriptions:"
51-
Write-Host ($SUBSCRIPTIONS | ConvertFrom-Json | ForEach-Object { "{0} {1}" -f $_.name, $_.id } | Format-Table)
52-
$subscription_input = Read-Host "Enter the name or ID of the subscription you want to use: "
53-
$AZURE_SUBSCRIPTION_ID = ($SUBSCRIPTIONS | ConvertFrom-Json | Where-Object { $_.name -eq $subscription_input -or $_.id -eq $subscription_input } | Select-Object -exp id)
54-
if (-not [string]::IsNullOrEmpty($AZURE_SUBSCRIPTION_ID)) {
55-
Write-Host "Setting active subscription to: $AZURE_SUBSCRIPTION_ID"
44+
read -r -p "Do you want to use the above subscription? (Y/n) " response
45+
response=${response:-Y}
46+
case "$response" in
47+
[yY][eE][sS]|[yY])
48+
;;
49+
*)
50+
echo "Listing available subscriptions..."
51+
SUBSCRIPTIONS=$(az account list --query 'sort_by([], &name)' --output json)
52+
echo "Available subscriptions:"
53+
echo "$SUBSCRIPTIONS" | jq -r '.[] | [.name, .id] | @tsv' | column -t -s $'\t'
54+
read -r -p "Enter the name or ID of the subscription you want to use: " subscription_input
55+
AZURE_SUBSCRIPTION_ID=$(echo "$SUBSCRIPTIONS" | jq -r --arg input "$subscription_input" '.[] | select(.name==$input or .id==$input) | .id')
56+
if [[ -n "$AZURE_SUBSCRIPTION_ID" ]]; then
57+
echo "Setting active subscription to: $AZURE_SUBSCRIPTION_ID"
5658
az account set -s $AZURE_SUBSCRIPTION_ID
57-
}
58-
else {
59-
Write-Host "Subscription not found. Please enter a valid subscription name or ID."
59+
else
60+
echo "Subscription not found. Please enter a valid subscription name or ID."
6061
exit 1
61-
}
62-
break
63-
}
64-
}
65-
}
66-
else {
67-
az account set -s $env:AZURE_SUBSCRIPTION_ID
68-
}
62+
fi
63+
;;
64+
*)
65+
echo "Use the \`az account set\` command to set the subscription you'd like to use and re-run this script."
66+
exit 0
67+
;;
68+
esac
69+
else
70+
az account set -s $AZURE_SUBSCRIPTION_ID
71+
fi

infra/hooks/postdeploy.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Runs the post-deploy script after the environment is provisioned
1+
# Runs the post-deploy script after the apps are deployed
22
# It does the following:
33
# 1. Loads the azd environment variables
44
# 2. Logs in to the Azure CLI if not running in a GitHub Action

infra/hooks/postdeploy.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# Runs the post-deploy script after the apps are deployed
4+
# It does the following:
5+
# 1. Loads the azd environment variables
6+
# 2. Logs in to the Azure CLI if not running in a GitHub Action
7+
# 3. Deploys the application to Azure Static Web Apps
8+
9+
set -e
10+
11+
echo "Running post-deploy script..."
12+
13+
# REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
14+
REPOSITORY_ROOT="$(dirname "$(realpath "$0")")/../.."
15+
16+
# Deploy SWA app
17+
"$REPOSITORY_ROOT/infra/hooks/deploy_swa.sh"

infra/hooks/postprovision.sh

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
# Runs the post-provision script after the environment is provisioned
1+
#!/bin/bash
2+
3+
# Runs the post-provision script before the environment is provisioned
24
# It does the following:
35
# 1. Loads the azd environment variables
46
# 2. Logs in to the Azure CLI if not running in a GitHub Action
57
# 3. Updates the application on Microsoft Entra ID
68

7-
Write-Host "Running pre-provision script..."
9+
set -e
10+
11+
echo "Running post-provision script..."
812

9-
# $REPOSITORY_ROOT = git rev-parse --show-toplevel
10-
$REPOSITORY_ROOT = "$(Split-Path $MyInvocation.MyCommand.Path)/../.."
13+
# REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
14+
REPOSITORY_ROOT="$(dirname "$(realpath "$0")")/../.."
1115

12-
# Update the Entra ID application
13-
& "$REPOSITORY_ROOT/infra/hooks/update_app.ps1"
16+
# Update the Entra ID application in Azure
17+
"$REPOSITORY_ROOT/infra/hooks/update_app.sh"

infra/hooks/preprovision.sh

+9-68
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,17 @@
1+
#!/bin/bash
2+
13
# Runs the pre-provision script before the environment is provisioned
24
# It does the following:
35
# 1. Loads the azd environment variables
46
# 2. Logs in to the Azure CLI if not running in a GitHub Action
7+
# 3. Registers the application on Microsoft Entra ID
58

6-
Write-Host "Running pre-provision script..."
7-
8-
# $REPOSITORY_ROOT = git rev-parse --show-toplevel
9-
$REPOSITORY_ROOT = "$(Split-Path $MyInvocation.MyCommand.Path)/../.."
10-
11-
# Load the azd environment variables
12-
& "$REPOSITORY_ROOT/infra/hooks/load_azd_env.ps1" -ShowMessage
13-
14-
if ([string]::IsNullOrEmpty($env:GITHUB_WORKSPACE)) {
15-
# The GITHUB_WORKSPACE is not set, meaning this is not running in a GitHub Action
16-
& "$REPOSITORY_ROOT/infra/hooks/login.ps1"
17-
}
18-
19-
$AZURE_ENV_NAME = $env:AZURE_ENV_NAME
20-
21-
# Run only if GITHUB_WORKSPACE is NOT set - this is NOT running in a GitHub Action workflow
22-
if ([string]::IsNullOrEmpty($env:GITHUB_WORKSPACE)) {
23-
Write-Host "Registering the application in Azure..."
24-
25-
# Create a service principal
26-
$appId = $env:AZURE_CLIENT_ID
27-
if ([string]::IsNullOrEmpty($appId)) {
28-
$appId = az ad app list --display-name "spn-$AZURE_ENV_NAME" --query "[].appId" -o tsv
29-
if ([string]::IsNullOrEmpty($appId)) {
30-
$appId = az ad app create --display-name spn-$AZURE_ENV_NAME --query "appId" -o tsv
31-
$spnId = az ad sp create --id $appId --query "id" -o tsv
32-
}
33-
}
34-
35-
$spnId = az ad sp list --display-name "spn-$AZURE_ENV_NAME" --query "[].id" -o tsv
36-
if ([string]::IsNullOrEmpty($spnId)) {
37-
$spnId = az ad sp create --id $appId --query "id" -o tsv
38-
}
39-
40-
$objectId = az ad app show --id $appId --query "id" -o tsv
41-
42-
# Add client secret to the app
43-
$clientSecret = az ad app credential reset --id $appId --display-name "default" --append
44-
45-
# Add identifier URIs to the app
46-
$identifierUris = @( "api://$appId" )
47-
48-
# Add API scopes to the app
49-
$api = @{
50-
acceptMappedClaims = $null;
51-
knownClientApplications = @();
52-
requestedAccessTokenVersion = $null;
53-
oauth2PermissionScopes = @(
54-
@{
55-
type = "User";
56-
value = "user_impersonation";
57-
adminConsentDisplayName = "Access EasyAuth apps";
58-
adminConsentDescription = "Allows users to access apps using EasyAuth";
59-
isEnabled = $true;
60-
}
61-
)
62-
}
9+
set -e
6310

64-
$payload = @{ $identifierUris = $identifierUris; api = $api } | ConvertTo-Json -Depth 100 -Compress | ConvertTo-Json
11+
echo "Running pre-provision script..."
6512

66-
az rest -m PATCH `
67-
--uri "https://graph.microsoft.com/v1.0/applications/$objectId" `
68-
--headers Content-Type=application/json `
69-
--body $payload
13+
# REPOSITORY_ROOT=$(git rev-parse --show-toplevel)
14+
REPOSITORY_ROOT="$(dirname "$(realpath "$0")")/../.."
7015

71-
# Set the environment variables
72-
azd env set AZURE_PRINCIPAL_ID $appId
73-
azd env set AZURE_PRINCIPAL_SECRET $clientSecret
74-
} else {
75-
Write-Host "Skipping to register the application in Azure..."
76-
}
16+
# Register the Entra ID application in Azure
17+
"$REPOSITORY_ROOT/infra/hooks/register_app.sh"

0 commit comments

Comments
 (0)