Build and Deploy Your Laravel Application Using GitHub Actions
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.
Why Use GitHub Actions for Laravel Deployment?
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:
- Seamless Integration: Automate your deployment process without relying on third-party CI/CD tools.
- Custom Workflows: Create custom workflows tailored to your project’s needs, ensuring efficient deployment.
- Secure Secrets Management: Safely store sensitive data like SSH keys and API tokens directly in GitHub Secrets.
For more details, you can explore GitHub’s official documentation on GitHub Actions.
Step-by-Step Guide to Build and Deploy Laravel Applications
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.
Step 1: Setup Environment in GitHub Actions
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.
Step 2: Deploy to Production Using SSH Action
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!"
Step 3: Explanation of the Deployment Commands
- Enter Maintenance Mode: Puts the Laravel application in maintenance mode to prevent user access during deployment. Learn more in the Laravel Maintenance Mode documentation.
- Update Codebase: Fetches the latest changes from the repository and resets the working directory to match the
mainbranch. - Install Dependencies: Installs necessary PHP dependencies via Composer. For more on Composer, visit the official Composer documentation.
- Clear Cache and Optimize: Clears and optimizes the configuration cache to boost performance.
- Migrate Database: Applies any new database migrations. More details on this can be found in the Laravel Migrations documentation.
- Exit Maintenance Mode: Brings the application back online.
Best Practices for CI/CD with Laravel and GitHub Actions
- Use Secrets for Sensitive Information: Ensure all sensitive information, like server credentials, is stored in GitHub Secrets.
- Test Before Deploying: Always run tests in your CI/CD pipeline before deploying to production. Check out Laravel Testing for effective testing strategies.
- Optimize for Speed: Use caching mechanisms to speed up builds and reduce redundant tasks. Learn more about caching in GitHub Actions.
- Monitor Deployments: Implement error monitoring tools like Sentry or Bugsnag to track and fix issues post-deployment.
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!"
Conclusion
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!
Boost Your Deployment Workflow Today!
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!
0 Comments
Like 0