You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过GitHub Actions部署Angular应用到Azure静态Web应用遇Node版本问题

解决Azure静态Web Apps部署时Node版本不匹配的问题

你的问题核心在于Azure/static-web-apps-deploy@v1旧版部署动作会忽略你通过actions/setup-node配置的Node环境,默认使用内置的Node 16.20.2,和Angular 17要求的Node 18.13+版本冲突。

修正方案

升级部署动作为最新稳定版@v4,并明确指定与本地一致的Node版本(推荐用你本地的Node 20):

name: Azure Static Web Apps CI/CD

on:
  push:
    branches:
      - master
  pull_request:
    types: [opened, synchronize, reopened, closed]
    branches:
      - master

jobs:
  build_and_deploy_job:
    if: github.event_name == 'push' || (github.event_name == 'pull_request' && github.event.action != 'closed')
    runs-on: ubuntu-latest
    name: Build and Deploy Job
    steps:
      - uses: actions/checkout@v4
        with:
          sub-modules: true
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
          cache: 'npm' # 可选:缓存npm依赖,加快构建速度
      # 手动执行依赖安装与构建,方便排查问题(可选)
      - name: Install dependencies
        run: npm ci
      - name: Build Angular app
        run: npm run build --prod
      # 使用新版部署动作并指定Node版本
      - name: Build And Deploy
        id: builddeploy
        uses: Azure/static-web-apps-deploy@v4
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_VICTORIOUS_DESERT_030B8CE03 }}
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          action: "upload"
          app_location: "/"
          api_location: "api"
          output_location: "dist/sun-club"
          node_version: "20"

  close_pull_request_job:
    if: github.event_name == 'pull_request' && github.event.action == 'closed'
    runs-on: ubuntu-latest
    name: Close Pull Request Job
    steps:
      - name: Close Pull Request
        id: closepullrequest
        uses: Azure/static-web-apps-deploy@v4
        with:
          azure_static_web_apps_api_token: ${{ secrets.AZURE_STATIC_WEB_APPS_API_TOKEN_VICTORIOUS_DESERT_030B8CE03 }}
          action: "close"

关键改动说明

  1. 升级部署动作:Azure/static-web-apps-deploy@v4支持自定义Node版本,不会强制使用旧版内置环境。
  2. 统一Node版本:在setup-node和部署步骤的node_version参数都设置为20,和本地环境保持一致,避免版本差异问题。
  3. 手动构建步骤(可选):将依赖安装、构建过程单独拆分,便于排查构建阶段的错误,同时利用缓存提升构建效率。

内容的提问来源于stack exchange,提问作者Radu Alexandru

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 11:46:14