Automating Laravel Deployments with GitHub Actions
Setting up automated deployments for my Laravel projects used to be a headache, but GitHub Actions has made it significantly easier. Here's how I automate deployments to Ubuntu servers using GitHub Actions, and some common gotchas I've run into along the way.
Setting up automated deployments for my Laravel projects used to be a real pain. I’d end up manually SSHing into servers, running commands, and hoping nothing broke. Now, I use GitHub Actions, and it's made my life a lot easier. This post will walk you through how I automate Laravel deployments to Ubuntu servers using GitHub Actions, along with some common issues I've encountered.
My Workflow
My workflow is fairly straightforward. It runs tests, clears the cache, migrates the database, and then deploys the code to the server. I trigger the workflow on every push to the main branch.
Here’s a basic example of my GitHub Actions workflow file (.github/workflows/laravel-deployment.yml):
name: Laravel Deployment
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
extensions: pdo,openssl,mbstring,json
- name: Install Dependencies
run: composer install --no-interaction
- name: Copy .env
run: |
cp .env.example .env
- name: Run Migrations
run: php artisan migrate --force
- name: Clear Cache
run: php artisan cache:clear
- name: Deploy to Server
uses: appleboy/[email protected]
with:
host: your_server_ip
username: your_username
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /var/www/your_project
git pull origin main
composer install --no-interaction
php artisan migrate --force
php artisan cache:clear
Important: Replace your_server_ip, your_username, and the SSH_PRIVATE_KEY secret with your actual server details. I store my SSH private key as a GitHub secret. This is much safer than putting it directly in the workflow file.
Setting up SSH Keys
To deploy to your server, you'll need to set up an SSH key. First, generate a new SSH key pair on your local machine (or on a dedicated server if you prefer):
ssh-keygen -t rsa -b 4096 -f deploy_key
Then, add the public key (deploy_key.pub) to the authorized_keys file on your server. Make sure the permissions on the authorized_keys file are correct (usually 600).
Next, add the private key as a secret in your GitHub repository. Go to your repository's settings, then Secrets and Variables, and click on
Related reading
Frequently asked questions
Why use GitHub Actions for Laravel deployments?
GitHub Actions lets me automate the entire deployment process, from running tests to deploying code, directly within my GitHub repository. It's much more simpler than manually SSHing into servers.
What's the difference between GitHub Actions and other CI/CD tools?
While other tools like Jenkins or GitLab CI exist, GitHub Actions are tightly integrated with GitHub, making setup and usage simpler, especially for projects already hosted there. I find it's less overhead for smaller projects.
Can I use GitHub Actions with other server operating systems?
Yes, I’ve used it with AlmaLinux too. The key is to ensure your workflow uses the correct commands and paths for your server's OS. You’ll need to adjust the scripts accordingly.