无法通过GitHub Actions将Flutter应用发布至Azure App Service
问题描述
我按照教程将Flutter应用部署至Azure App Service,部署中心自动生成了以下GitHub Actions工作流:
name: Build and deploy Node.js app to Azure Web App - flutter-test on: push: branches: - master workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Node.js version uses: actions/setup-node@v1 with: node-version: '18.x' - name: npm install, build, and test run: | npm install npm run build --if-present npm run test --if-present - name: Upload artifact for deployment job uses: actions/upload-artifact@v2 with: name: node-app path: . deploy: runs-on: ubuntu-latest needs: build environment: name: 'Production' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} steps: - name: Download artifact from build job uses: actions/download-artifact@v2 with: name: node-app - name: 'Deploy to Azure Web App' id: deploy-to-webapp uses: azure/webapps-deploy@v2 with: app-name: 'flutter-test' slot-name: 'Production' publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_9F371C7309EE423496F3C77A4D2B4CAA }} package: .
工作流执行成功,但访问App Service URL时显示默认Node.js欢迎页(如下所示),而非预期的默认Flutter启动项目:
解决方案
出现该问题的核心原因是:自动生成的Node.js工作流未执行Flutter Web构建步骤,直接部署了项目根目录文件,导致Azure App Service加载默认Node.js页面而非Flutter产物。
1. 重构Build作业流程
替换原Node.js构建步骤,改为Flutter Web专属构建流程,生成正确的Web产物:
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 # 配置稳定版Flutter环境 - name: Set up Flutter uses: subosito/flutter-action@v2 with: flutter-version: 'stable' # 安装Flutter项目依赖 - name: Install Flutter dependencies run: flutter pub get # 构建生产环境Flutter Web产物,输出至build/web目录 - name: Build Flutter Web run: flutter build web --release # 仅上传构建好的Web产物,减少冗余 - name: Upload build artifact uses: actions/upload-artifact@v3 with: name: flutter-web-build path: build/web
2. 调整Deploy作业的部署路径
修改部署阶段,确保将Flutter Web产物部署到Azure App Service:
deploy: runs-on: ubuntu-latest needs: build environment: name: 'Production' url: ${{ steps.deploy-to-webapp.outputs.webapp-url }} steps: - name: Download Flutter build artifact uses: actions/download-artifact@v3 with: name: flutter-web-build - name: Deploy to Azure Web App id: deploy-to-webapp uses: azure/webapps-deploy@v2 with: app-name: 'flutter-test' slot-name: 'Production' publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_9F371C7309EE423496F3C77A4D2B4CAA }} package: .
3. 配置SPA路由支持(可选)
若部署后Flutter路由功能异常,可在Azure门户的App Service 配置 > 常规设置中,设置启动命令为:
pm2 serve /home/site/wwwroot --no-daemon --spa
该命令启用SPA模式,确保所有路由请求指向index.html,适配Flutter Web单页应用特性。
完成修改后,重新推送代码触发GitHub Actions工作流,部署成功后即可访问默认Flutter启动项目。
内容的提问来源于stack exchange,提问作者Code Ratchet
相关产品推荐
相关产品推荐

