NERDY

Gitlab CI phpcs optimization

2019-06-02

At my company we use Gitlab CI and for most projects we use phpcs to scan our codebase. The stage might take more time then is necessary because it’s scanning all files instead of just the ones that have changed. Because of this we are now using the following line to make it a lot faster (the slowest part is spinning up the docker container).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
codestyle:
image: [some phpcs docker image]
stage: linting
tags:
- docker
script:
- git diff origin/development... --name-only > git-diff-files.txt
- cat git-diff-files.txt
- phpcs
--error-severity=1
--warning-severity=1
--cache=.phpcs-cache
--ignore="*.min.js,*.min.css"
--report=code
git-diff-files.txt
except:
- development
- tags

The git diff will output a list of files which are saved to a temporary text file. And to check whether the diff actually outputs something we cat the file.
phpcs accepts a file with a filelisting as last argument.