Repository navigation
118 lines (104 loc) · 3.89 KB
/
Copy pathcodeql.yml
File metadata and controls
118 lines (104 loc) · 3.89 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
name: 'CodeQL'
on:
push:
branches:
- 'main'
pull_request:
branches:
- 'main'
schedule:
- cron: '0 5 * * 5'
permissions:
contents: 'read'
# A newer push to the same pull request makes the running analysis obsolete.
# Pushes to main are left to finish.
concurrency:
group: '${{ github.workflow }}-${{ github.ref }}'
cancel-in-progress: "${{ github.event_name == 'pull_request' }}"
jobs:
# A pull request is only analysed for the languages it touches. Pushes to
# main and the weekly run analyse everything.
changes:
name: 'Detect relevant changes'
runs-on: 'ubuntu-latest'
permissions:
pull-requests: 'read'
outputs:
languages: '${{ steps.filter.outputs.languages }}'
steps:
- name: 'Harden Runner'
uses: 'step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a' # v2.22.4
with:
egress-policy: 'audit'
# The files API returns at most 3000 files, so a pull request that
# large analyses everything.
- name: 'Determine the languages to analyse'
id: 'filter'
env:
EVENT_NAME: '${{ github.event_name }}'
GH_TOKEN: '${{ github.token }}'
PR_NUMBER: '${{ github.event.pull_request.number }}'
run: |
languages='["actions","python"]'
if [[ "$EVENT_NAME" == 'pull_request' ]]; then
files="$(gh api --paginate "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/files" --jq '.[].filename')"
if [[ "$(wc --lines <<< "$files")" -lt 3000 ]]; then
languages="$(jq --compact-output --null-input --raw-input '
[inputs] as $files
| [
if any($files[]; test("^\\.github/(actions|workflows)/")) then "actions" else empty end,
if any($files[]; test("\\.py$|^\\.github/workflows/codeql\\.yml$")) then "python" else empty end
]' <<< "$files")"
fi
fi
echo "languages=${languages}" | tee --append "$GITHUB_OUTPUT"
analyze:
name: 'Analyze'
needs: 'changes'
if: "needs.changes.outputs.languages != '[]'"
runs-on: 'ubuntu-latest'
permissions:
actions: 'read'
contents: 'read'
security-events: 'write'
strategy:
fail-fast: false
matrix:
language: '${{ fromJSON(needs.changes.outputs.languages) }}'
steps:
- name: 'Harden Runner'
uses: 'step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a' # v2.22.4
with:
egress-policy: 'audit'
- name: 'Checkout repository'
uses: 'actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1' # v7.0.1
- name: 'Initialize CodeQL'
uses: 'github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066' # v4.39.3
with:
languages: '${{ matrix.language }}'
- name: 'Autobuild'
uses: 'github/codeql-action/autobuild@33d7b85da1a04171e211befe2c739303b9a4c066' # v4.39.3
- name: 'Perform CodeQL Analysis'
uses: 'github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066' # v4.39.3
with:
category: '/language:${{ matrix.language }}'
# The only check the branch ruleset requires from this workflow. A job
# skipped above never reports its matrix names, so requiring those would
# block every pull request that skips them. Skipped counts as passed here.
result:
name: 'CodeQL'
if: 'always()'
needs:
- 'analyze'
- 'changes'
runs-on: 'ubuntu-latest'
steps:
- name: 'Harden Runner'
uses: 'step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a' # v2.22.4
with:
egress-policy: 'audit'
- name: 'Fail unless every job passed or was skipped'
env:
NEEDS: '${{ toJSON(needs) }}'
run: |
jq --exit-status 'all(.[]; .result == "success" or .result == "skipped")' <<< "$NEEDS"