Setup GitHub Actions for Laravel projects

  • avatar
  • 2.7K Views
  • 1 Like
  • 5 mins read

Managing a Laravel project involves many tasks that can be automated to save time and reduce errors. GitHub Actions offers a powerful way to automate workflows directly within your GitHub repository. By integrating GitHub Actions into your Laravel project, you can automate testing, deployment, and other routine tasks, ensuring a more efficient and reliable development process.

Prerequisites

Understanding of GitHub Actions and some basic concepts: A basic guide to GitHub Actions.

Why use GitHub Actions in Laravel projects?

GitHub Actions is a CI/CD (Continuous Integration and Continuous Deployment) tool that allows you to automate your software workflows directly in your GitHub repository. It provides the ability to create custom workflows that can run on specific events like pushes, pull requests, and more. For Laravel projects, GitHub Actions can be invaluable. It ensures that every piece of code is tested and validated before it is merged, maintaining the integrity of the codebase. Additionally, it helps in deploying the application smoothly, reducing the chances of errors in production.

Setting up GitHub Actions for Laravel

To start using GitHub Actions with your Laravel project, create a new workflow file in the .github/workflows directory of your repository. This file will define the automated tasks you want to run. For example, you can set up a workflow to run your tests whenever code is pushed to the repository. Here's a simple configuration to get you started:

# .github/workflows/ci.yml

name: Laravel CI

on:
push:
branches:
- master

jobs:
test:
name: Run tests
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP with Composer v2
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer:v2

- name: Install dependencies
run: composer install -q --ignore-platform-reqs --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Run tests
run: vendor/bin/phpunit tests

In this example, the workflow is triggered whenever a push event occurs on the master branch. It operates on an Ubuntu latest runner and comprises several steps:

  1. Checkout code: Retrieves the latest code from the repository.

  2. Set up PHP: Configures the PHP environment for building the application.

  3. Install dependencies: Installs the project dependencies using Composer.

  4. Run tests: Executes the application tests.

By setting up this YAML file, GitHub Actions will automatically execute tasks whenever new code is pushed to the main branch, ensuring an efficient continuous integration process.

Enhancing Your Workflow

GitHub Actions can do more than just run tests. You can extend your workflow to include tasks like static analysis, code style checks, and deployment. For instance, you can add a step to run PHPStan code analysis, generate coverage and scan it using SonarCloud:

# .github/workflows/deploy.yml

name: Laravel CI

on:
push:
branches:
- master
pull_request:
types: [opened, synchronize, reopened]

jobs:
test:
name: Run tests & analysis
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP with Composer v2
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
tools: composer:v2
coverage: xdebug

- name: Install dependencies
run: composer install -q --ignore-platform-reqs --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Run PHPStan
run: vendor/bin/phpstan analyse -c phpstan.neon

- name: Run tests with PHPUnit
run: export XDEBUG_MODE=coverage && vendor/bin/phpunit tests --coverage-html reports/ --coverage-clover=coverage.xml

- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

You can also add deployment steps to automatically deploy your application when changes are merged into the main branch. This can be done by adding a step that uses SSH to deploy to your server or by integrating with a service like AWS or DigitalOcean.

Conclusion

Integrating GitHub Actions into your Laravel project can significantly improve your workflow by automating repetitive tasks and ensuring consistency across your development process. By setting up workflows for testing, code analysis, and deployment, you can focus more on writing code and less on managing the pipeline. Give it a try, and see how it can transform your Laravel development experience.

 Join Our Monthly Newsletter

Get the latest news and popular articles to your inbox every month

We never send SPAM nor unsolicited emails

0 Comments

Leave a Reply

Your email address will not be published.

Replying to the message: View original

Hey visitor! Unlock access to featured articles, remove ads and much more - it's free.