Back to Blog
CI/CD for Laravel with GitHub Actions: Automated Testing and Deployment
Automate Everything
Manual deployments are error-prone and slow. CI/CD pipelines run tests automatically and deploy with confidence. GitHub Actions provides this integrated with your repository.
Testing Workflow
name: Tests
on:
push:
branches: [main, develop]
pull_request:
branches: [main]
jobs:
tests:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_DATABASE: testing
MYSQL_ROOT_PASSWORD: password
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s
steps:
- uses: actions/checkout@v4
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: mbstring, pdo_mysql
coverage: xdebug
- name: Cache Composer dependencies
uses: actions/cache@v3
with:
path: vendor
key: composer-${{ hashFiles('composer.lock') }}
- name: Install dependencies
run: composer install --no-interaction
- name: Run tests
run: php artisan test --coverage
env:
DB_HOST: 127.0.0.1
DB_DATABASE: testing
DB_PASSWORD: password
Deployment Workflow
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
needs: tests
steps:
- uses: actions/checkout@v4
- name: Deploy to production
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /var/www/app
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan queue:restart
Environment Secrets
Store sensitive data in GitHub Secrets:
- SSH_KEY: Private key for server access
- HOST: Server IP or hostname
- USERNAME: SSH username
- SLACK_WEBHOOK: For notifications
Zero-Downtime Deployment
# Using Envoy or similar
@servers(['web' => 'user@host'])
@task('deploy')
cd /var/www
git clone --depth 1 {{ $repo }} releases/{{ $release }}
cd releases/{{ $release }}
composer install --no-dev
ln -s ../../.env .env
ln -s ../../storage storage
php artisan migrate --force
ln -sfn /var/www/releases/{{ $release }} /var/www/current
sudo systemctl reload php-fpm
@endtask
Notification on Deploy
- name: Notify Slack
if: success()
uses: rtCamp/action-slack-notify@v2
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
SLACK_MESSAGE: 'Deployed to production successfully'
Conclusion
CI/CD pipelines catch issues early and enable confident, frequent deployments. Start with automated tests, add deployment automation, and iterate on your pipeline as needs evolve.
Related Articles
Need Help With Your Project?
I respond to all inquiries within 24 hours. Let's discuss how I can help build your production-ready system.
Get In Touch