Automate Everything
Manual deployments are error-prone. Let machines do it.
GitHub Actions Basics
Create .github/workflows/ci.yml:
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
- name: Install dependencies
run: composer install
- name: Run tests
run: php artisan test
Add Linting
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
- run: npm ci
- run: npm run lint
Auto-Deploy to Staging
deploy-staging:
needs: [test, lint]
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Deploy to staging
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.STAGING_HOST }}
username: ${{ secrets.STAGING_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/staging
git pull
composer install --no-dev
php artisan migrate --force
Production Deploy
Manual approval:
deploy-production:
needs: [deploy-staging]
runs-on: ubuntu-latest
environment: production # Requires approval
steps:
- name: Deploy to production
# Same as staging but different host
What to Run
- On every push: Tests, linting
- On PR merge to main: Deploy to staging
- Manual trigger: Deploy to production
Tips
- Keep builds fast (cache dependencies)
- Run tests in parallel
- Fail fast - stop on first error
- Notify on failures (Slack, email)
