CI ด้วย GitHub Actions
Node CI workflow ที่คุณรู้จักอยู่แล้ว
หัวข้อที่มีชื่อว่า “Node CI workflow ที่คุณรู้จักอยู่แล้ว”ถ้าคุณเคยตั้งค่า GitHub Actions สำหรับ TypeScript project มาก่อน pattern คือ: checkout → setup-node → install → lint → type-check → test Python CI ตาม four-step logic เดียวกันทุกประการ — ด้วยชื่อ action เฉพาะ Python และการเรียก tool
เทียบกันชัดๆ: Node vs Python CI workflow
หัวข้อที่มีชื่อว่า “เทียบกันชัดๆ: Node vs Python CI workflow”# .github/workflows/ci.yml (Node / TypeScript)name: CI
on: push: branches: [main, dev] pull_request: branches: [main]
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Setup Node uses: actions/setup-node@v4 with: node-version: '20' cache: 'npm'
- name: Install dependencies run: npm ci
- name: Lint run: npx eslint src/ --max-warnings 0
- name: Type check run: npx tsc --noEmit
- name: Test run: npx jest --coverage# .github/workflows/ci.yml (Python)name: CI
on: push: branches: [main, dev] pull_request: branches: [main]
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Setup Python uses: actions/setup-python@v5 with: python-version: '3.12' cache: 'pip'
- name: Install dependencies run: pip install -r requirements.txt
- name: Lint & format check (ruff) run: | ruff check . ruff format --check .
- name: Type check (mypy) run: mypy src/ --strict
- name: Test (pytest) run: pytest --tb=short -qPython CI workflow แบบ production เต็มรูปแบบ
หัวข้อที่มีชื่อว่า “Python CI workflow แบบ production เต็มรูปแบบ”นี่คือ workflow ที่สมบูรณ์มากขึ้นพร้อม caching, coverage reporting และ matrix testing บน Python หลาย versions:
name: CI
on: push: branches: [main, dev] pull_request: branches: [main]
jobs: quality: name: Lint & Type Check runs-on: ubuntu-latest steps: - uses: actions/checkout@v4
- name: Set up Python uses: actions/setup-python@v5 with: python-version: '3.12' cache: 'pip'
- name: Install dev dependencies run: | pip install --upgrade pip pip install -r requirements-dev.txt
- name: Lint with ruff run: ruff check . --output-format=github
- name: Check formatting with ruff run: ruff format --check .
- name: Type check with mypy run: mypy src/ --strict
test: name: Test (Python ${{ matrix.python-version }}) runs-on: ubuntu-latest strategy: matrix: python-version: ['3.11', '3.12']
steps: - uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} cache: 'pip'
- name: Install dependencies run: | pip install --upgrade pip pip install -r requirements.txt pip install -r requirements-dev.txt
- name: Run tests with coverage run: pytest --cov=src --cov-report=xml --cov-fail-under=80 -v
- name: Upload coverage to Codecov uses: codecov/codecov-action@v4 with: file: ./coverage.xmlใช้ uv เพื่อ CI ที่เร็วขึ้น
หัวข้อที่มีชื่อว่า “ใช้ uv เพื่อ CI ที่เร็วขึ้น”การแทนที่ pip ด้วย uv ใน CI ลดเวลาติดตั้งจาก 30–90 วินาทีเหลือ 2–5 วินาทีในส่วน projects ส่วนใหญ่:
- name: Install uv uses: astral-sh/setup-uv@v3 with: version: "latest"
- name: Install dependencies with uv run: uv sync --frozen # เทียบกับ npm ci — ติดตั้งจาก lockfile อย่างแม่นยำหมายเหตุเรื่อง Playground: ไฟล์ YAML ของ GitHub Actions กำหนด workflow ที่รันบนเซิร์ฟเวอร์ของ GitHub จึง demo ใน playground บน browser ไม่ได้ ให้ commit ไฟล์
.github/workflows/ci.ymlเข้า repository ของคุณแล้ว push เพื่อดูผลจริง