協助使用 SWA CLI 建立、設定與部署 Azure Static Web Apps。適用於部署靜態網站至 Azure、設定 SWA 本地開發環境、設定 staticwebapp.config.json、為 SWA 新增 Azure Functions API,或設定 Static Web Apps 的 GitHub Actions CI/CD 功能。
概觀 (Overview)
Azure Static Web Apps (SWA) 可託管靜態前端,並支援可選的 Serverless API 後端。SWA CLI (swa) 提供本地開發模擬與部署功能。
核心功能:
- 包含 API 代理與驗證模擬的本地模擬器
- 框架自動偵測與設定
- 直接部署至 Azure
- 支援資料庫連線
設定檔:
swa-cli.config.json- CLI 設定檔,由swa init建立(切勿手動建立)staticwebapp.config.json- 執行階段設定(路由、驗證、標頭、API 執行階段)- 可手動建立
一般說明 (General Instructions)
安裝 (Installation)
npm install -D @azure/static-web-apps-cli
驗證安裝:npx swa --version
快速入門工作流程 (Quick Start Workflow)
重要提示:請務必使用 swa init 來建立設定檔。切勿手動建立 swa-cli.config.json。
swa init- 必須執行的第一步 - 自動偵測框架並建立swa-cli.config.jsonswa start- 於http://localhost:4280啟動本地模擬器swa login- 驗證並登入 Azureswa deploy- 部署至 Azure
設定檔說明 (Configuration Files)
swa-cli.config.json - 由 swa init 產生,請勿手動建立:
- 執行
swa init透過互動式設定並進行框架偵測 - 執行
swa init --yes以套用自動偵測的預設值 - 初始化後,僅能透過編輯產生的檔案來自訂設定
產生的設定檔範例(僅供參考):
{
"$schema": "https://aka.ms/azure/static-web-apps-cli/schema",
"configurations": {
"app": {
"appLocation": ".",
"apiLocation": "api",
"outputLocation": "dist",
"appBuildCommand": "npm run build",
"run": "npm run dev",
"appDevserverUrl": "http://localhost:3000"
}
}
}
staticwebapp.config.json(基於應用程式原始碼或輸出資料夾)- 此檔案可以手動建立以用於執行階段設定:
{
"navigationFallback": {
"rewrite": "/index.html",
"exclude": ["/images/*", "/css/*"]
},
"routes": [
{ "route": "/api/*", "allowedRoles": ["authenticated"] }
],
"platform": {
"apiRuntime": "node:20"
}
}
命令列參考 (Command-line Reference)
swa login
驗證並登入 Azure 以進行部署。
swa login # 互動式登入
swa login --subscription-id <id> # 指定訂閱 ID
swa login --clear-credentials # 清除快取的憑證
旗標: --subscription-id, -S | --resource-group, -R | --tenant-id, -T | --client-id, -C | --client-secret, -CS | --app-name, -n
swa init
根據現有的前端與(可選的)API 設定新的 SWA 專案。會自動偵測框架。
swa init # 互動式設定
swa init --yes # 接受預設值
swa build
建置前端與/或 API。
swa build # 使用設定檔進行建置
swa build --auto # 自動偵測並建置
swa build myApp # 建置指定的設定組合
旗標: --app-location, -a | --api-location, -i | --output-location, -O | --app-build-command, -A | --api-build-command, -I
swa start
啟動本地開發模擬器。
swa start # 從 outputLocation 提供服務
swa start ./dist # 提供特定資料夾的內容
swa start http://localhost:3000 # 反向代理至開發伺服器
swa start ./dist --api-location ./api # 包含 API 資料夾
swa start http://localhost:3000 --run "npm start" # 自動啟動開發伺服器
常見框架連接埠:
| 框架 | 連接埠 |
|---|---|
| React/Vue/Next.js | 3000 |
| Angular | 4200 |
| Vite | 5173 |
關鍵旗標:
--port, -p- 模擬器連接埠(預設值:4280)--api-location, -i- API 資料夾路徑--api-port, -j- API 連接埠(預設值:7071)--run, -r- 啟動開發伺服器的命令--open, -o- 自動開啟瀏覽器--ssl, -s- 啟用 HTTPS
swa deploy
部署至 Azure Static Web Apps。
swa deploy # 使用設定檔部署
swa deploy ./dist # 部署特定資料夾
swa deploy --env production # 部署至生產環境 (production)
swa deploy --deployment-token <TOKEN> # 使用部署權杖
swa deploy --dry-run # 預覽且不實際部署
取得部署權杖:
- Azure Portal:Static Web App → 概觀 → 管理部署權杖
- CLI:
swa deploy --print-token - 環境變數:
SWA_CLI_DEPLOYMENT_TOKEN
關鍵旗標:
--env- 目標環境(preview或production)--deployment-token, -d- 部署權杖--app-name, -n- Azure SWA 資源名稱
swa db
初始化資料庫連線。
swa db init --database-type mssql
swa db init --database-type postgresql
swa db init --database-type cosmosdb_nosql
常見情境 (Scenarios)
從現有的前端與後端建立 SWA
在執行 swa start 或 swa deploy 前,請務必先執行 swa init。請勿手動建立 swa-cli.config.json。
# 1. 安裝 CLI
npm install -D @azure/static-web-apps-cli
# 2. 初始化 - 必要步驟:建立帶有自動偵測設定的 swa-cli.config.json
npx swa init # 互動模式
# 或
npx swa init --yes # 接受自動偵測的預設值
# 3. 建置應用程式(若需要)
npm run build
# 4. 本地測試(使用 swa-cli.config.json 中的設定)
npx swa start
# 5. 部署
npx swa login
npx swa deploy --env production
新增 Azure Functions 後端
- 建立 API 資料夾:
mkdir api && cd api
func init --worker-runtime node --model V4
func new --name message --template "HTTP trigger"
- 函式範例 (
api/src/functions/message.js):
const { app } = require('@azure/functions');
app.http('message', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: async (request) => {
const name = request.query.get('name') || 'World';
return { jsonBody: { message: `Hello, ${name}!` } };
}
});
- 設定 API 執行階段 於
staticwebapp.config.json:
{
"platform": { "apiRuntime": "node:20" }
}
- 更新 CLI 設定 於
swa-cli.config.json:
{
"configurations": {
"app": { "apiLocation": "api" }
}
}
- 本地測試:
npx swa start ./dist --api-location ./api
# 可在 http://localhost:4280/api/message 存取 API
支援的 API 執行階段: node:18, node:20, node:22, dotnet:8.0, dotnet-isolated:8.0, python:3.10, python:3.11
設定 GitHub Actions 部署
- 建立 SWA 資源 於 Azure Portal 或透過 Azure CLI
- 連結 GitHub 儲存庫 - 工作流程會自動產生,或可手動建立:
.github/workflows/azure-static-web-apps.yml:
name: Azure Static Web Apps CI/CD
on:
push:
branches: [main]
pull_request:
types: [opened, synchronize, reopened, closed]
branches: [main]
jobs:
build_and_deploy:
if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build And Deploy
uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
action: upload
app_location: /
api_location: api
output_location: dist
close_pr:
if: github.event_name == 'pull_request' && github.event.action == 'closed'
runs-on: ubuntu-latest
steps:
- uses: Azure/static-web-apps-deploy@v1
with:
azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN }}
action: close
- 新增 Secret: 將部署權杖複製到儲存庫的 Secret
AZURE_STATIC_WEB_APPS_API_TOKEN
工作流程設定:
app_location- 前端原始碼路徑api_location- API 原始碼路徑output_location- 建置後的輸出資料夾skip_app_build: true- 若已預先建置則跳過app_build_command- 自訂建置命令
疑難排解 (Troubleshooting)
| 問題 | 解決方案 |
|---|---|
| 用戶端路由出現 404 | 在 staticwebapp.config.json 中新增設定 navigationFallback 並帶入 rewrite: "/index.html" |
| API 回傳 404 | 檢查 api 資料夾結構、確認已設定 platform.apiRuntime,並檢查函式匯出內容 |
| 找不到建置輸出 | 確認 output_location 與實際建置輸出的目錄符合 |
| 本地驗證無法運作 | 使用 /.auth/login/<provider> 存取驗證模擬器 UI |
| CORS 錯誤 | 在 /api/* 底下的 API 為同源 (same-origin);外部 API 需要 CORS 標頭 |
| 部署權杖過期 | 請在 Azure Portal → Static Web App → 管理部署權杖 中重新產生 |
| 設定檔未套用 | 確認 staticwebapp.config.json 放置在 app_location 或 output_location 中 |
| 本地 API 超時 | 預設超時時間為 45 秒;請最佳化函式或檢查是否有阻塞發生的呼叫 |
偵錯命令:
swa start --verbose log # 輸出詳細記錄
swa deploy --dry-run # 預覽部署內容
swa --print-config # 顯示解析後的設定內容






