How to Integrate TinyMCE in Laravel | Latest 2024
Read More
Deploying a web application effectively is crucial for delivering a reliable user experience. However, the deployment process can become complex without the right tools and strategies. This guide will show you how to build and deploy your Laravel application using GitHub Actions, a powerful CI/CD tool that simplifies deployment to AWS Elastic Beanstalk or any server via SSH.
GitHub Actions is a CI/CD (Continuous Integration and Continuous Deployment) platform that enables developers to automate their build, test, and deployment pipelines directly from their GitHub repositories. For Laravel developers, GitHub Actions provide:
For more details, you can explore GitHub’s official documentation on GitHub Actions.
Here’s a comprehensive guide on how to set up GitHub Actions for deploying a Laravel application. We’ll use the SSH protocol to deploy to a remote server.
First, create a .github/workflows
directory in your Laravel project and add a main.yml
file for the GitHub Actions configuration:
name: Build and Deploy
on:
push:
branches:
- main
jobs:
build:
name: Build and Deploy
runs-on: ubuntu-latest
steps:
- name: Setup Environment
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
The above configuration specifies that every time there is a push to the main
branch, the job Build and Deploy
will run on the latest version of Ubuntu.
To deploy your Laravel application, we'll use the appleboy/ssh-action
to run deployment commands on the remote server:
- name: Deploy to production
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
port: ${{ secrets.PORT }}
script: |
cd /var/www/html
echo "Deploying application ..."
# Enter maintenance mode
php artisan down || true
# Update codebase
git fetch origin
git reset --hard origin/main
git pull origin release
# Install dependencies based on lock file
composer install --no-interaction --prefer-dist --optimize-autoloader
# Clear cache
php artisan config:cache
php artisan optimize
# Migrate database
php artisan migrate --force
# Exit maintenance mode
php artisan up
echo "Application deployed!"
main
branch.Full YML code:
name: Build and Deploy
on:
push:
branches:
- main
jobs:
build:
name: Build and Deploy
runs-on: ubuntu-latest
steps:
- name: Setup Environment
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
- name: Deploy to production
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
port: ${{ secrets.PORT }}
script: |
cd /var/www/html
echo "Deploying application ..."
# Enter maintenance mode
php artisan down || true
# Update codebase
git fetch origin
git reset --hard origin/main
git pull origin release
# Install dependencies based on lock file
composer install --no-interaction --prefer-dist --optimize-autoloader
# Clear cache
php artisan config:cache
php artisan optimize
# Migrate database
php artisan migrate --force
# Exit maintenance mode
php artisan up
echo "Application deployed!"
Using GitHub Actions to automate the deployment of your Laravel application streamlines your workflow, saves time, and reduces the risk of human error. By following the steps outlined in this guide, you can set up a robust CI/CD pipeline that ensures your Laravel application is deployed smoothly and securely. Start automating your deployments today to boost productivity and focus more on writing quality code!
If you found this guide helpful, don't forget to share it with your fellow developers. Also, check out our related posts on Laravel Performance Optimization and Automating Backups with GitHub Actions to enhance your development workflow further!
Recent posts form our Blog
0 Comments
Like 0