如何在GitLab共享运行器上构建Flutter Windows桌面应用?
Hey there, I totally get wanting the easiest path here—since you're only familiar with the basics of Docker and GitLab CI, let's stick to a solution that avoids complex setup and leverages GitLab's existing Windows shared runners. Here's exactly what to do:
Core Idea
Since Flutter requires a matching host OS to build native desktop apps (hence that build windows only supported on Windows hosts error), we'll use GitLab's Windows shared runners and automate Flutter installation directly in the CI script—no need to manually configure the runner or mess with cumbersome Windows Docker images.
Step-by-Step .gitlab-ci.yml Setup
Create or update your .gitlab-ci.yml file with this content. I'll break down each part so you know what's happening:
stages: - build build_windows: stage: build tags: - windows # Use GitLab's official Windows shared runner; adjust tag if using self-hosted runners before_script: # Automatically install Flutter via PowerShell - | $FLUTTER_VERSION="3.13.0" # Replace with your app's exact Flutter version $FLUTTER_ZIP="flutter_windows_$FLUTTER_VERSION-stable.zip" $FLUTTER_URL="https://storage.googleapis.com/flutter_infra_release/releases/stable/windows/$FLUTTER_ZIP" # Download & extract Flutter if not already cached if (-not (Test-Path "C:\flutter")) { Invoke-WebRequest -Uri $FLUTTER_URL -OutFile $FLUTTER_ZIP Expand-Archive -Path $FLUTTER_ZIP -DestinationPath "C:\" } # Add Flutter to system PATH and set required env vars $env:PATH += ";C:\flutter\bin" $env:FLUTTER_ROOT = "C:\flutter" # Validate setup (skip Android licenses since we're building Windows only) flutter doctor -v --disable-telemetry script: # Install app dependencies flutter pub get # Build optimized Windows release flutter build windows --release artifacts: paths: - build/windows/runner/release/ # Upload final executable and assets expire_in: 1 week # Adjust retention period as needed
Key Optimizations & Notes
- Speed Up Future Runs: Add a cache to avoid re-downloading Flutter every time. Just insert this under the
build_windowsjob:cache: key: flutter-windows-cache paths: - C:\flutter\ - Runner Tags: The
windowstag works for GitLab's cloud shared runners. If using a self-hosted instance, confirm the correct tag for your Windows runners. - Linux Expansion Later: When you're ready to add Linux builds, just add a separate job that uses your original Docker plan. Example:
build_linux: stage: build image: cirrusci/flutter:stable tags: - linux script: - flutter pub get - flutter build linux --release artifacts: paths: - build/linux/x64/release/bundle/
Why This Is the Lowest-Friction Approach
- No manual runner maintenance: Everything from Flutter installation to build runs automatically in the CI script.
- Avoids Windows Docker headaches: Windows Docker images are heavier, require extra system setup (like Hyper-V), and add unnecessary complexity for your use case.
- Easy to extend: Adding Linux support later is straightforward since you can reuse the Docker approach you initially planned.
内容的提问来源于stack exchange,提问作者David Arppe

