-
Notifications
You must be signed in to change notification settings - Fork 17
/
action.yaml
426 lines (375 loc) · 14.4 KB
/
action.yaml
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
name: Trunk Check
author: trunk.io
description: The official trunk.io GitHub action
branding:
icon: check
color: green
inputs:
trunk-path:
description:
Path to Trunk Launcher. If not provided, we'll look for it the repo root, `.trunk/bin` and
`tools/`. If it can't be found anywhere and is not provided explicitly, we'll download it on
demand.
required: false
label:
description:
Label to append to the check run name (useful if running Trunk Check on multiple platforms)
deprecationMessage: "Use `arguments: --github-label=value` instead."
required: false
arguments:
description: Extra arguments to pass to trunk
required: false
check-mode:
description:
Trunk check mode. Leave unset to autodetect. Set to 'all' to check the entire repository. Set
to 'populate_cache_only' in a dedicated workflow to populate the GitHub Actions cache with
Trunk artifacts (see docs for more details).
required: false
check-all-mode:
description:
If set to "hold-the-line", computes new/existing issues by comparing to previous upload.
required: false
default: ""
check-run-id:
description: Check run ID. If set, posts annotations using CheckService.
required: false
cache:
description:
Cache trunk downloads and results between runs. Caching is only needed when using ephemeral CI
runners.
required: false
default: "true"
cache-key:
description:
A key unique to the repo/branch this action is being run on (e.g. the repo name and branch)
required: false
post-init:
description: Steps to run after auto-init / before check
required: false
github-token:
description: For overriding github.token
required: false
trunk-token:
description:
You can find a per-repo API token in the Trunk web app settings. This will cause results to be
uploaded to the Trunk web app if this job is a scheduled job running on a branch, or if
`check-mode` is set to 'all'.
required: false
upload-series:
description:
Upload series name, for when `trunk-token` is provided. If not provided, we'll use the branch
name.
required: false
save-annotations:
description:
Save annotations as an artifact instead of posting them from this action. This is neccesary if
your repository will be used with forks as they will will not have permissions to upload
annotations. By default forks will be auto-detected.
required: false
default: auto
post-annotations:
description:
Post annotations previously uploaded with the `save-annotations` option (specific to posting
annotations from forks).
required: false
default: "false"
setup-deps:
description:
Install dependencies for trunk check that the trunk CLI does not manage. This is only
necessary if you have Node dependencies in your package.json that your Node linters need (e.g.
eslint dependencies, or @types packages).
required: false
default: "false"
debug:
description: Internal use only
required: false
default: "false"
timeout-seconds:
description:
Timeout seconds before we kill the long running trunk check process via unix timeout command.
Default setting of 0 seconds disables the timeout.
required: false
default: 0
cat-trunk-debug-logs:
description: Option to cat .trunk/logs/cli.log && .trunk/logs/daemon.log
required: false
default: false
lfs-checkout:
description: Option to checkout LFS files
required: false
default: true
runs:
using: composite
steps:
- name: Set up inputs
shell: bash
run: |
cat >>$GITHUB_ENV <<EOF
GITHUB_TOKEN=${{ github.token }}
TRUNK_LAUNCHER_QUIET=false
EOF
# First arg is field to fetch, second arg is default value or empty
payload() {
if [ $# -lt 2 ]; then
DEFAULT_VALUE=empty
else
DEFAULT_VALUE=\"$2\"
fi
if command -v jq >/dev/null; then
jq -r ".inputs.payload | fromjson | .$1 // ${DEFAULT_VALUE}" ${TEST_GITHUB_EVENT_PATH:-${GITHUB_EVENT_PATH}}
else
echo "::error::jq not installed on system!"
exit 1
fi
}
# Every inputs.field should be referenced as INPUT_FIELD later in the action. This allows
# the field to be set either as an argument to the github action or via inputs.json.
if [[ "${{ inputs.check-mode }}" == "payload" ]]; then
INPUT_GITHUB_TOKEN=$(payload githubToken)
INPUT_TRUNK_TOKEN=$(payload trunkToken)
echo "::add-mask::${INPUT_GITHUB_TOKEN}"
echo "::add-mask::${INPUT_TRUNK_TOKEN}"
cat >>$GITHUB_ENV <<EOF
INPUT_GITHUB_TOKEN=${INPUT_GITHUB_TOKEN}
INPUT_TRUNK_TOKEN=${INPUT_TRUNK_TOKEN}
TRUNK_TOKEN=${INPUT_TRUNK_TOKEN}
TRUNK_API_ADDRESS=$(payload trunkApiAddress)
GITHUB_EVENT_PULL_REQUEST_BASE_REPO_OWNER=$(payload pullRequest.base.repo.owner.login)
GITHUB_EVENT_PULL_REQUEST_BASE_REPO_NAME=$(payload pullRequest.base.repo.name)
GITHUB_EVENT_PULL_REQUEST_BASE_SHA=$(payload pullRequest.base.sha)
GITHUB_EVENT_PULL_REQUEST_HEAD_REPO_FORK=$(payload pullRequest.head.repo.fork)
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA=$(payload pullRequest.head.sha)
GITHUB_EVENT_PULL_REQUEST_NUMBER=$(payload pullRequest.number)
INPUT_ARGUMENTS=$(payload arguments)
INPUT_CACHE=$(payload cache)
INPUT_CACHE_KEY=$(payload cacheKey)
INPUT_CACHE_PATH=$(payload cachePath)
INPUT_CHECK_ALL_MODE=$(payload checkAllMode)
INPUT_CHECK_MODE=$(payload checkMode)
INPUT_CHECK_RUN_ID=$(payload checkRunId)
INPUT_DEBUG=$(payload debug)
INPUT_GITHUB_REF_NAME=$(payload targetRefName)
INPUT_LABEL=$(payload label)
INPUT_SETUP_CACHE_KEY=$(payload setupCacheKey)
INPUT_SETUP_DEPS=$(payload setupDeps)
INPUT_TARGET_CHECKOUT=$(payload targetCheckout)
INPUT_TARGET_CHECKOUT_REF=$(payload targetCheckoutRef)
INPUT_TRUNK_PATH=$(payload trunkPath)
INPUT_UPLOAD_LANDING_STATE=$(payload uploadLandingState)
INPUT_UPLOAD_SERIES=$(payload uploadSeries)
INPUT_UPLOAD_ID=$(payload uploadId)
INPUT_AUTOFIX_AND_PUSH=$(payload autofixAndPush)
INPUT_LFS_CHECKOUT=$(payload lfsCheckout true)
EOF
else
cat >>$GITHUB_ENV <<EOF
INPUT_GITHUB_TOKEN=${{ inputs.github-token }}
INPUT_TRUNK_TOKEN=${{ inputs.trunk-token }}
TRUNK_TOKEN=${{ inputs.trunk-token }}
GITHUB_EVENT_PULL_REQUEST_BASE_SHA=${{ github.event.pull_request.base.sha }}
GITHUB_EVENT_PULL_REQUEST_HEAD_REPO_FORK=${{ github.event.pull_request.head.repo.fork }}
GITHUB_EVENT_PULL_REQUEST_HEAD_SHA=${{ github.event.pull_request.head.sha }}
GITHUB_EVENT_PULL_REQUEST_NUMBER=${{ github.event.pull_request.number }}
GITHUB_REF_NAME=${{ github.ref_name }}
INPUT_ARGUMENTS=${{ inputs.arguments }}
INPUT_CACHE=${{ inputs.cache }}
INPUT_CACHE_KEY=trunk-${{ inputs.cache-key }}-${{ runner.os }}-${{ hashFiles('.trunk/trunk.yaml') }}
INPUT_CACHE_PATH=~/.cache/trunk
INPUT_CAT_TRUNK_DEBUG_LOGS=${{ inputs.cat-trunk-debug-logs }}
INPUT_CHECK_ALL_MODE=${{ inputs.check-all-mode }}
INPUT_CHECK_MODE=${{ inputs.check-mode }}
INPUT_CHECK_RUN_ID=${{ inputs.check-run-id }}
INPUT_DEBUG=${{ inputs.debug }}
INPUT_GITHUB_REF_NAME=${{ github.ref_name }}
INPUT_LABEL=${{ inputs.label }}
INPUT_SETUP_CACHE_KEY=${{ inputs.cache-key }}
INPUT_SETUP_DEPS=${{ inputs.setup-deps }}
INPUT_TARGET_CHECKOUT=
INPUT_TARGET_CHECKOUT_REF=
INPUT_TIMEOUT_SECONDS=${{ inputs.timeout-seconds }}
INPUT_TRUNK_PATH=${{ inputs.trunk-path }}
INPUT_UPLOAD_LANDING_STATE=false
INPUT_UPLOAD_SERIES=${{ inputs.upload-series }}
INPUT_UPLOAD_ID=
INPUT_AUTOFIX_AND_PUSH=
INPUT_LFS_CHECKOUT=${{ inputs.lfs-checkout }}
EOF
fi
- name: Checkout
if: env.INPUT_TARGET_CHECKOUT
uses: actions/checkout@v4
with:
lfs: ${{ env.INPUT_LFS_CHECKOUT && inputs.lfs-checkout }}
submodules: recursive
repository: ${{ env.INPUT_TARGET_CHECKOUT }}
ref: ${{ env.INPUT_TARGET_CHECKOUT_REF }}
token: ${{ env.INPUT_GITHUB_TOKEN }}
- name: Locate trunk
shell: bash
run: ${GITHUB_ACTION_PATH}/setup/locate_trunk.sh
- name: Determine check mode
shell: bash
run: |
# Determine check mode
${GITHUB_ACTION_PATH}/determine_check_mode.sh
- name: Init on-demand
shell: bash
run: |
if [[ ! -e .trunk/trunk.yaml ]]; then
${TRUNK_PATH:-trunk} --ci init
echo "INITIALIZED_TRUNK=true" >>$GITHUB_ENV
fi
- name: Detect setup strategy
shell: bash
run: |
if [[ -e .trunk/setup-ci ]]; then
echo "INPUT_SETUP_DEPS=true" >>$GITHUB_ENV
else
mkdir -p .trunk
ln -s ${{ github.action_path }}/setup-env .trunk/setup-ci
echo .trunk/setup-ci >>.git/info/exclude
fi
- name: Set up env
uses: ./.trunk/setup-ci
if: env.INPUT_SETUP_DEPS == 'true'
with:
cache-key: ${{ env.INPUT_SETUP_CACHE_KEY }}
- name: Post-init steps
if: inputs.post-init
shell: bash
run: ${{ inputs.post-init }}
- name: Cache Linters/Formatters
if: env.TRUNK_CHECK_MODE != 'none' && env.INPUT_CACHE == 'true'
uses: actions/cache@v4
with:
path: ${{ env.INPUT_CACHE_PATH }}
key: ${{ env.INPUT_CACHE_KEY }}
- name: Run trunk check on pull request
if: env.TRUNK_CHECK_MODE == 'pull_request'
shell: bash
run: |
# Run 'trunk check' on pull request
if [[ "${{ inputs.timeout-seconds }}" != "0" ]]; then
timeout ${{ inputs.timeout-seconds }} ${GITHUB_ACTION_PATH}/pull_request.sh
else
${GITHUB_ACTION_PATH}/pull_request.sh
fi
env:
INPUT_SAVE_ANNOTATIONS: ${{ inputs.save-annotations }}
- name: Run trunk check on push
if: env.TRUNK_CHECK_MODE == 'push'
shell: bash
run: |
# Run 'trunk check' on push
if [[ "${{ inputs.timeout-seconds }}" != "0" ]]; then
timeout ${{ inputs.timeout-seconds }} ${GITHUB_ACTION_PATH}/push.sh
else
${GITHUB_ACTION_PATH}/push.sh
fi
env:
GITHUB_EVENT_AFTER: ${{ env.GITHUB_EVENT_AFTER || github.event.after }}
GITHUB_EVENT_BEFORE: ${{ env.GITHUB_EVENT_BEFORE || github.event.before }}
- name: Run trunk check on all
if: env.TRUNK_CHECK_MODE == 'all'
shell: bash
run: |
# Run 'trunk check' on all
if [[ "${{ inputs.timeout-seconds }}" != "0" ]]; then
timeout ${{ inputs.timeout-seconds }} ${GITHUB_ACTION_PATH}/all.sh
else
${GITHUB_ACTION_PATH}/all.sh
fi
- name: Run trunk check on Trunk Merge
if: env.TRUNK_CHECK_MODE == 'trunk_merge'
shell: bash
run: |
# Run 'trunk check' on Trunk Merge
if [[ "${{ inputs.timeout-seconds }}" != "0" ]]; then
timeout ${{ inputs.timeout-seconds }} ${GITHUB_ACTION_PATH}/trunk_merge.sh
else
${GITHUB_ACTION_PATH}/trunk_merge.sh
fi
- name: Cat Trunk Cli / Daemon logs
if: always() && inputs.cat-trunk-debug-logs == 'true'
shell: bash
run: |
echo "::group::.trunk/logs/cli.log"
if [[ -f .trunk/logs/cli.log ]]; then
cat .trunk/logs/cli.log
else
echo ".trunk/logs/cli.log doesn't exist"
fi
echo "::endgroup::"
echo "::group::.trunk/logs/daemon.log"
if [[ -f .trunk/logs/daemon.log ]]; then
cat .trunk/logs/daemon.log
else
echo ".trunk/logs/daemon.log doesn't exist"
fi
echo "::endgroup::"
- name: Run trunk install to populate the GitHub Actions cache
if: env.TRUNK_CHECK_MODE == 'populate_cache_only'
shell: bash
run: |
# Run 'trunk install' to populate the GitHub Actions cache
${GITHUB_ACTION_PATH}/populate_cache_only.sh
- name: Upload annotations artifact
if: always() && env.TRUNK_UPLOAD_ANNOTATIONS == 'true'
uses: actions/upload-artifact@v4
with:
name: trunk-annotations
overwrite: true
path: ${{ env.TRUNK_TMPDIR }}/annotations.bin
- name: Download annotations artifact
if: inputs.post-annotations == 'true'
uses: actions/github-script@v7
with:
# TODO(chris): We can't use the official download artifact action yet: https://github.com/actions/download-artifact/issues/172
script: |
let artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{ github.event.workflow_run.id }},
});
let matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "trunk-annotations"
})[0];
if (matchArtifact) {
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
let fs = require('fs');
fs.writeFileSync('${{ env.TRUNK_TMPDIR }}/annotations.zip', Buffer.from(download.data));
}
- name: Unpack annotations artifact
if: inputs.post-annotations == 'true'
run: |
# Unpack annotations artifact
cd ${{ env.TRUNK_TMPDIR }} && unzip annotations.zip
shell: bash
- name: Post annotations
if: inputs.post-annotations == 'true'
shell: bash
run: |
# Post annotations
${GITHUB_ACTION_PATH}/annotate.sh
env:
GITHUB_EVENT_WORKFLOW_RUN_HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
- name: Upload landing state
if: env.INPUT_UPLOAD_LANDING_STATE == 'true'
continue-on-error: true
uses: actions/upload-artifact@v4
with:
name: landing-state.json
path: .trunk/landing-state.json
overwrite: true
if-no-files-found: warn
- name: Cleanup temporary files
if: always()
shell: bash
run: |
# Cleanup temporary files
${GITHUB_ACTION_PATH}/cleanup.sh