18. Job Matrix
Matrix
Matrix คืออะไร?
Matrix ใน GitHub Actions เป็นฟีเจอร์ที่ช่วยให้เราสามารถรัน job เดียวกันหลายครั้งด้วยค่าตัวแปรที่แตกต่างกัน
เหมือนกับการสร้าง “ตาราง” ของการทดสอบที่ครอบคลุมหลายๆสถานการณ์
ทำให้เราเขียน Workflow ครั้งเดียว แล้วสามารถรัน workflow ได้หลายครั้งด้วยค่าตัวแปรที่แตกต่างกัน
เช่นเราอยาก build app สำหรับ Linux, Mac และ Windows เราก็จะเขียน Workflow ครั้งเดียว แล้วกำหนด Matrix ให้มี Linux, Mac และ Windows
ทำให้ Workflow จะทำงาน 3 ครั้ง แบบขนานกัน แบ่งเป็น
- Linux
- Mac
- Windows
ตัวอย่างการใช้งาน
- ทดสอบหลาย Environment (Node.js versions, OS, browsers)
- Build หลาย Configuration (development, staging, production)
- Deploy ไปหลาย Target (different regions, environments)
- ประหยัดเวลา - รันแบบ parallel แทนการเขียน job แยก
ตัวอย่างการใช้งาน Matrix กับ React CSR (Vite)
name: React CI/CD
on: push: branches: [main, develop] pull_request: branches: [main]
jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: # ทดสอบหลาย OS และ Node version os: [ubuntu-latest, windows-latest, macos-latest] node-version: [18, 20, 21] # ทดสอบหลาย browser (สำหรับ e2e testing) browser: [chrome, firefox, safari] exclude: # Safari ใช้ได้แค่บน macOS - os: ubuntu-latest browser: safari - os: windows-latest browser: safari include: # เพิ่ม configuration พิเศษ - os: ubuntu-latest node-version: 20 browser: chrome coverage: true
steps: - uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} cache: "npm"
- name: Install dependencies run: npm ci
- name: Run tests run: npm test env: BROWSER: ${{ matrix.browser }}
- name: Run coverage (if enabled) if: matrix.coverage run: npm run test:coverage
- name: Build for ${{ matrix.os }} run: npm run build env: NODE_ENV: production
build-and-deploy: needs: test runs-on: ubuntu-latest strategy: matrix: environment: [staging, production] include: - environment: staging api_url: https://api-staging.example.com deploy_target: staging-bucket - environment: production api_url: https://api.example.com deploy_target: prod-bucket
steps: - uses: actions/checkout@v4
- name: Setup Node.js uses: actions/setup-node@v4 with: node-version: 20 cache: "npm"
- name: Install dependencies run: npm ci
- name: Build for ${{ matrix.environment }} run: npm run build env: VITE_API_URL: ${{ matrix.api_url }} VITE_ENV: ${{ matrix.environment }}
- name: Deploy to ${{ matrix.deploy_target }} run: | echo "Deploying to ${{ matrix.environment }}" # AWS S3 deployment example aws s3 sync dist/ s3://${{ matrix.deploy_target }}
ตัวอย่างการใช้ Matrix กับ Elysia
เป็นตัวอย่างง่ายๆนะ
เราจะ build Elysia โดยการใช้ Bun versions ต่างๆ เพื่อให้เห็นภาพการใช้งาน Matrix เท่านั้นนะ
name: ElysiaJS CI - Bun Versions
on: push: branches: [main, develop] pull_request: branches: [main]
jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: # ทดสอบหลาย Bun version bun-version: ["1.0.0", "1.0.15", "1.0.25", "latest", "canary"] # ทดสอบหลาย OS os: [ubuntu-latest, macos-latest, windows-latest] exclude: # Canary อาจไม่เสถียรบน Windows - os: windows-latest bun-version: canary
steps: - uses: actions/checkout@v4
- name: Setup Bun ${{ matrix.bun-version }} uses: oven-sh/setup-bun@v1 with: bun-version: ${{ matrix.bun-version }}
- name: Show Bun version run: bun --version
- name: Install dependencies run: bun install
- name: Run tests run: bun test env: NODE_ENV: test
- name: Build project run: bun run build