Every developer has had this experience: you deploy code manually, something goes wrong, and you spend an hour figuring out why the server state is different from what you tested. CI/CD prevents this by automating the process.
What CI/CD gives you
Continuous Integration (CI) automatically runs your tests on every push. Continuous Deployment (CD) automatically deploys when tests pass. Together, they give you confidence that what you merged is what you deployed.
GitHub Actions setup
Create .github/workflows/deploy.yml:
name: Deploy
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Install dependencies
run: composer install --no-dev --optimize-autoloader
- name: Run tests
run: php artisan test
deploy:
needs: test
runs-on: ubuntu-latest
steps:
- name: Deploy to server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/yourapp
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
Environment secrets
Store sensitive values (server host, SSH keys, database passwords) as GitHub Secrets. Never put them in your workflow file.
Database migrations in CI
Run migrations with --force in production to bypass the interactive prompt. Make sure your migrations are backward-compatible — add columns with defaults, never drop columns in the same migration as a code change.
Zero-downtime deployment
For zero-downtime, consider deploying to a new directory and swapping a symlink. Tools like Deployer or Envoyer handle this for you with rollback support built in.



