-
Notifications
You must be signed in to change notification settings - Fork 14
380 lines (352 loc) · 13.5 KB
/
codeql-analysis.yaml
File metadata and controls
380 lines (352 loc) · 13.5 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
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
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: "CodeQL Analysis"
"on":
push:
branches: [main, develop]
pull_request:
schedule:
- cron: "0 3 * * 0" # weekly (UTC) — adjust as needed
workflow_dispatch:
inputs:
ref:
description: "The branch, ref, or SHA to checkout. Defaults to the repository's default branch."
required: false
type: string
workflow_call:
inputs:
checkout-path:
description: "Path to check out code to"
required: false
type: string
build-path:
description: "Path for build artifacts"
required: false
type: string
language-matrix:
description: "JSON array of languages to analyze"
required: false
type: string
pr-number:
description: "PR number if run in PR context"
required: false
type: string
pr-head-repo:
description: "The full name of the PR head repository"
required: false
type: string
pr-base-repo:
description: "The full name of the PR base repository"
required: false
type: string
pr-base-sha:
description: "Base SHA of the PR for relevance check"
required: false
type: string
pr-head-sha:
description: "Head SHA of the PR for relevance check"
required: false
type: string
ref:
description: "The branch, ref, or SHA to checkout"
required: false
type: string
repo:
description: "The repository to checkout from"
required: false
type: string
permissions:
actions: read
contents: read
security-events: write
env:
BUILD_TYPE: RelWithDebInfo
CPP_COMPILER: g++
jobs:
setup:
runs-on: ubuntu-latest
outputs:
is_act: ${{ steps.setup.outputs.is_act }}
ref: ${{ steps.setup.outputs.ref }}
repo: ${{ steps.setup.outputs.repo }}
base_sha: ${{ steps.setup.outputs.base_sha }}
pr_number: ${{ steps.setup.outputs.pr_number }}
checkout_path: ${{ steps.setup.outputs.checkout_path }}
build_path: ${{ steps.setup.outputs.build_path }}
skip_detection: ${{ steps.should_skip.outputs.skip }}
has_changes_cpp: ${{ steps.detect_cpp.outputs.has_changes }}
has_changes_python: ${{ steps.detect_python.outputs.has_changes }}
has_changes_actions: ${{ steps.detect_actions.outputs.has_changes }}
steps:
- name: Workflow setup
id: setup
uses: Framework-R-D/phlex/.github/actions/workflow-setup@main
with:
ref: ${{ inputs.ref }}
repo: ${{ inputs.repo }}
pr-base-sha: ${{ inputs.pr-base-sha }}
checkout-path: ${{ inputs.checkout-path }}
build-path: ${{ inputs.build-path }}
- name: Determine if detection should be skipped
id: should_skip
env:
IS_ACT: ${{ steps.setup.outputs.is_act }}
LANGUAGE_MATRIX: ${{ inputs.language-matrix }}
run: |
if [ "${GITHUB_EVENT_NAME}" = "schedule" ] || \
[ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ] || \
[ "${GITHUB_EVENT_NAME}" = "push" ] || \
{ [ "${GITHUB_EVENT_NAME}" = "workflow_call" ] && [ -n "${LANGUAGE_MATRIX}" ]; } || \
[ "${IS_ACT}" = "true" ]; then
echo "skip=true" >> "$GITHUB_OUTPUT"
else
echo "skip=false" >> "$GITHUB_OUTPUT"
fi
- name: Detect C++ changes
id: detect_cpp
if: steps.should_skip.outputs.skip != 'true'
uses: Framework-R-D/phlex/.github/actions/run-change-detection@main
with:
checkout-path: ${{ steps.setup.outputs.checkout_path }}
ref: ${{ steps.setup.outputs.ref }}
repo: ${{ steps.setup.outputs.repo }}
base-ref: ${{ steps.setup.outputs.base_sha }}
head-ref: ${{ inputs.pr-head-sha || steps.setup.outputs.ref }}
file-type: |
cpp
cmake
- name: Detect Python changes
id: detect_python
if: steps.should_skip.outputs.skip != 'true'
uses: Framework-R-D/phlex/.github/actions/run-change-detection@main
with:
checkout-path: ${{ steps.setup.outputs.checkout_path }}
ref: ${{ steps.setup.outputs.ref }}
repo: ${{ steps.setup.outputs.repo }}
base-ref: ${{ steps.setup.outputs.base_sha }}
head-ref: ${{ inputs.pr-head-sha || steps.setup.outputs.ref }}
file-type: python
- name: Detect Actions changes
id: detect_actions
if: steps.should_skip.outputs.skip != 'true'
uses: Framework-R-D/phlex/.github/actions/run-change-detection@main
with:
checkout-path: ${{ steps.setup.outputs.checkout_path }}
ref: ${{ steps.setup.outputs.ref }}
repo: ${{ steps.setup.outputs.repo }}
base-ref: ${{ steps.setup.outputs.base_sha }}
head-ref: ${{ inputs.pr-head-sha || steps.setup.outputs.ref }}
include-globs: |
.github/workflows/*.yaml
.github/workflows/*.yml
.github/actions/**/action.yaml
.github/actions/**/action.yml
determine-languages:
needs: setup
if: always() && needs.setup.result == 'success'
runs-on: ubuntu-latest
outputs:
languages: ${{ steps.build_matrix.outputs.languages }}
steps:
- name: Build language matrix
id: build_matrix
env:
LANGUAGE_MATRIX: ${{ inputs.language-matrix }}
SKIP_DETECTION: ${{ needs.setup.outputs.skip_detection }}
HAS_CHANGES_CPP: ${{ needs.setup.outputs.has_changes_cpp }}
HAS_CHANGES_PYTHON: ${{ needs.setup.outputs.has_changes_python }}
HAS_CHANGES_ACTIONS: ${{ needs.setup.outputs.has_changes_actions }}
run: |
# If detection was skipped, use all languages or the provided language-matrix
if [ "${SKIP_DETECTION}" = "true" ]; then
if [ "${GITHUB_EVENT_NAME}" = "workflow_call" ] && [ -n "${LANGUAGE_MATRIX}" ]; then
# Validate that language-matrix is valid JSON
if ! echo "${LANGUAGE_MATRIX}" | python3 -c "import sys, json; json.load(sys.stdin)" 2>/dev/null; then
echo "::error::Invalid language-matrix input: must be valid JSON array"
exit 1
fi
echo "languages=${LANGUAGE_MATRIX}" >> "$GITHUB_OUTPUT"
else
echo 'languages=["cpp", "python", "actions"]' >> "$GITHUB_OUTPUT"
fi
exit 0
fi
# Build array based on detection results
langs=()
if [ "${HAS_CHANGES_CPP}" = "true" ]; then
langs+=("cpp")
fi
if [ "${HAS_CHANGES_PYTHON}" = "true" ]; then
langs+=("python")
fi
if [ "${HAS_CHANGES_ACTIONS}" = "true" ]; then
langs+=("actions")
fi
# Convert bash array to JSON array
if [ "${#langs[@]}" -eq 0 ]; then
echo 'languages=[]' >> "$GITHUB_OUTPUT"
else
json_array="["
for i in "${!langs[@]}"; do
if [ "$i" -gt 0 ]; then
json_array+=","
fi
json_array+="\"${langs[$i]}\""
done
json_array+="]"
echo "languages=$json_array" >> "$GITHUB_OUTPUT"
fi
codeql:
needs: [setup, determine-languages]
if: >
needs.determine-languages.result == 'success' && needs.determine-languages.outputs.languages != '[]'
name: Analyze ${{ matrix.language }} with CodeQL
runs-on: ubuntu-24.04
container:
image: ghcr.io/framework-r-d/phlex-ci:latest
env:
local_checkout_path: ${{ needs.setup.outputs.checkout_path }}
local_build_path: ${{ needs.setup.outputs.build_path }}
CODEQL_EXTRACTOR_CPP_COMPILATION_DATABASE:
${{ github.workspace }}/${{ needs.setup.outputs.build_path }}/compile_commands.json
strategy:
fail-fast: false
matrix:
language: ${{ fromJson(needs.determine-languages.outputs.languages) }}
timeout-minutes: 120
steps:
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: ${{ needs.setup.outputs.ref }}
path: ${{ env.local_checkout_path }}
fetch-depth: 0
- name: Setup build environment
uses: Framework-R-D/phlex/.github/actions/setup-build-env@main
with:
build-path: ${{ env.local_build_path }}
- name: Initialize CodeQL
uses: github/codeql-action/init@b1bff81932f5cdfc8695c7752dcee935dcd061c8 # v4.33.0
with:
languages: ${{ matrix.language }}
config-file: ${{ env.local_checkout_path }}/.github/codeql/codeql-config.yml
source-root: ${{ env.local_checkout_path }}
build-mode: none
- name: Produce compile_commands.json (C++ only)
if: matrix.language == 'cpp'
uses: Framework-R-D/phlex/.github/actions/configure-cmake@main
with:
build-type: ${{ env.BUILD_TYPE }}
source-path: ${{ env.local_checkout_path }}
build-path: ${{ env.local_build_path }}
- name: Verify compile_commands.json (C++ only)
if: matrix.language == 'cpp'
run: |
set -eu
if [ ! -f "$CODEQL_EXTRACTOR_CPP_COMPILATION_DATABASE" ]; then
echo "Expected compile_commands.json at $CODEQL_EXTRACTOR_CPP_COMPILATION_DATABASE" >&2
exit 1
fi
# Run CodeQL analysis (uploads results to code scanning)
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@b1bff81932f5cdfc8695c7752dcee935dcd061c8 # v4.33.0
with:
checkout_path: ${{ env.local_checkout_path }}
output: results
category: ${{ matrix.language }}
- name: Upload SARIF results
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
with:
name: codeql-sarif-${{ matrix.language }}
path: results
retention-days: 7
codeql-report:
name: Aggregate CodeQL alerts
needs: codeql
runs-on: ubuntu-24.04
if: needs.codeql.result == 'success'
permissions:
contents: read
issues: write
pull-requests: write
security-events: read
env:
CODEQL_MIN_LEVEL: warning
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Set log file path
id: set_log_path
run: echo "path=$RUNNER_TEMP/codeql-alerts.log" >> "$GITHUB_OUTPUT"
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
- name: Checkout Phlex for scripts
if: github.event_name == 'workflow_call'
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
repository: Framework-R-D/phlex
path: phlex
fetch-depth: 0
- name: Download CodeQL SARIF artifacts
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
with:
pattern: codeql-sarif-*
path: sarif
merge-multiple: true
- name: Check CodeQL SARIF for new or resolved alerts
id: check_codeql
env:
LOG_PATH: ${{ steps.set_log_path.outputs.path }}
INPUT_PR_NUMBER: ${{ inputs.pr-number }}
run: |
set -eu
ARGS=(
--sarif "$GITHUB_WORKSPACE/sarif"
--min-level "${CODEQL_MIN_LEVEL}"
--log-path "${LOG_PATH}"
)
PR_NUMBER=""
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
PR_NUMBER="$(jq -r '.pull_request.number // empty' "${GITHUB_EVENT_PATH}")"
elif [ "${GITHUB_EVENT_NAME}" = "workflow_call" ]; then
PR_NUMBER="${INPUT_PR_NUMBER}"
fi
if [ -n "$PR_NUMBER" ]; then
ARGS+=(--ref "refs/pull/${PR_NUMBER}/merge")
fi
script_path="scripts/check_codeql_alerts.py"
if [ "${GITHUB_EVENT_NAME}" = "workflow_call" ]; then
script_path="phlex/$script_path"
fi
python3 "$script_path" "${ARGS[@]}"
- name: Upload CodeQL alerts debug log
if: always()
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
with:
name: codeql-alerts-debug-log
path: ${{ steps.set_log_path.outputs.path }}
retention-days: 3
- name: Prepare PR comment data
if: >-
github.event_name == 'pull_request' && steps.check_codeql.outputs.comment_path != ''
env:
COMMENT_PATH: ${{ steps.check_codeql.outputs.comment_path }}
run: |
mkdir -p pr_comment_data
jq -r '.pull_request.number // empty' "${GITHUB_EVENT_PATH}" > pr_comment_data/pr_number.txt
cp "${COMMENT_PATH}" pr_comment_data/comment_body.md
- name: Upload PR comment data
if: >-
github.event_name == 'pull_request' && steps.check_codeql.outputs.comment_path != ''
uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7.0.0
with:
name: codeql-pr-data
path: pr_comment_data/
retention-days: 1
- name: Fail workflow due to new CodeQL alerts
# Fails the check on the PR so the user sees red X
if: >-
github.event_name == 'pull_request' && steps.check_codeql.outputs.new_alerts == 'true'
run: |
echo "New CodeQL alerts detected; failing job."
exit 1