DevOps Guide 2026

Fast CI/CD Pipelines GitHub Actions

เรียนรู้วิธีสร้าง CI/CD Pipeline ที่รวดเร็วด้วย Caching และ Matrix Strategy ลดเวลา Build 30-50%

GitHub Actions Caching Matrix Strategy

Pipeline Flow & Cache Layers

Pipeline Flow Code Push git push Trigger GitHub Actions Cache Restore Deps Matrix Parallel Jobs Deploy Cache Layers 1 Node Modules key: os-node-hashFiles ~70% cache hit rate ⏱ Save ~2-3 min path: node_modules 2 Build Cache key: os-build-hash ~50% faster builds ⏱ Save ~1-2 min path: .next/cache 3 Docker Cache key: os-docker-hash Layer caching ⏱ Save ~3-5 min type: registry 📊 30-50% Build Time Reduction 🚀 70% Cache Hit Rate ⚡ Parallel Execution

Prerequisites / สิ่งที่ต้องเตรียม

  • GitHub Account - ต้องมี GitHub repository ที่เปิดใช้งาน GitHub Actions
  • GitHub Actions Syntax - ความเข้าใจพื้นฐานเกี่ยวกับ YAML workflow files
  • Docker Basics - ความเข้าใจเกี่ยวกับ Docker images และ containers
  • Dependency Caching Concepts - ความเข้าใจเกี่ยวกับ package managers (npm, pip, yarn)

บทนำ (Introduction)

GitHub Actions เป็นเครื่องมือ CI/CD ที่ทรงพลัง แต่หากไม่ optimize อย่างถูกต้อง pipeline ของคุณอาจใช้เวลานานเกินไป ในบทความนี้เราจะเรียนรู้เทคนิคพื้นฐานในการปรับปรุงประสิทธิภาพ CI/CD Pipeline โดยใช้ Caching และ Matrix Strategy

เหตุผลที่อยากเขียน

การตั้งค่า CI/CD อย่างรวดเร็วโดยใช้แคชและ matrix ทำให้เวลา build ลดลงอย่างมาก เหมาะกับทีม DevOps ที่ต้องส่งหลายสภาพแวดล้อม

Why Caching Matters / ทำไม Caching ถึงสำคัญ

Caching เป็นเทคนิคที่ช่วยลดเวลาในการดาวน์โหลด dependencies และ rebuild artifacts โดยเก็บผลลัพธ์จาก runs ก่อนหน้าและนำกลับมาใช้ใหม่

ประหยัดเวลา

ลดเวลาทำงานของ pipeline ลง 30-50% โดยไม่ต้องดาวน์โหลด dependencies ทุกครั้ง

ลดค่าใช้จ่าย

GitHub Actions มี limit การใช้งาน minutes per month การลดเวลาเท่ากับลดค่าใช้จ่าย

ประเภทของ Cache:

Dependency Cache

เก็บ node_modules, .venv, vendor และ dependencies อื่นๆ

Build Cache

เก็บผลลัพธ์จากการ build เช่น .next/cache, dist, build

Docker Layer Cache

เก็บ Docker layers เพื่อใช้กับ builds ถัดไป

Setting Up Workflows / การตั้งค่า Workflow

สร้างไฟล์ workflow ที่ .github/workflows/ci.yml

name: CI/CD Pipeline

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]

jobs:
  build-and-test:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'
      
      - name: Cache node modules
        uses: actions/cache@v4
        id: cache-npm
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ runner.os }}-node-

Cache Key Strategy: ใช้ hashFiles() เพื่อสร้าง unique key จาก package-lock.json หรือ yarn.lock

Matrix Strategy Examples / ตัวอย่าง Matrix Strategy

Matrix Strategy ช่วยให้คุณทดสอบหลายเวอร์ชันของ dependencies และ OS พร้อมกัน ลดเวลารอคอยผลลัพธ์ทั้งหมด

name: Test Matrix

on: [push, pull_request]

jobs:
  test:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
        node-version: [18.x, 20.x, 22.x]
    
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      
      - name: Setup Node ${{ matrix.node-version }}
        uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      
      - name: Cache node modules
        uses: actions/cache@v4
        with:
          path: node_modules
          key: ${{ matrix.os }}-node-${{ matrix.node-version }}-${{ hashFiles('package-lock.json') }}
          restore-keys: |
            ${{ matrix.os }}-node-${{ matrix.node-version }}-
      
      - name: Install dependencies
        run: npm ci
      
      - name: Run tests
        run: npm test
3
OS Platforms
3
Node Versions
9
Total Jobs

Debugging Cache Misses / การ Debug Cache Misses

เมื่อ cache ไม่ทำงาน ตรวจสอบปัญหาทั่วไปเหล่านี้:

1. Cache Key ไม่ตรงกัน

ตรวจสอบว่า hashFiles() อ้างอิงไฟล์ที่ถูกต้อง (package-lock.json, yarn.lock)

2. Path ไม่ถูกต้อง

ตรวจสอบว่า path ชี้ไปที่ directory ที่ถูกต้อง (node_modules, .next/cache)

3. Cache Size เกิน Limit

GitHub Actions cache limit คือ 10 GB per repository และ 7 days retention

4. Branch ไม่ Match

Cache โดย default จะ share ระหว่าง branches เดียวกันเท่านั้น ใช้ scope-key หากต้องการ cross-branch caching

# Debug cache hits/misses
- name: Check cache hit
  if: steps.cache-npm.outputs.cache-hit != 'true'
  run: echo "Cache MISS - Installing dependencies..."

- name: Cache hit info
  if: steps.cache-npm.outputs.cache-hit == 'true'
  run: echo "Cache HIT - Using cached dependencies..."

FAQ / คำถามที่พบบ่อย

Q: Cache จะถูกลบเมื่อไหร่?
A: GitHub Actions cache จะถูกลบอัตโนมัติหลังจาก 7 วันที่ไม่ได้ใช้ หรือเมื่อครบ quota 10 GB
Q: สามารถใช้ cache ร่วมกับ private packages ได้ไหม?
A: ได้ แต่ต้องใช้ actions/cache@v4 กับ restore-keys ที่เหมาะสม และต้องมี authentication tokens ที่ถูกต้อง
Q: Matrix ใช้ memory มากเกินไป ทำอย่างไร?
A: ใช้ max-parallel จำกัดจำนวน jobs ที่รันพร้อมกัน หรือแบ่ง matrix เป็นหลาย workflow
Q: วิธี monitor cache usage?
A: ใช้ GitHub REST API หรือ Actions UI ใน repository settings เพื่อดู cache statistics

Related Articles / บทความที่เกี่ยวข้อง