Back to Blog

Github actions with Laravel Pint

Julian Beaujardin
Julian Beaujardin February 20th, 2023

Instead of always running PHP CS Fixer or Laravel Pint locally, it might be a good idea to set up a GitHub action, that will reformat the code in Github following the laravel Pint formatting rules after each push.

This is how it worked for me!

I built a CI pipeline by creating a .github/workflows/pint.yml at the top of my repo. Inside the workflows folder is where I place my actions as .yml files. In this case, I have: pint.yml

I have personally been using Github actions with aglipanci/laravel-pint-action and it works great!

name: Check & fix styling
on: [push, pull_request]
jobs:
  phplint:
    name: Laravel Pint
    runs-on: ubuntu-latest
    steps:
        - uses: actions/checkout@v3
          with:
            fetch-depth: 2
        - name: Laravel Pint
          uses: aglipanci/laravel-pint-action@2.0.0
          with:
            preset: laravel
            configPath: "pint.json"
            pintVersion: 1.2

The pint.json file in the root of my laravel project will be used for configuration during the run of the Action.

So far so good! However, the action above will only notify us about potential code formating issues. What we really want is Github to also make the suggested changes and push them automatically to our branch as a new commit.

In order for me to achieve such behaviour I also have to use a git-auto-commit action.

    - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
        commit_message: Fixing styling
        skip_fetch: true

Great! So I now have Laravel Pint running and linting my code and then push it back to my branch so I can also see a diff with the changes and can now make sure that all of the code in my main branch is following laravel Pint rules.

The full action / code now look like this:

name: Check & fix styling
on: [push, pull_request]
jobs:
  phplint:
    name: Laravel Pint
    runs-on: ubuntu-latest
    steps:
        - uses: actions/checkout@v3
          with:
            fetch-depth: 2
        - name: Laravel Pint
          uses: aglipanci/laravel-pint-action@2.0.0
          with:
            preset: laravel
            configPath: "pint.json"
            pintVersion: 1.2

        - name: Commit changes
          uses: stefanzweifel/git-auto-commit-action@v4
          with:
            commit_message: Fixing styling
            skip_fetch: true

No, after every push I see pint's commit:

alt text

And when I click on the commit, I see a diff with the changes made by Pint.

alt text