Code Style formatting with Github Actions
First published on May 2, 2020 · Last updated on May 10, 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:
- Tools to automatically format PHP, JavaScript and CSS files
- Run prettier or php-cs-fixer with GitHub Actions
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/checkout@v2
- 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/checkout@v2
- 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:
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 Belgium. I work as one of the core team members of Laravel, the popular PHP framework.
I'm also the creator of Eventy, a platform that connects the dots between user groups, speakers, conferences and attendees.
My passions are open-source, growing communities, and building products that people love.
