Code Style formatting with Github Actions

Published on May 2, 2020

Yesterday I dived into Prettier and PHP CS Fixer for my projects. So I decided to write a quick blog post on the subject. I managed to get a great quick start by reading these two excellent blog posts by Freek Van der Herten and Stefan Zweifer on the subject:

In the end, I decided to combine the two in a single workflow but in two jobs:

name: Code Style

on: [push]

jobs:
  php-cs-fixer:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/[email protected]

      - name: Run PHP CS Fixer
        uses: docker://oskarstark/php-cs-fixer-ga
        with:
          args: --config=.php_cs.dist --allow-risky=yes

      - name: Commit changes
        uses: stefanzweifel/[email protected]
        with:
          commit_message: Fix styling
          branch: ${{ github.head_ref }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  prettier:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/[email protected]

      - name: Install NPM dependencies
        run: npm ci

      - name: Run Prettier
        run: npm run format

      - name: Commit changes
        uses: stefanzweifel/[email protected]
        with:
          commit_message: Apply Prettier changes
          branch: ${{ github.head_ref }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

The benefit of this is that your Github Actions will be neatly grouped in Pull Requests and the Github Actions overview:

Github Actions

Also, we only run them on push so any code that gets merged with incorrect styling will be automatically fixed. Here's the full Pull Request for Laravel.io where we added the workflow and set up Prettier and PHP CS Fixer: https://github.com/laravelio/laravel.io/pull/520

About Me

I'm a software engineer from Antwerp, Belgium. I work as one of the core team members of Laravel, the popular PHP framework.

My passions are open-source, building communities, managing software teams, and creating quality and maintainable products.

I'm currently building Eventy, an app for user group leaders, conference organisers, speakers, attendees, sponsors and venue owners.

Together with my buddy Rias I organize events for Full Stack Belgium in the cities of Antwerp and Ghent. And together with my buddy Freek I organise Full Stack Europe, the conference for the whole team.

Follow me on Twitter at @driesvints where I also share some Dev Tips from time to time.

© Dries Vints