ข้ามไปยังเนื้อหา

Building & Deploying

ใน React การ deploy หมายถึงการรัน npm run build เพื่อสร้าง static folder dist/ แล้วนำขึ้น Vercel, Netlify หรือ S3 Flutter มีสาม build targets — Android (APK/AAB), iOS (IPA) และ web — แต่ละอันมีคำสั่งและข้อกำหนด signing ของตัวเอง logic ของ CI pipeline แทบจะเหมือนกับที่คุณเขียนสำหรับ React เพียงแค่เปลี่ยน action steps

Terminal window
# Android
flutter build apk # Debug APK (for testing)
flutter build apk --release # Release APK (signed)
flutter build appbundle # Android App Bundle (Play Store preferred)
# iOS (requires macOS + Xcode)
flutter build ipa # iOS Archive for App Store / TestFlight
flutter build ipa --export-method ad-hoc # Ad-hoc distribution
# Web
flutter build web # Output to build/web/
flutter build web --release # Minified production build
# Desktop
flutter build macos
flutter build windows
flutter build linux

web build จะ output ไปที่ build/web/ — เป็น static folder เหมือนกับ dist/ ของ Vite คุณ deploy ไปที่ CDN ใดก็ได้โดยไม่ต้องแก้ไขอะไร

Android release builds ต้องมีการ sign ด้วย keystore เปรียบเหมือน code-signing ในการพัฒนา app native — builds ที่ไม่ได้ sign จะไม่สามารถ distribute ผ่าน Play Store ได้

สร้าง keystore ด้วย Java keytool:

Terminal window
keytool -genkey -v -keystore ~/upload-keystore.jks -keyalg RSA -keysize 2048 -validity 10000 -alias upload

จากนั้นสร้าง android/key.properties เพื่อ reference keystore:

storePassword=<your-store-password>
keyPassword=<your-key-password>
keyAlias=upload
storeFile=<path-to>/upload-keystore.jks

สุดท้าย reference key.properties ใน android/app/build.gradle เพื่อให้ release build ดึง signing config มาโดยอัตโนมัติ

ปฏิบัติกับ key.properties และ .jks เหมือนกับ .env ไฟล์ที่มี secrets — อย่า commit ลง git เด็ดขาด เพิ่มทั้งคู่ลงใน .gitignore

iOS signing ต้องการ Apple Developer account, provisioning profile และ signing certificate ที่จัดการผ่าน Xcode flutter build ipa สร้าง .xcarchive แล้ว export เป็น .ipa วิธีที่ง่ายที่สุดคือเปิด “Automatically manage signing” ใน Xcode ซึ่งจัดการ profile และ certificate ให้โดยอัตโนมัติ

flutter build ipa รับ flag --export-method เพื่อควบคุม distribution channel: app-store สำหรับ App Store/TestFlight, ad-hoc สำหรับ direct device distribution

Terminal window
# Firebase Hosting
firebase deploy --only hosting
# Or drag-drop build/web/ to Netlify's deploy UI
# Or push to Vercel via the Vercel CLI

output ของ build/web/ คือ static site ปกติ — index.html, compiled JavaScript และ assets ที่ประกาศใน pubspec.yaml ไม่ต้องมี server

React CI pipeline ติดตั้ง Node, รัน npm ci, lint, test และ build Flutter CI pipeline ทำเช่นเดียวกันโดยใช้ subosito/flutter-action แทน actions/setup-node

React / GitHub Actions
name: React CI
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: actions/setup-node@v7
with:
node-version: '24'
- run: npm ci
- run: npm run lint
- run: npm test -- --watchAll=false
- run: npm run build
Flutter / GitHub Actions
name: Flutter CI
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: subosito/flutter-action@v2
with:
flutter-version: '3.47.0'
channel: 'stable'
- run: flutter pub get
- run: flutter analyze
- run: flutter test --coverage
- run: flutter build web --release

ตัวอย่าง workflow ที่ครบถ้วนพร้อม branch triggers สำหรับทั้ง main และ feature branches:

name: Flutter CI
on:
push:
branches: [main, dev]
pull_request:
branches: [main]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: subosito/flutter-action@v2
with:
flutter-version: '3.47.0'
channel: 'stable'
- name: Install dependencies
run: flutter pub get
- name: Analyze
run: flutter analyze
- name: Run tests
run: flutter test --coverage
- name: Build web
run: flutter build web --release
คำสั่งใดที่ build Flutter app สำหรับ web?
Flutter equivalent ของ actions/setup-node ใน GitHub Actions คืออะไร?
flutter build appbundle สร้างอะไร และทำไมจึงควรใช้แทน APK สำหรับ Play Store?
ที่ใดที่คุณไม่ควร commit Android keystore หรือ key.properties ไป?