This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy Flutter Web to GitHub Pages | |
| on: | |
| push: | |
| branches: [ main ] | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pages: write | |
| id-token: write | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Setup Flutter with Cache | |
| uses: subosito/flutter-action@v2 | |
| with: | |
| channel: 'stable' | |
| cache: true | |
| cache-key: flutter-stable-${{ hashFiles('pubspec.lock') }} | |
| - name: Cache Flutter dependencies | |
| uses: actions/cache@v4 | |
| with: | |
| path: | | |
| ${{ runner.temp }}/flutter_cache | |
| ~/.pub-cache | |
| key: ${{ runner.os }}-flutter-${{ hashFiles('pubspec.lock') }} | |
| restore-keys: | | |
| ${{ runner.os }}-flutter- | |
| - name: Enable Flutter Web | |
| run: flutter config --enable-web | |
| - name: Install dependencies | |
| run: | | |
| flutter pub get | |
| - name: Clean build | |
| run: flutter clean | |
| - name: Build Flutter Web | |
| run: | | |
| # Flutter 3.x 会自动选择最佳的渲染器 | |
| # 如需强制使用 HTML renderer,可通过环境变量设置 | |
| flutter build web \ | |
| --release \ | |
| --base-href /network-calculator/ \ | |
| --dart-define=BUILD_MODE=production \ | |
| --dart-define=APP_VERSION=1.6.0 | |
| - name: Prepare deployment files | |
| run: | | |
| cd build/web | |
| # 1. 防止 Jekyll 处理 | |
| echo "" > .nojekyll | |
| # 2. 创建 404.html 支持客户端路由 | |
| cp index.html 404.html | |
| # 3. 添加部署信息文件 | |
| echo "Network Calculator - Flutter Web" > DEPLOY.txt | |
| echo "Version: 1.6.0" >> DEPLOY.txt | |
| echo "Build Date: $(date -u +'%Y-%m-%d %H:%M:%S UTC')" >> DEPLOY.txt | |
| echo "Commit SHA: ${{ github.sha }}" >> DEPLOY.txt | |
| echo "GitHub Pages URL: https://hoochanlon.github.io/network-calculator/" >> DEPLOY.txt | |
| # 4. 显示构建结果 | |
| echo "=== Build completed successfully ===" | |
| echo "Total size: $(du -sh . | cut -f1)" | |
| echo "File count: $(find . -type f | wc -l)" | |
| # 5. 检查关键文件 | |
| echo "=== Checking key files ===" | |
| ls -la index.html flutter.js main.dart.js | |
| # 6. 修复可能的路径问题 | |
| if [ -f index.html ]; then | |
| echo "Index.html exists with size: $(wc -l < index.html) lines" | |
| fi | |
| - name: Upload GitHub Pages artifact | |
| uses: actions/upload-pages-artifact@v3 # 使用 v3 版本 | |
| with: | |
| path: ./build/web | |
| deploy: | |
| environment: | |
| name: github-pages | |
| url: ${{ steps.deployment.outputs.page_url }} | |
| runs-on: ubuntu-latest | |
| needs: build | |
| permissions: | |
| pages: write | |
| id-token: write | |
| steps: | |
| - name: Deploy to GitHub Pages | |
| id: deployment | |
| uses: actions/deploy-pages@v4 # 使用 v4 版本 |