Fix styling and formatting of PHP files using Github Actions and Laravel Pint

Introduction

While working on a team consisting of multiple developers, it’s really important to have a predefined set of rules about different aspects of the project. Whether it is about the authority to review and merge the PRs, project planning, or providing access to the server.

So, consistency and proper workflow are really important while working in a team of multiple developers. And, one more important aspect that needs to have consistency is code formatting and implementation of fixed styling rules.

In this article, we will be looking at how we can format our code with a recently released formatter called Laravel Pint which will automatically format our code on every pull request to a certain branch.


Prerequisites

For this workflow to work, we first need to install Laravel Pint, and we can install it with a composer command like so:


composer require laravel/pint --dev

Defining the Workflow

Once we’ve installed the package, the next thing is to define the workflow. So, let’s take a look at the workflow and I’ll explain each section as we go along:


// .github/format_code.yml

name: Fix Code Style

on:
  pull_request:
    branches: [dev]

jobs:
  laravel-pint:
    runs-on: ubuntu-latest

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

      - name: Composer Install
        run: composer install

      - name: Run Laravel Pint
        run: ./vendor/bin/pint

      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Apply Laravel Pint changes
        env:
          GITHUB_TOKEN: ${{ secrets.PERSONAL_TOKEN }}

The first thing to note about this file is that it should be placed inside the .github folder of the root directory with an extension of .yml, otherwise it won’t work.


// .github/format_code.yml

name: Fix Code Style

on:
  pull_request:
    branches: [dev]

The first section of the action defines the name of the action, with the name: prefix, and the on: section defines what causes this action to trigger. In our case, the action will trigger whenever anyone makes a pull_request to a dev branch. You can replace dev with any valid branch name you like and to check out the documentation and what other trigger options are available, please click here.



jobs:
  laravel-pint:
    runs-on: ubuntu-latest

The next section is the jobs section. Here, we can define the tasks that we need to run. In our case, we’ve defined a job called laravel-pint, we can define multiple jobs here, and all of them should be defined at the same level (since it’s a yml file) and Github Actions will run them one by one as per the order. In this case, we’re only defining one job that is laravel-pint.

The next parameter passed is runs-on: which indicates where this action will be executed, which is an Ubuntu-based system in our case.



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

The next parameter passed is the steps: prefix. Here, we will define the necessary steps to accomplish the task. The name prefix, as the name says is to indicate the name of the task, and the uses: prefix is to indicate any third-party action that this action uses. And in this case, we will be using the checkout action to checkout (or clone) our code to the previously defined OS i.e Ubuntu.



- name: Composer Install
        run: composer install

The next step is to install the composer packages since we’ve only cloned the repo on our previous step, and in this step, we install the composer packages by running composer install


      - name: Run Laravel Pint
        run: ./vendor/bin/pint

And then, we will be running the  ./vendor/bin/pint command to format all of the PHP files of our repo.



      - name: Commit changes
        uses: stefanzweifel/git-auto-commit-action@v4
        with:
          commit_message: Apply Laravel Pint changes
        env:
          GITHUB_TOKEN: ${{ secrets.PERSONAL_TOKEN }}

And finally, comes the main part. Until this time, we have installed packages and formatted files on the virtual system. It does perform all the tasks but how do we reflect those changes on our repo? So, to make those changes to our repo, we will be using an auto-commit action called stefanzweifel/git-auto-commit-action and v4 is the latest at the time of writing this article.

But, before using this action, we need to set up a few stuff.

So, head over to this link (make sure you’re logged in to Github), to create a new token, give it a proper name, and make sure to give repo and workflow scopes, select expiration date as per your need. And then, click on Generate Token

Copy the generated token and head over to the Settings page of your Repository, inside the Secrets tab, click on the Actions link.

Click on New Repository Secret, give the name of the secret as PERSONAL_TOKEN and paste the copied secret token value.

And now that everything is set up and ready, the above code will use that auto-commit-action and using that PERSONAL_TOKEN, commit to our repo with the message applied in the commit_message prefix.

The last thing that we need to do is commit and push this file to our repository, and the next time anyone makes a pull request to our repo on the dev branch, we can see that a new action is running which will perform all of the specified tasks, format the files and commit to our repository, and along with that, we can also see what files this action has changed.

Reference Links

Tapan Sharma: