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

CI ด้วย GitHub Actions

ถ้าคุณเคยตั้งค่า GitHub Actions สำหรับ TypeScript project มาก่อน pattern คือ: checkout → setup-node → install → lint → type-check → test Python CI ตาม four-step logic เดียวกันทุกประการ — ด้วยชื่อ action เฉพาะ Python และการเรียก tool

TypeScript
# .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
Python
# .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 -q

นี่คือ workflow ที่สมบูรณ์มากขึ้นพร้อม caching, coverage reporting และ matrix testing บน Python หลาย versions:

.github/workflows/ci.yml
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

การแทนที่ 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 เพื่อดูผลจริง

GitHub Actions action ใดตั้งค่า Python version เฉพาะใน CI?
คำสั่ง ruff ใดตรวจสอบ formatting โดยไม่แก้ไขไฟล์ (สำหรับ CI)?
อะไรคือคู่เทียบ uv ของ "npm ci" — reproducible install จาก lockfile?
ใน pytest CI command flag ใดทำให้ run fail ถ้า coverage ลดลงต่ำกว่า 80%?