Continuous Integration: GitHub Actions and GitLab
In continuous integration (CI), run dresscode check, and GitHub shows every PHP code style violation
right at its line in the pull request. For GitLab and Jenkins there is the checkstyle format, a cache limits later
runs to the changed files, and the exit code distinguishes a violation from a check that never ran.
What to Run in CI
check, not fix. CI should say whether the code is fine, and the exit code 1 on a
violation fails the job; the developer makes the fix locally, where they can see it. If you want CI to offer the fix as well, run
fix followed by git diff --exit-code. The diff in the output of the job is then exactly what the
developer should commit.
The exit codes are fixed. 0 means the code is clean. 1 means a violation, a file that cannot be
parsed, or more warnings than --max-warnings allows. 2 means that a file failed (an exception in a rule,
rules stuck in a loop). 3 means an error on the command line or in the configuration. On 2 and
3, the job should fail more loudly than on 1: 3 means that nothing was checked, and
2 means that some file was not checked. The whole scale is described in Command Line.
GitHub Actions
name: Code style
on: [push, pull_request]
jobs:
dresscode:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: '8.4'
- run: composer create-project dresscode/dresscode temp/dresscode --no-progress
- run: temp/dresscode/bin/dresscode check
Nothing else is needed: DressCode recognizes that it is running as a step of GitHub Actions and switches the output to the
github format, in which every violation is an annotation. In the pull request, each annotation then appears right at
its line, with the message and the decision path. In the output of the job, each violation comes with the file, the line and the
decision, and the output ends with a summary.
Pin the version of DressCode, either with a version number in create-project
(dresscode/dresscode:1.3.0) or through composer.lock: a new decision of a set or a standard in a minor
version can start reporting something that used to pass, and that should happen during an update you make on purpose, not in
someone else's pull request. And watch the PHP version on the runner: DressCode itself needs PHP 8.4 to 8.6, even when it checks
a project written for an older version.
GitLab, Jenkins and Others
Where there is no native format, use checkstyle, which Jenkins, the cs2pr tool and most converters to
GitLab's Code Quality format understand, or json for your own processing:
dresscode:
script:
- composer create-project dresscode/dresscode temp/dresscode --no-progress
- temp/dresscode/bin/dresscode check -f checkstyle > dresscode.xml
artifacts:
when: always
paths: [dresscode.xml]
Neither of these formats changes its structure in a minor version, see Backward Compatibility. The console and
github formats, on the other hand, are for people and may change.
Projects with Types
In a project with typeAnalysis: phpstan, both DressCode and PHPStan belong among the development dependencies, so
that DressCode asks the version of PHPStan and the configuration the project works with (Types from PHPStan). The job then installs the dependencies of the
project and runs DressCode from vendor/bin:
- run: composer install --no-progress
- run: vendor/bin/dresscode check --no-cache
The --no-cache in the example is there because with types the cache would save nothing in CI anyway. A verdict
then also depends on what the other files of the project declare, so the cache throws away everything it holds as soon as any file
in the analyzed paths, phpstan.neon or the installed packages change. In CI, where a fresh checkout gives every file
a new time, that happens on every run. The next section therefore applies to a project without types.
Cache Between Runs
DressCode keeps the fingerprints of files that passed clean in the system temporary directory. In CI, where every run starts from scratch, this does not help. But when you point the cache to a directory in the project and let CI keep that directory, the second run checks only the changed files:
cacheDir: temp/dresscode-cache
- uses: actions/cache@v4
with:
path: temp/dresscode-cache
key: dresscode-${{ github.sha }}
restore-keys: dresscode-
actions/cache does not save a cache again at the end of the job when its key already exists, so with a fixed key
the cache would stay stuck at the state of the first run. That is why the key contains the commit: every run saves a new cache,
and restore-keys restores the newest one at the start of the next run. After you change the configuration, update the
tool or edit your own rule, the files are checked again automatically, see Cache and Parallel Runs.
Parallel Runs
The number of worker processes follows the number of CPUs of the runner, so on a two-core runner there is nothing to tune.
Where a runner shares its machine with other jobs, --jobs 4 keeps the run from taking every core.