Run Laravel Pint as part of your CI Pipeline with Github Actions
Nuno Maduro from the Laravel core-team announced the launch of a new first-party package, Laravel Pint which is "[...] an opinionated PHP code style fixer for minimalists. Pint is built on top of PHP-CS-Fixer and makes it simple to ensure that your code style stays clean and consistent."
This is great but what if only a few developers within an organization are using Pint ? Do we explicitly ask the entire dev team to use Pint ? Well, in this article we will take a different (and a more elegant) approach.
We will not cover the innstallation of the package. You can find more informations about the package and how it works on its official github repo. We will be focusing of how we can run Laravel Pint against our code as part of our CI pipeline using Github Actions.
If you don't have a CI pipeline built already you should create a .github/workflows folder at the root of your repo. Inside the workflows folder is where you'll place your actions as .yml files.
I've been using aglipanci/laravel-pint-action and it works great. If you want to use it you can reference it in your yml file like so:
1name: PHP Linting (Pint)2on:3 workflow_dispatch:4 push:5 branches-ignore:6 - 'dependabot/npm_and_yarn/*'7jobs:8 phplint:9 runs-on: ubuntu-latest10 steps:11 - uses: actions/checkout@v312 with:13 fetch-depth: 214 - name: "laravel-pint"15 uses: email@example.com with:17 preset: laravel
Let's take a look at the code we have here:
- workflow_dispatch: which will also allow us to manually dipatch the workflow aginst the branch we want
- push: branches-ignore: meaning that it will be triggered on all of the branches except the branch we want to ignore
- job: steps: we use the checkout action and then the laravel-pint-action
Now if provided a pint.json (check pint github repo for more details about how to configure pint) file in the root, it will be used for configuration during the run of the Action.
This is looking good and it will work but the only problem is that the action above will only inform us about potential code formating issues. What we really want is to also go ahead and make the suggested changes and then push them to our branch. If you want to achieve such behaviour you have to use the laravel-pint action in combination with another action like git-auto-commit or create-pull-request action. In this case I think git-auto-commit is the better choice because if we are dealing with a pull request, after the linting we want a commit inside that pull request with the code fixed. Ok let's do just that:
1- name: Commit changes2 uses: stefanzweifel/git-auto-commit-action@v43 with:4 commit_message: PHP Linting (Pint)5 skip_fetch: true
Great! So we now have Laravel Pint running and linting our code and then push it back to our branch / PR so we can also see a diff with the changes and we can now make sure that all of the code in our main branch is following laravel's rules.
The full action / code should now look like this:
1name: PHP Linting (Pint)2on:3 workflow_dispatch:4 push:5 branches-ignore:6 - 'dependabot/npm_and_yarn/*'7jobs:8 phplint:9 runs-on: ubuntu-latest10 steps:11 - uses: actions/checkout@v312 with:13 fetch-depth: 214 - name: "laravel-pint"15 uses: firstname.lastname@example.org with:17 preset: laravel1819 - name: Commit changes20 uses: stefanzweifel/git-auto-commit-action@v421 with:22 commit_message: PHP Linting (Pint)23 skip_fetch: true
If everything went well, after we push our code we should be able to see pint's commit and it should look something like this:
And if you click on the commit, you should see a diff with the changes made by Pint.
PHP / Laravel Developer. Writing and maintaining @LaravelMagazine. Host of "The Laravel Magazine Podcast". Pronouns: vi/vim.