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:
- 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/[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:
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
← Previous post
The Beauty of Single Action ControllersNext post →
The New Blade Icons ReleaseAbout Me
I'm a software engineer from Antwerp, Belgium. I work as one of the core team members of Laravel, the popular PHP framework. I'm also the creator of Blade UI Kit, a set of renderless components to utilise in your Laravel Blade views.
My passions are open-source, building communities, managing software teams, and creating quality and maintainable products.
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: @driesvints.