使用 Azure DevOps 發佈 Azure Static Web Apps 新發現一個專門 for 前端的服務 Azure Static Web Apps
這邊以 Vue 筆記一下 Azure DevOps CI/CD 到該服務的流程
VUE
trigger:
- master
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
submodules: true
- task: AzureStaticWebApp@0
inputs:
app_location: '/'
output_location: 'dist'
app_build_command: 'npm run build'
# routes_location: 'public/json'
azure_static_web_apps_api_token: $(deployment_token)
Blazor
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
trigger:
- develop
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
submodules: true
- task: AzureStaticWebApp@0
inputs:
app_location: '/src/MyProject.Blazor'
output_location: 'wwwroot'
azure_static_web_apps_api_token: $(deployment_token)
Blazor 專案根目錄放 staticwebapp.config.json
{
"navigationFallback": {
"rewrite": "/index.html"
}
}
Angualr
# Node.js with Angular
# Build a Node.js project that uses Angular.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/javascript
trigger:
- master
pool:
vmImage: ubuntu-latest
steps:
- task: AzureStaticWebApp@0
inputs:
app_location: '/'
output_location: 'dist/MyApp'
azure_static_web_apps_api_token: '$(deployment_token)'
routes_location: '/'
注意: output_location: 'dist/MyApp'
2021/10/8
參照: 教學課程:使用 Azure DevOps 發佈 Azure 靜態 Web Apps | Microsoft Docs
Step 1 建立靜態 Web 應用程式 首先到 Azure Portal 建立一個新的 靜態 Web 應用程式
Git 來源選 Other 稍後自行設定使用 DevOps 部屬
建立完後 概觀 (上方) 管理部署權杖 複製 部署權杖 稍後會用到
Step 2 準備 Vue 專案 Git Repo 建立自己的前端應用,我這邊是用 Vue 建了一個預設專案
然後到 Devops 建立一個測試專案的 Git Repo
把手上的前端原始碼 push 上去
Step 3 建立 DevOps CI/CD 在 Repo 右上角可以按 set build pipeline
也可以到 pipeline 按新增
然後搜尋 static 會找到一個範本
輸入以下參數
app_location: '/'
output_location: 'dist'
azure_static_web_apps_api_token: $(deployment_token)
會自動生成以下內容
azure-pipelines.yml
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
submodules: true
- task: AzureStaticWebApp@0
inputs:
app_location: '/'
output_location: 'dist'
azure_static_web_apps_api_token: $(deployment_token)
這邊必須右上角設定環境變數 deployment_token 把 Azure Static Web Apps 的發布權杖密碼設定進去
說明一下參數
app_location => 程式原始碼所在目錄 ex: /app
output_location => 編譯完後的靜態網頁相對於app的路徑 ex: dist 例如: app_location + output_location = /app/dist 也就是最後部屬時會把這路徑裡面的東西拿去打包 (ZIP) 並上傳
azure_static_web_apps_api_token 就 Azure 那邊應用的發布權杖 因為這邊實際上會在 git repo 建立 yml 檔案,所以為了安全性建議使用環境變數 $(deployment_token)
App Build Command => 預設是 npm run build, 有需要可以使用自己的指令
Routes Location => routes.json 的位置,如果不是用 hash route 需要設定轉址, 則需要建立並指定該設定檔(routes.json)位置, 參照 Configure Azure Static Web Apps | Microsoft Learn routes.json
{
"routes": [
{
"route": "/*",
"serve": "/index.html",
"statusCode": 200
}
]
}
trigger:
- main
pool:
vmImage: ubuntu-latest
steps:
- task: NodeTool@0
inputs:
versionSpec: '10.x'
displayName: 'Install Node.js'
- script: |
npm install
npm run build
displayName: 'npm install and build'
- task: AzureStaticWebApp@0
inputs:
app_location: '/dist'
skip_app_build: true
azure_static_web_apps_api_token: '$(deployment_token)'
存檔之後就會觸發 CI/CD, 可以到 azure 提供的 app url 看到自己的網站,之後只要 psuh 到 main 就會自動 build & publish
部署後檢查 開啟部署網址並直接重整一個前端子路徑。首頁正常、子路徑回 404 時,先確認發佈目錄裡有隨成品部署的 staticwebapp.config.json ,再檢查路由 fallback。若連首頁也沒有本次變更,回到 Pipeline 對照建置輸出與部署工作的輸入目錄。
Azure Static Web Apps 的路由與設定檔
01 December 2026
關於筆記整理者 Jakeuj