Skip to main content

Command Palette

Search for a command to run...

Setting up PHPCS in GitLab only on changed files

Published
1 min read
Setting up PHPCS in GitLab only on changed files
G

I am a self-taught developer currently in Salt Lake City. Currently, work in Laravel but have experience in plain PHP, PERL, and CakePHP.

Entrepreneurial VP of tech and CTO with a strong grasp of both the business and engineering. PHP developer for 10 years with experience in CakePHP and Laravel

I wanted to save some time on our runners in GitLab and decided to only run phpcs on changed files for that merge request. I also wanted it to run as a git commit hook to try and prevent bad code from even getting pushed up.

What I came up with was to add two shell scripts to the repo under the folder bin.

changed.sh (chmod u+x)

#!/bin/bash

git fetch origin master
git diff --name-status origin/master | grep '\.php$' | grep -v "^[RD]" | awk '{ print $2 }'

Add the follow to your .gitlab-ci.yml

codestyle:
  stage: testing
  needs: ["composer"]
  image: lorisleiva/laravel-docker
  script:
    - files=`sh bin/changed.sh`
    - phpcs --config-set ignore_warnings_on_exit 1
    - if [ ! -z "$files" ]; then echo $files | xargs phpcs; fi

changed.sh could just go inside the yml file, but I like using it before deploys.

Thanks, for reading, please let me know if you found this helpful or have questions over on Twitter: https://twitter.com/guywarner801

More from this blog

Laravel & PHP by Guy Warner

18 posts

Self taught programmer -> Freelancer -> Senior Developer -> CTO

Laravel, PHP and all that comes with it.