Skip to main content

Setting up PHPCS in GitLab only on changed files

Updated 1 min readMarkdown
Part of seriesLaravel in Gitlab
Setting up PHPCS in GitLab only on changed files

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

About the author

  • 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

No comments yet. Be the first to comment.

More from this blog