GitHub Actions เป็นเครื่องมือ CI/CD ที่ทรงพลังสำหรับการทดสอบและปรับใช้โค้ดโดยอัตโนมัติ บทความนี้จะแนะนำวิธีสร้าง CI Pipeline ที่ทดสอบโค้ดอัตโนมัติทุกครั้งที่มีการ Push ไปยัง Repository โดยใช้ Self-hosted Runner ที่ติดตั้งบน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง
GitHub Actions คืออะไร
GitHub Actions เป็นแพลตฟอร์ม Continuous Integration (CI) ที่ผสานรวมโดยตรงเข้ากับ GitHub ช่วยให้คุณสามารถเขียน Workflow ที่จะทำงานอัตโนมัติเมื่อมีเหตุการณ์ต่างๆ เกิดขึ้น เช่น การ Push โค้ด การสร้าง Pull Request หรือการสร้าง Release
- ไม่ต้องติดตั้งเซิร์ฟเวอร์ CI แยกต่างหาก
- รองรับการทดสอบแบบขนาน (Parallel Testing)
- มี Marketplace ให้เลือก Action จากชุมชน
- สามารถใช้ Self-hosted Runner เพื่อควบคุมสิ่งแวดล้อมได้อย่างสมบูรณ์
- ฟรีสำหรับ Public Repository และมี Quota สำหรับ Private Repository
โครงสร้างไฟล์ .github/workflows
Workflow ใน GitHub Actions ถูกกำหนดโดยไฟล์ YAML ที่อยู่ในโฟลเดอร์ .github/workflows ในรูท Repository ของคุณ ไฟล์เหล่านี้จะบอกให้ GitHub Actions ทราบว่าควรทำงานอะไรเมื่อเกิดเหตุการณ์ที่กำหนดไว้
name: CI Pipeline
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
องค์ประกอบหลักของ Workflow ได้แก่ name: ชื่อของ Workflow, on: เงื่อนไขที่จะทำให้ Workflow ทำงาน, jobs: รายการงานที่จะถูกดำเนินการ, runs-on: ประเภทของเครื่องที่จะใช้, และ steps: ขั้นตอนต่างๆ ที่จะถูกดำเนินการตามลำดับ
Automated Testing: Unit Test และ Integration Test
การทดสอบอัตโนมัติเป็นส่วนสำคัญของ CI Pipeline ที่ดี GitHub Actions ช่วยให้คุณสามารถรันการทดสอบ Unit Test และ Integration Test ได้อัตโนมัติทุกครั้งที่มีการ Push โค้ด
Unit Testing สำหรับ Node.js
name: Unit Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16, 18, 20]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm ci
- name: Run unit tests
run: npm run test:unit
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage/coverage-final.json
Integration Testing กับ Database
name: Integration Tests
on: [push, pull_request]
jobs:
integration:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:15
env:
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install dependencies
run: npm ci
- name: Run integration tests
run: npm run test:integration
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/testdb
Self-hosted Runner บน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง
สำหรับโปรเจคที่ต้องการการควบคุมสิ่งแวดล้อมมากขึ้น หรือต้องการโปรแกรมที่ไม่สามารถรันได้บน GitHub-hosted Runner คุณสามารถใช้ Self-hosted Runner โดยติดตั้งบน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง ได้
ข้อดีของการใช้ Self-hosted Runner: ควบคุมสิ่งแวดล้อมได้อย่างสมบูรณ์, ติดตั้ง Custom Software ได้ตามต้องการ, ไม่มี Quota Limit สำหรับการใช้งาน, และทำงานได้เร็วกว่าเพราะสิ่งแวดล้อมเตรียมพร้อมแล้ว
# 1. ดาวน์โหลด Runner Application
mkdir actions-runner && cd actions-runner
curl -o actions-runner-linux-x64-2.311.0.tar.gz -L \
https://github.com/actions/runner/releases/download/v2.311.0/actions-runner-linux-x64-2.311.0.tar.gz
tar xzf ./actions-runner-linux-x64-2.311.0.tar.gz
# 2. สร้าง Runner
./config.sh --url https://github.com/YOUR_USERNAME/YOUR_REPO --token YOUR_TOKEN
# 3. ติดตั้งเป็น Service (ทำงานเบื้องหลัง)
sudo ./svc.sh install
sudo ./svc.sh start
ตัวอย่าง CI Pipeline สำหรับ Python
name: Python CI
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.9', '3.10', '3.11']
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
cache: 'pip'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install -r requirements-dev.txt
- name: Lint with flake8
run: flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
- name: Run tests with pytest
run: pytest tests/ -v --cov=app --cov-report=xml
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./coverage.xml
Best Practices สำหรับ GitHub Actions CI
- ใช้ Matrix Strategy: ทดสอบกับหลายเวอร์ชันในครั้งเดียว ลดเวลาการทดสอบ
- Cache Dependencies: ใช้ actions/cache เพื่อลดเวลาในการติดตั้ง Dependencies
- จำกัด Permissions: ให้ Secrets และ Token มีสิทธิน้อยที่สุดเท่าที่จำเป็น
- ใช้ Branch Protection: บังคับให้ tests ผ่านก่อน Merge Pull Request
- Fail Fast: ตั้งค่า fail-fast: true ใน Matrix เพื่อหยุดทันทีเมื่อเจอข้อผิดพลาด
สรุป
GitHub Actions เป็นเครื่องมือที่ทรงพลังสำหรับการสร้าง CI Pipeline ที่ทดสอบโค้ดอัตโนมัติ ทำให้คุณสามารถค้นหาปัญหาได้เร็วขึ้นและรักษาคุณภาพของโค้ด ด้วยการตั้งค่า Self-hosted Runner บน Cloud VPS ของ ผู้ให้บริการโฮสติ้ง คุณจะได้รับความยืดหยุ่นและการควบคุมสิ่งแวดล้อมเต็มที่ ไม่มีข้อจำกัดด้าน Quota และสามารถปรับแต่งสิ่งแวดล้อมได้ตามความต้องการของโปรเจค
การลงทุนในการสร้าง CI Pipeline ที่ดีจะช่วยประหยัดเวลาและลดปัญหาในระยะยาว เริ่มต้นใช้งาน GitHub Actions วันนี้ด้วยการสร้างไฟล์ YAML ง่ายๆ ในโฟลเดอร์ .github/workflows แล้วปรับปรุงต่อไปตามความต้องการของโปรเจค
