-
Notifications
You must be signed in to change notification settings - Fork 0
165 lines (147 loc) · 5.99 KB
/
Copy pathcd.yml
File metadata and controls
165 lines (147 loc) · 5.99 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# ---------------------------------------------------------------------------
# CD Pipeline — deploy backend + frontend to Azure on push to main
# ---------------------------------------------------------------------------
# Triggered after CI passes on main branch.
#
# Jobs:
# 1. backend — publish .NET 8 app → Azure App Service (zip deploy)
# 2. frontend — build Vite app → Azure App Service (static host w/ SPA fallback)
#
# Why App Service for frontend instead of Static Web Apps?
# The Azure subscription is region-locked (israelcentral / swedencentral / etc.)
# and SWA is not available in those regions. App Service is, so we host the
# built `dist/` on a Linux App Service using the built-in `pm2 serve` command
# with `--spa` so client-side routes fall back to index.html.
#
# Required GitHub Secrets:
# - AZURE_WEBAPP_PUBLISH_PROFILE (backend App Service publish profile XML)
# - AZURE_WEBAPP_FRONTEND_PUBLISH_PROFILE (frontend App Service publish profile XML)
#
# Gating via GitHub Repository Variables (Settings > Secrets and variables >
# Actions > Variables tab):
# - DEPLOY_BACKEND_ENABLED=true enables the backend job
# - DEPLOY_FRONTEND_ENABLED=true enables the frontend job
# If either variable is unset or != "true", that job is skipped so the CD
# run stays green while Azure resources/secrets are still being provisioned.
# ---------------------------------------------------------------------------
name: CD
on:
push:
branches: [main]
concurrency:
group: cd-${{ github.ref }}
cancel-in-progress: false # Don't cancel in-flight deploys
jobs:
backend:
name: Deploy Backend
if: vars.DEPLOY_BACKEND_ENABLED == 'true'
runs-on: ubuntu-latest
defaults:
run:
working-directory: 02-Server
steps:
- uses: actions/checkout@v4
- uses: actions/setup-dotnet@v4
with:
dotnet-version: "8.0.x"
# Publish targeting linux-x64 so the output matches the App Service
# runtime and does NOT include runtimes/win/* (backslash paths in those
# files have historically corrupted wwwroot on Linux App Service).
#
# We let `publish` do its own restore here. The Ubuntu runner ships
# newer .NET SDKs alongside 8.0, and a prior `dotnet restore` without
# an explicit RID produced an assets file that didn't satisfy
# `publish -r linux-x64 --no-restore`.
- name: Publish (linux-x64, framework-dependent)
run: dotnet publish GroundShareAPI.csproj -c Release -r linux-x64 --self-contained false -o ./publish-linux
# Zip on Linux so path separators are forward slashes. Paired with
# WEBSITE_RUN_FROM_PACKAGE=1 on the App Service, the deploy mounts the
# zip read-only and avoids rsync into the locked wwwroot share.
- name: Package
run: |
cd publish-linux
zip -r ../deploy-linux.zip . -x "*.pdb"
- name: Deploy to Azure App Service
uses: azure/webapps-deploy@v3
with:
app-name: app-groundshare-api
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
package: 02-Server/deploy-linux.zip
- name: Health check
run: |
sleep 30
for i in 1 2 3 4 5; do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://app-groundshare-api.azurewebsites.net/api/health || true)
if [ "$STATUS" = "200" ]; then
echo "Health check passed"
exit 0
fi
echo "Attempt $i: status $STATUS, retrying in 15s..."
sleep 15
done
echo "Health check failed after 5 attempts"
exit 1
frontend:
name: Deploy Frontend
if: vars.DEPLOY_FRONTEND_ENABLED == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
cache: "npm"
cache-dependency-path: 03-Client/package-lock.json
- name: Install dependencies
working-directory: 03-Client
run: npm ci
- name: Build
working-directory: 03-Client
run: npm run build
env:
VITE_API_BASE_URL: https://app-groundshare-api.azurewebsites.net/api
# Zip the built dist/ along with a minimal package.json so Azure App
# Service (Linux Node) auto-starts it via `npm start`. The start script
# uses pm2's built-in static server with `--spa` so unmatched routes
# fall back to index.html (required for React Router deep links).
#
# Doing it this way removes the need to set a Startup Command in the
# Portal — the zip is self-describing.
- name: Package dist
working-directory: 03-Client
run: |
cp -r dist deploy-frontend
cat > deploy-frontend/package.json <<'JSON'
{
"name": "groundshare-web",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "pm2 serve . --no-daemon --spa"
}
}
JSON
cd deploy-frontend
zip -r ../deploy-frontend.zip .
- name: Deploy to Azure App Service (frontend)
uses: azure/webapps-deploy@v3
with:
app-name: app-groundshare-web
publish-profile: ${{ secrets.AZURE_WEBAPP_FRONTEND_PUBLISH_PROFILE }}
package: 03-Client/deploy-frontend.zip
# First-time Oryx build + pm2 warmup on a cold Free-tier App Service
# can take 2-3 minutes. Poll for up to ~4 minutes before failing.
- name: Health check
run: |
sleep 60
for i in $(seq 1 12); do
STATUS=$(curl -s -o /dev/null -w "%{http_code}" https://app-groundshare-web-ekfneqfaahaca8ek.israelcentral-01.azurewebsites.net/ || true)
if [ "$STATUS" = "200" ]; then
echo "Health check passed"
exit 0
fi
echo "Attempt $i: status $STATUS, retrying in 15s..."
sleep 15
done
echo "Health check failed"
exit 1