
Setting Up Continuous Deployment (CD) in Laravel with GitHub Actions
Published on Jan 4, 2025
If you didn't follow a long, we previously published a post about deploying laravel application to digital ocean you can find it here.
Previously I was doing deployment manually, in this blog for example what I was doing is: to communicate with github I added a public key in github under my account settings > SSH and GPG keys. which I generated on the server visa ssh-keygen command. When I add any change in the code and push it to github, I pull those changes from github manually, and if I have any new migrations I run php artisan migrate and so on.
Well, I'm a lazy person and don't want to ssh into the server every time I push new code into my main branch and run some commands.
Lucky me, there are some easy ways to automate this process one of them by using github actions. So, all I need to do is to push the changes to github and it should be pulled automatically and running any script I want. This is something we call Continues Deployment (check this article for Continuous Integration). Let's check how we can accomplish that:
Prerequisites
- DigitalOcean Droplet: You need to have a running laravel application on a digital ocean droplet.
- GitHub Repository: Your Laravel application’s code is hosted in a GitHub repository.
- SSH Access: You have an ssh keys on you PC
Add SSH Key to The Repo Secrets
An important step we are going to need is to add our public key into the server and private key into the repo secrets
Add public key to server
To add the public key into the server if you don't already added it you may run the following command:
cat ~/.ssh/id_rsa.pub | ssh root@your_server_ip 'cat >> ~/.ssh/authorized_keys'
This command will copy the public key on your pc and add it into authorized_keys file on the server
Add private key to github
Copy the private key on your pc and navigate to repository > Settings > Secrets and variables > Actions and add new Repository Secret with Name DEPLOY_KEY and past the ssh private key in it's value.
Let's do the magic
In your project add file deploy.yml under .github/workflows and add the following code:
name: Deploy Laravel Application on: push: branches: - main pull_request: branches: - main jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v3 - name: Set Up SSH run: | mkdir -p ~/.ssh echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan -H your_server_ip >> ~/.ssh/known_hosts - name: Deploy Application run: | ssh root@your_server_ip \ "cd /var/www/project_directory && \ 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 && \ supervisorctl restart all"
This setup is exactly what I'm doing to automate deployment in this blog.
In simple words what this code is doing is running the job on Github Action Runner (not on your server), to be able to communicate with your server it will need the ssh key we added in the secrets.
Then it will login to the server and run the necessary commands. It's that simple!
if you want to understand basic commands for github actions, please check this article
Conclusion
With this simple setup, you’ve automated the deployment process for your Laravel application using GitHub Actions. Every change pushed to the main branch or merged via a pull request will now be deployed automatically, saving time and reducing manual effort.