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
Build commands
หัวข้อที่มีชื่อว่า “Build commands”# Androidflutter build apk # Debug APK (สำหรับทดสอบ)flutter build apk --release # Release APK (signed)flutter build appbundle # Android App Bundle (แนะนำสำหรับ Play Store)
# iOS (ต้องใช้ macOS + Xcode)flutter build ipa # iOS Archive สำหรับ App Store / TestFlightflutter build ipa --export-method ad-hoc # Ad-hoc distribution
# Webflutter build web # Output ไปที่ build/web/flutter build web --release # Minified production build
# Desktopflutter build macosflutter build windowsflutter build linuxweb build จะ output ไปที่ build/web/ — เป็น static folder เหมือนกับ dist/ ของ Vite คุณ deploy ไปที่ CDN ใดก็ได้โดยไม่ต้องแก้ไขอะไร
Release signing — Android
หัวข้อที่มีชื่อว่า “Release signing — Android”Android release builds ต้องมีการ sign ด้วย keystore เปรียบเหมือน code-signing ในการพัฒนา app native — builds ที่ไม่ได้ sign จะไม่สามารถ distribute ผ่าน Play Store ได้
สร้าง keystore ด้วย Java keytool:
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=uploadstoreFile=<path-to>/upload-keystore.jksสุดท้าย reference key.properties ใน android/app/build.gradle เพื่อให้ release build ดึง signing config มาโดยอัตโนมัติ
ปฏิบัติกับ key.properties และ .jks เหมือนกับ .env ไฟล์ที่มี secrets — อย่า commit ลง git เด็ดขาด เพิ่มทั้งคู่ลงใน .gitignore
Release signing — iOS
หัวข้อที่มีชื่อว่า “Release signing — iOS”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
Web deploy
หัวข้อที่มีชื่อว่า “Web deploy”# Buildflutter build web --release
# Deploy ไปที่ Firebase Hostingfirebase deploy --only hosting
# หรือลาก build/web/ ไปยัง Netlify deploy UI# หรือ push ไปยัง Vercel ผ่าน Vercel CLIoutput ของ build/web/ คือ static site ปกติ — index.html, compiled JavaScript และ assets ที่ประกาศใน pubspec.yaml ไม่ต้องมี server
GitHub Actions CI
หัวข้อที่มีชื่อว่า “GitHub Actions CI”React CI pipeline ติดตั้ง Node, รัน npm ci, lint, test และ build Flutter CI pipeline ทำเช่นเดียวกันโดยใช้ subosito/flutter-action แทน actions/setup-node
name: React CIon: push: branches: [main]jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: '20' - run: npm ci - run: npm run lint - run: npm test -- --watchAll=false - run: npm run buildname: Flutter CIon: push: branches: [main]jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: subosito/flutter-action@v2 with: flutter-version: '3.22.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@v4
- uses: subosito/flutter-action@v2 with: flutter-version: '3.22.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