GitHub Actions CI/CD Configuration Tutorial: Automated Build, Test, and Deployment
GitHub Actions is GitHub's built-in CI/CD platform, letting you define automated workflows directly in your repository.
1. CI/CD Basics
CI (Continuous Integration): Auto-build and test on code changes
CD (Continuous Deployment): Auto-deploy after tests pass
2. GitHub Actions Fundamentals
2.1 Core Concepts
| Term | Description |
|---|---|
| Workflow | Automated process definition |
| Job | Execution unit in a workflow |
| Step | Specific operation in a job |
| Action | Reusable function module |
| Runner | Server executing the workflow |
3. Node.js CI/CD Practice
3.1 Complete Workflow
name: Node.js CI/CD
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: npm run lint
test:
needs: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
cache: 'npm'
- run: npm ci
- run: npm test
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm ci
- run: npm run build
- name: Deploy to VPS
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
source: "dist/"
target: "/var/www/app/"
4. SSH Deployment
Set secrets in GitHub: SERVER_HOST, SERVER_USER, SSH_PRIVATE_KEY
5. Popular Actions
| Action | Purpose |
|---|---|
| actions/checkout | Checkout code |
| actions/setup-node | Setup Node.js |
| actions/setup-python | Setup Python |
| appleboy/scp-action | SCP file transfer |
| appleboy/ssh-action | SSH commands |
| docker/build-push-action | Docker build/push |
6. Other Languages
PHP (Laravel)
name: Laravel CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: test
ports:
- 3306:3306
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: mbstring, pdo_mysql
- run: composer install -q --no-ansi --no-interaction
- run: cp .env.example .env
- run: php artisan key:generate
- run: php artisan migrate --force
- run: php artisan test
Python (Django)
name: Django CI
on: [push]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.11'
- run: pip install -r requirements.txt
- run: python manage.py test
7. Best Practices
- Split workflows: lint on PR, full test before merge, deploy only on main
- Cache dependencies to reduce build time
- Conditional execution for specific branches
- Notifications on failure
- Use Secrets for sensitive info, limit permissions
8. Summary
GitHub Actions makes CI/CD simpler than ever. Start with Lint + Test, gradually add deployment steps for a complete DevOps pipeline.