Run prettier or php-cs-fixer with GitHub Actions

Stefan Zweifel shares his GitHub Actions Workflows to run prettier and php-cs-fixer on his repos:

Over the past few weeks I’ve added a handful of workflows to my projects.

One workflow is really like, is to run prettier and php-cs-fixer to automatically format my code and commit the fixed files back to the repository.

Here’s the contents of the prettier workflow for example (.github/workflows/format_prettier):

name: Format (prettier)

    - '**.css'
    - '**.js'
    - '**.vue'

    runs-on: ubuntu-latest
    - uses: actions/checkout@v1

    - name: Install
      run: yarn install
        CI: true

    - name: Run prettier
      run: yarn run prettier --write 'src/**/*.{css,js,vue}'

    - uses: stefanzweifel/git-auto-commit-action@v2.1.0
        commit_message: Apply prettier changes
        branch: ${{ github.head_ref }}
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Upon each PR the workflow spins up an ubuntu machine which:

  1. Checks out the code
  2. Installs the dependencies (given that prettier is listed as a dev dependency in package.json)
  3. Runs the linter on certain files, with autofix enabled
  4. Commits all changes made by the linter

The php-cs-fixer works in a similar way.

Run prettier or php-cs-fixer with GitHub Actions →

🤩 There’s more cool things you can do with GitHub Actions. Automatically compressing (image) assets for example is a really neat workflow. You can even use GitHub Actions to schedule deploys of your site.

🤔 Looking to set up prettier and/or php-cs-fixer? Look no further.

Freek on Twitter

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.