Automate Code Formatting in Laravel Projects with GitHub Actions and Pint

Automate Laravel code formatting with GitHub Actions and Pint. Ensure PSR-12 compliance, save time, and maintain clean code effortlessly.
Automate Code Formatting in Laravel Projects with GitHub Actions and Pint Image

In the world of modern software development, maintaining clean and consistent code is crucial. For Laravel developers, Pint is a fantastic tool that helps automate code formatting according to PSR-12 standards. But what if you could automate this process even further? Enter GitHub Actions! By setting up a GitHub Action workflow for Pint, you can ensure that every push to your repository is automatically formatted, saving you time and effort.

In this blog post, we’ll walk you through the steps to set up GitHub Actions for Pint in your Laravel project. Let’s dive in!

Why Use Pint with GitHub Actions?

Pint is a lightweight code formatter for Laravel projects that ensures your code adheres to PSR-12 standards. By integrating Laravel Pint with GitHub Actions, you can:

  • Automatically format your code on every push or pull request.
  • Maintain consistent code style across your team.
  • Save time by eliminating manual formatting tasks.
  • Ensure your codebase remains clean and professional.

Step-by-Step Guide to Setting Up GitHub Actions for Pint

Step 1: Grant GitHub Actions Read and Write Permissions

Before setting up the workflow, you need to ensure that GitHub Actions has the necessary permissions to read and write to your repository. Here’s how:

  1. Go to your repository on GitHub.
  2. Navigate to Settings > Actions > General.

    setting page
  3. Scroll down to the Workflow Permissions section.
  4. Select Read and Write Permissions.

    workflow permisson page
  5. Click Save to apply the changes.

This step ensures that GitHub Actions can automatically commit changes (like formatted code) back to your repository.

Step 2: Create the Workflow File

Next, you’ll create a GitHub Actions workflow file to define the steps for running Pint. Here’s how:

  1. Go to the Code section of your repository.
  2. Create a new file in the .github/workflows/ directory. Name it pint.yml.

    • GitHub will automatically create the .github/workflows/ directory if it doesn’t already exist.
    create new file step
  3. Add the following YAML code to the pint.yml file:
name: Fix Code Style

on: [push]

jobs:
  lint:
    runs-on: ubuntu-latest
    strategy:
      fail-fast: true
      matrix:
        php: [8.4]

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

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: json, dom, curl, libxml, mbstring
          coverage: none

      - name: Install Pint
        run: composer global require laravel/pint

      - name: Run Pint
        run: pint

      - name: Commit linted files
        uses: stefanzweifel/git-auto-commit-action@v5

Step 3: Commit the Changes

Once you’ve added the YAML code to the pint.yml file, save and commit the changes to your repository. This will trigger the GitHub Actions workflow.

Step 4: Push Your Code and Watch the Magic Happen

Now, whenever you push code to the master/main branch (or create a pull request), GitHub Actions will automatically:

  1. Check out your code.
  2. Set up PHP and install dependencies.
  3. Run Pint to format your code.
  4. Commit any formatting changes back to the repository.

Pint is highly customizable. You can define your own formatting rules by creating a pint.json file in the root of your project. You can also customize the commit message in the workflow file by modifying the git commit -m line in the YAML code.

Output after push the code in repository:

pint yml file setup

Official Documentation

For more advanced configurations and options, check out the official documentation for Laravel Pint and GitHub Actions.

Conclusion

By setting up GitHub Actions for Pint in your Laravel project, you can automate code formatting and ensure your codebase remains clean and consistent. This not only saves time but also helps your team focus on writing great code instead of worrying about formatting.

Give it a try and Happy coding! 🚀

Pro Tip: If you’re working in a team, consider adding this workflow to your CI/CD pipeline to enforce code formatting standards across the board.

Do you Like?