通过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"
关键改动说明
- 升级部署动作:
Azure/static-web-apps-deploy@v4支持自定义Node版本,不会强制使用旧版内置环境。 - 统一Node版本:在
setup-node和部署步骤的node_version参数都设置为20,和本地环境保持一致,避免版本差异问题。 - 手动构建步骤(可选):将依赖安装、构建过程单独拆分,便于排查构建阶段的错误,同时利用缓存提升构建效率。
内容的提问来源于stack exchange,提问作者Radu Alexandru
相关产品推荐
相关产品推荐

