Skip to content

Commit 8024d23

Browse files
committed
infra: update setup script
1 parent d1f25b8 commit 8024d23

File tree

4 files changed

+115
-53
lines changed

4 files changed

+115
-53
lines changed

.azure/setup.sh

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
# Usage: ./setup.sh <project_name> [environment_name] [location] [options]
44
# Setup the current GitHub repo for deploying on Azure.
55
##############################################################################
6-
# v1.0.2 | dependencies: Azure CLI, GitHub CLI, jq
6+
# v1.1.2 | dependencies: Azure CLI, GitHub CLI, jq
77
##############################################################################
88

9-
set -e
10-
cd $(dirname ${BASH_SOURCE[0]})
9+
set -euo pipefail
10+
cd "$(dirname "${BASH_SOURCE[0]}")"
1111
if [[ -f ".settings" ]]; then
1212
source .settings
1313
fi
@@ -21,12 +21,14 @@ showUsage() {
2121
echo " -s, --skip-login Skip Azure and GitHub login steps"
2222
echo " -t, --terminate Remove current setup and delete deployed resources"
2323
echo " -l, --ci-login Only perform Azure CLI login using environment credentials"
24+
echo " -c, --use-code Use device code login flow instead of browser"
2425
echo
2526
}
2627

2728
skip_login=false
2829
terminate=false
2930
ci_login=false
31+
use_code=false
3032
args=()
3133

3234
while [[ $# -gt 0 ]]; do
@@ -43,11 +45,15 @@ while [[ $# -gt 0 ]]; do
4345
ci_login=true
4446
shift
4547
;;
48+
-c|--use-code)
49+
use_code=true
50+
shift
51+
;;
4652
--help)
4753
showUsage
4854
exit 0
4955
;;
50-
-*|--*)
56+
--*|-*)
5157
showUsage
5258
echo "Unknown option $1"
5359
exit 1
@@ -73,17 +79,17 @@ if ! command -v az &> /dev/null; then
7379
exit 1
7480
fi
7581

76-
if [[ "$ci_login" = true ]]; then
82+
if [[ "$ci_login" == true ]]; then
7783
echo "Logging in to Azure using \$AZURE_CREDENTIALS..."
78-
if [[ -z "${AZURE_CREDENTIALS}" ]]; then
84+
if [[ -z "${AZURE_CREDENTIALS:-}" ]]; then
7985
echo "Azure credentials not found."
8086
echo "Please run .azure/setup.sh locally to setup your deployment."
8187
exit 1
8288
fi
83-
client_id="$(echo $AZURE_CREDENTIALS | jq -r .clientId)"
84-
client_secret="$(echo $AZURE_CREDENTIALS | jq -r .clientSecret)"
85-
subscription_id="$(echo $AZURE_CREDENTIALS | jq -r .subscriptionId)"
86-
tenant_id="$(echo $AZURE_CREDENTIALS | jq -r .tenantId)"
89+
client_id="$(echo "$AZURE_CREDENTIALS" | jq -r .clientId)"
90+
client_secret="$(echo "$AZURE_CREDENTIALS" | jq -r .clientSecret)"
91+
subscription_id="$(echo "$AZURE_CREDENTIALS" | jq -r .subscriptionId)"
92+
tenant_id="$(echo "$AZURE_CREDENTIALS" | jq -r .tenantId)"
8793
az login \
8894
--service-principal \
8995
--username "${client_id}" \
@@ -106,20 +112,56 @@ if [[ -z "$project_name" ]]; then
106112
exit 1
107113
fi
108114

109-
if [[ "$skip_login" = false ]]; then
115+
if [[ "$skip_login" == false ]]; then
116+
az_login_options=""
117+
if [[ "$use_code" == true || "${CODESPACES:-}" == true ]]; then
118+
az_login_options="--use-device-code"
119+
fi
120+
110121
echo "Logging in to Azure..."
111-
az login
112-
echo "Logging in to GitHub..."
113-
gh auth login
122+
az login --query "[].{name:name,id:id}" $az_login_options
123+
echo "Listed above are your available subscriptions."
124+
echo
125+
126+
echo "Currently selected subscription is:"
127+
az account show \
128+
--query "{name:name,id:id}" \
129+
--output tsv
130+
echo
131+
read -r -n 1 -p "Is your current subscription correct? (Y/n) " is_correct
132+
echo
133+
134+
if [[ "$is_correct" == "n" ]]; then
135+
read -r -p "Enter your subscription name or ID: " az_sub
136+
az account set \
137+
--subscription "$az_sub" \
138+
--query "{name:name,id:id}" \
139+
--output tsv
140+
echo "Azure default subscription has been updated successfully."
141+
fi
142+
143+
if [[ -z "${GITHUB_TOKEN:-}" || "${CODESPACES:-}" == true ]]; then
144+
unset GITHUB_TOKEN
145+
echo "Logging in to GitHub..."
146+
gh auth login
147+
148+
if [[ -n "${GITHUB_REPOSITORY:-}" ]]; then
149+
echo "Setting default GitHub repository to '$GITHUB_REPOSITORY'..."
150+
gh repo set-default "$GITHUB_REPOSITORY"
151+
fi
152+
else
153+
echo "GITHUB_TOKEN is already set, skipping GitHub login."
154+
fi
155+
114156
echo "Login successful."
115157
fi
116158

117-
if [[ "$terminate" = true ]]; then
159+
if [[ "$terminate" == true ]]; then
118160
echo "Deleting current setup..."
119-
.azure/infra.sh down ${project_name} ${environment} ${location}
161+
.azure/infra.sh down "${project_name}" "${environment}" "${location}"
120162
echo "Retrieving GitHub repository URL..."
121163
remote_repo=$(git config --get remote.origin.url)
122-
gh secret delete AZURE_CREDENTIALS -R $remote_repo
164+
gh secret delete AZURE_CREDENTIALS -R "$remote_repo"
123165
echo "Setup deleted."
124166
else
125167
echo "Retrieving Azure subscription..."
@@ -141,8 +183,12 @@ else
141183
echo "Retrieving GitHub repository URL..."
142184
remote_repo=$(git config --get remote.origin.url)
143185
echo "Setting up GitHub repository secrets..."
144-
gh secret set AZURE_CREDENTIALS -b"$service_principal" -R $remote_repo
145-
echo "Triggering Azure deployment..."
146-
gh workflow run deploy.yml
186+
gh secret set AZURE_CREDENTIALS -b"$service_principal" -R "$remote_repo"
187+
188+
if [[ -f ".github/workflows/deploy.yml" ]]; then
189+
echo "Found deploy.yml workflow, triggering deployment..."
190+
gh workflow run deploy.yml
191+
fi
192+
147193
echo "Setup success!"
148194
fi

.github/workflows/template.yml

Whitespace-only changes.

TODO

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
# TODO solution
2-
- [x] Feedback npm CI with docker + npm workspaces
3-
- [x] CosmosDB
4-
- [ ] Add correlation ID in logs
5-
- [ ] Bicep
6-
- [x] update setup script
7-
- [ ] update build script
8-
- [ ] update deploy script
9-
- [ ] GitHub Actions deployment
10-
- [ ] devcontainer with vscode extensions
11-
- [ ] secrets bash array with newlines -> nope?
2+
- [ ] provide zip of each service built by CI, in packages
3+
- [ ] add in description mention of 3 frameworks
4+
- [ ] URL -> host in outputs
5+
6+
- [ ] Add correlation ID in logs (Dapr?)
7+
8+
## Nice to have
9+
- [ ] devcontainer with vscode extensions (what's missing? Db explorer?)
10+
- [c] cosmosdb emulator in compose file?
11+
- [ ] secrets bash array with newlines -> nope? ask Chris
1212
- [ ] env file uppercase
1313
- [ ] URLs and DB CS in format <name>_URL
1414
- [ ] bash strict mode
1515

1616
# Workshop
17-
- [x] Simple website to push/get requests with auth [SWA]
18-
- [x] CosmosDB to store results
1917
<!-- - [ ] Event Grid to pass events -->
2018
<!-- - [ ] Azure Function to process events and store in DB -->
21-
- [x] Microservices problems
22-
* 3 services:
23-
- API gateway - N Dice Rolls
24-
- Dice Rolls API - DB / APIs: new roll dice / get last N rolls - DB
25-
- Settings API - Store User<>Dice Faces preferences - DB
26-
<!-- * [ ] Service to service communication/auth -->
27-
* Service discovery
28-
* Load balancing / scaling
29-
* Monitoring
19+
<!-- * [ ] Service to service communication/auth -->
20+
* Service discovery?
3021
* Logging/Tracing
3122

32-
- [x] website
33-
* [x] Login
34-
* [x] Set dice type preference
35-
* [x] Roll N dices and show results / time
36-
* [x] Show last N rolls
23+
- [ ] website nice CSS?
3724

3825
# Plan
3926

docs/workshop.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ This will start the creation of a dev container environment, which is a pre-conf
122122

123123
<div class="info" data-title="note">
124124

125-
> You don't have to worry about Codespaces usage cost for this workshop, as it's free for forks of our template repository. For personal usage, Codespaces includes up to 60 hours of free usage per month for all GitHub users, see [the pricing details here](https://github.com/features/codespaces).
125+
> Codespaces includes up to 60 hours of free usage per month for all GitHub users, see [the pricing details here](https://github.com/features/codespaces).
126126
127127
</div>
128128

@@ -1517,8 +1517,15 @@ Create a new file named `staticwebapp.config.json` in the `packages/website/publ
15171517
}
15181518
```
15191519

1520-
TODO: complete
1521-
TODO: raise issue about the rewrite
1520+
We're defining two things here: the routing rules, and the navigation fallback. We add a routing rules to only allow access to our API to authenticated users. SWA provides two built-in roles: `authenticated` and `anonymous`. We use the `authenticated` role here, because we want to make sure that only authenticated users can access our API. If a user tries to access our API without being authenticated, it will return a `401 Unauthorized` response.
1521+
1522+
<div class="info" data-title="info">
1523+
1524+
> Routing options also allows to define redirections, rewriting, caching headers, and more. See the [documentation](https://learn.microsoft.com/azure/static-web-apps/configuration) for more details.
1525+
1526+
</div>
1527+
1528+
The navigation fallback is used to redirect all requests to unknown resources to the `index.html` file. This is mandatory for single-page applications, though we're only using it here to make sure that you always end up on the index page.
15221529

15231530
### Setting up the SWA CLI
15241531

@@ -1565,8 +1572,6 @@ Then, we'll add a new `start` script to our `package.json` file to start the SWA
15651572
},
15661573
```
15671574

1568-
TODO: cli fix: vite + api dev server url question
1569-
15701575
### Testing our application
15711576

15721577
We're now ready to test our whole application locally. To do so, we need to start in parallel the SWA CLI and the Docker compose environment with our services.
@@ -1618,7 +1623,7 @@ You should see the login page of our application:
16181623
16191624
If you select **Login**, you'll be redirected to the SWA CLI authentication emulator login page:
16201625

1621-
![Screenshot of the SWA CLI login page](./assets/swa-login.png)
1626+
![Screenshot of the SWA CLI login page](./assets/swa-cli-auth.png)
16221627

16231628
This is a fake login page made for local testing, where you can enter various parameters to simulate different users. Fill in any **Username** and select **Login**.
16241629

@@ -1633,7 +1638,31 @@ After you're done testing, you can stop the application by pressing `Ctrl+C` in
16331638
---
16341639
16351640
## Azure setup
1636-
- Setup azure account: script: explain what it does
1641+
1642+
Azure is a cloud platform that provides a wide range of services to build, deploy, and manage applications. We'll use various Azure services in this workshop to host our application.
1643+
1644+
First, you need to make sure you have an Azure account. If you don't have one, you can create a free account including Azure credits on the [Azure website](https://azure.microsoft.com/free/).
1645+
1646+
<div class="important" data-title="important">
1647+
1648+
> If you're following this workshop in-person at SnowCamp, you can use the following link to get a 50$ Azure Pass credit: [redeem your Azure Pass](https://azcheck.in/sno230125)
1649+
1650+
</div>
1651+
1652+
Once you have your Azure account, open a terminal at the root of the project and run:
1653+
1654+
```bash
1655+
.azure/setup.sh
1656+
```
1657+
1658+
This script uses the [Azure CLI](https://learn.microsoft.com/cli/azure) and [GitHub CLI](https://cli.github.com/) to do the following:
1659+
- Login into your Azure account
1660+
- Select a subscription to use
1661+
- Create a [service principal](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal), a token that will be used to create or update resources in Azure
1662+
- Login into your GitHub account
1663+
- Add the `AZURE_CREDENTIALS` secret to your GitHub repository, with your the service principal token
1664+
1665+
16371666

16381667
### Introducing Azure services
16391668
- Explain SWA / ACR / ACA / CosmosDB / Registry / Log analytics / Azure Monitor

0 commit comments

Comments
 (0)