What is CI/CD?
Continuous Integration (CI) automatically tests code on every push. Continuous Deployment (CD) automatically deploys tested code to production. GitHub Actions provides both, integrated directly into your repository.
Basic Workflow
Create .github/workflows/deploy.yml:
name: Deploy to Production
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: "8.2"
- name: Install dependencies
run: composer install --no-dev
- name: Run tests
run: vendor/bin/phpunit
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy via SSH
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/myapp
git pull origin main
composer install --no-dev
php artisan migrate --forceSetting Up Secrets
- Go to your repository → Settings → Secrets and variables → Actions
- Add
SERVER_HOST,SERVER_USER, andSSH_PRIVATE_KEY
Best Practices
- Always run tests before deploying
- Use environment-specific secrets
- Add a manual approval step for production deploys
- Cache dependencies to speed up builds