Repository navigation
243 lines (229 loc) · 10.4 KB
/
Copy pathphpstan.yml
File metadata and controls
243 lines (229 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
name: PHPStan
# Runs PHPStan for a plugin repository, with the dependencies and PHPStan's result cache cached.
#
# A reusable workflow. A plugin repository calls it from a thin workflow of its own; copy
# templates/workflows/phpstan.yml.
#
# The repository runs the analysis with its own Composer script, `composer phpstan` by default, so
# memory limits and flags stay in composer.json and the local command is the CI command.
#
# PHPStan only reads its result cache from `tmpDir` in its config, so the config has to point at
# the path this workflow caches:
#
# parameters:
# tmpDir: .cache/phpstan
#
# A pull request that changes none of the `changed-files` globs, and not the caller workflow file
# itself, skips the analysis, and the check still ends green. A push, a manual run and a pull
# request with no `changed-files` always analyse.
#
# Plugins PHPStan has to scan that are not in this repository are downloaded next to the checkout
# (`../<name>`), so `scanDirectories` in the PHPStan config reaches them as `../<name>`. An
# `owner/name` entry is a GitHub repository, its latest release; a bare slug is a WordPress.org
# plugin. A GitHub repository that has a composer.json gets `composer install` there.
#
# The caller grants the token: `contents: read` to check out, `pull-requests: read` to list the
# files a pull request changes. A called workflow cannot raise what the caller gave it.
# `concurrency` is the caller's too: here it would be evaluated with the caller's context.
#
# Draft pulls whose branch starts with a `skip-draft-prefixes` entry do not run until they are
# marked ready, so the caller has to listen for `ready_for_review`.
#
# Run it locally: `composer phpstan`
on:
workflow_call:
inputs:
php-version:
description: >-
PHP that installs dependencies and runs PHPStan. Use the oldest version the plugin
supports, so Composer resolves packages that still run there.
required: false
type: string
default: '7.4'
php-extensions:
description: 'PHP extensions setup-php installs, comma-separated.'
required: false
type: string
default: 'mbstring, intl, gd'
command:
description: 'Bash that runs the analysis from the repository root.'
required: false
type: string
default: composer phpstan
runs-on:
description: 'Runner label, for example `ubuntu-latest`.'
required: false
type: string
default: blacksmith-2vcpu-ubuntu-2404
changed-files:
description: >-
Globs, one per line. A pull request runs PHPStan only when it changes a file one of them
matches. `*` also matches `/`, so `*.php` is every PHP file. An empty string (`''`) always runs.
required: false
type: string
default: |
*.php
*phpstan*.neon*
*composer.json
*composer.lock
skip-draft-prefixes:
description: >-
Branch prefixes, one per line, whose draft pull requests skip PHPStan. A draft of
`release/1.2` skips when a prefix is `release/`. An empty string (`''`) means drafts run.
required: false
type: string
default: ''
dependency-cache-paths:
description: >-
Directories cached between runs, one per line. A repository that rewrites dependencies
after install (Strauss, generated test actors) lists those too, so PHPStan sees the same
files on every run and keeps its result cache.
required: false
type: string
default: vendor
dependency-cache-hash-files:
description: 'Git pathspecs, one per line, whose contents are part of the dependency cache key.'
required: false
type: string
default: |
composer.json
composer.lock
post-install-command:
description: >-
Bash that runs after `composer install` when the dependency cache missed. An empty string
(`''`) runs nothing. Example: `vendor/bin/codecept build`.
required: false
type: string
default: ''
plugins:
description: >-
Plugins PHPStan scans, one per line, downloaded to `../<name>`. `owner/name` is a GitHub
repository's latest release (a private one needs the `github-token` secret); a bare slug
is a WordPress.org plugin. An empty string (`''`) downloads nothing.
required: false
type: string
default: ''
secrets:
github-token:
description: >-
Token for private GitHub release downloads and for Composer. An empty string (`''`) uses
the workflow token, which cannot read another private repository. Fork pull requests get
no secrets.
required: false
jobs:
# One job, so the check is green when the analysis is skipped and no second runner starts to
# decide that. Every step after `changes` waits for its answer.
phpstan:
name: PHPStan
runs-on: ${{ inputs.runs-on }}
timeout-minutes: 30
# Step conditions cannot read secrets, so the job carries whether a token was passed.
env:
HAS_GITHUB_TOKEN: ${{ secrets.github-token != '' }}
permissions:
contents: read
pull-requests: read
steps:
# Without a checkout: the files of a pull request come from the API. `$/` is this repository
# at the commit that runs this workflow, so the action is always the same revision as the
# workflow. It needs a runner of version 2.336.0 or newer, which `runs-on` has to give. Not
# ./, which in a called workflow resolves against the caller's repository.
- name: Check whether this is a draft to skip
id: draft
uses: $/.github/actions/skip-drafts
with:
prefixes: ${{ inputs.skip-draft-prefixes }}
# A skipped draft leaves `changed` empty, so every step below that asks for 'true' is skipped.
- name: Check the files the pull request changes
id: changes
if: steps.draft.outputs.skip != 'true'
uses: $/.github/actions/changed-files
with:
changed-files: ${{ inputs.changed-files }}
- name: Check out the repository
if: steps.changes.outputs.changed == 'true'
uses: actions/checkout@v7
with:
persist-credentials: false
- name: Set up PHP
if: steps.changes.outputs.changed == 'true'
uses: shivammathur/setup-php@v2
with:
php-version: ${{ inputs.php-version }}
extensions: ${{ inputs.php-extensions }}
coverage: none
tools: composer:v2
- name: Download the plugins PHPStan scans
if: steps.changes.outputs.changed == 'true' && (inputs.plugins != '')
uses: $/.github/actions/download-plugins
with:
plugins: ${{ inputs.plugins }}
token: ${{ secrets.github-token }}
- name: Hash the dependency files
id: deps-hash
if: steps.changes.outputs.changed == 'true'
env:
HASH_FILES: ${{ inputs.dependency-cache-hash-files }}
run: |
set -euo pipefail
set -f # git expands the pathspecs
# shellcheck disable=SC2086 # one pathspec per word
hash=$(git ls-files -z -- $HASH_FILES | sort -z | xargs -0 -r sha256sum | sha256sum | cut -d' ' -f1)
echo "hash=$hash" >> "$GITHUB_OUTPUT"
# The key names the branch and falls back to any branch's cache, so a new pull request starts
# from main's dependencies instead of from nothing.
- name: Restore the dependency cache
id: deps-cache
if: steps.changes.outputs.changed == 'true'
uses: actions/cache/restore@v6
with:
path: ${{ inputs.dependency-cache-paths }}
key: v1-deps-${{ runner.os }}-php${{ inputs.php-version }}-${{ github.ref_name }}-${{ steps.deps-hash.outputs.hash }}
restore-keys: |
v1-deps-${{ runner.os }}-php${{ inputs.php-version }}-${{ github.ref_name }}-
v1-deps-${{ runner.os }}-php${{ inputs.php-version }}-
# A partial hit restored an older vendor, and `composer install` brings it up to date.
- name: Install the dependencies
id: install
if: steps.changes.outputs.changed == 'true' && steps.deps-cache.outputs.cache-hit != 'true'
env:
POST_INSTALL_COMMAND: ${{ inputs.post-install-command }}
COMPOSER_AUTH: ${{ secrets.github-token != '' && format('{{"github-oauth":{{"github.com":"{0}"}}}}', secrets.github-token) || '' }}
run: |
set -euo pipefail
composer install --no-interaction --no-progress --prefer-dist
if [ -n "$POST_INSTALL_COMMAND" ]; then
bash -eo pipefail -c "$POST_INSTALL_COMMAND"
fi
# Saved before the analysis, so a run that finds PHPStan errors still leaves the cache.
# A public caller with a token installs private packages into vendor, and a fork's pull
# request can restore the base branch's cache, so that combination does not save one.
- name: Save the dependency cache
if: steps.install.outcome == 'success' && !(env.HAS_GITHUB_TOKEN == 'true' && github.event.repository.private == false)
uses: actions/cache/save@v6
with:
path: ${{ inputs.dependency-cache-paths }}
key: ${{ steps.deps-cache.outputs.cache-primary-key }}
- name: Restore the PHPStan cache
id: phpstan-cache
if: steps.changes.outputs.changed == 'true'
uses: actions/cache/restore@v6
with:
path: .cache/phpstan
key: v1-phpstan-${{ runner.os }}-php${{ inputs.php-version }}-${{ github.ref_name }}-${{ github.run_id }}
restore-keys: |
v1-phpstan-${{ runner.os }}-php${{ inputs.php-version }}-${{ github.ref_name }}-
v1-phpstan-${{ runner.os }}-php${{ inputs.php-version }}-
- name: Run PHPStan
if: steps.changes.outputs.changed == 'true'
env:
ANALYSIS_COMMAND: ${{ inputs.command }}
run: bash -eo pipefail -c "$ANALYSIS_COMMAND"
# Saved when PHPStan reports errors too: it still writes the cache, and the next run reads it.
# The run id in the key means a save never collides with an older cache.
- name: Save the PHPStan cache
if: ${{ !cancelled() && steps.phpstan-cache.outcome == 'success' }}
uses: actions/cache/save@v6
with:
path: .cache/phpstan
key: ${{ steps.phpstan-cache.outputs.cache-primary-key }}