Repository navigation
186 lines (171 loc) · 7.45 KB
/
Copy pathcodeql.yml
File metadata and controls
186 lines (171 loc) · 7.45 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
name: CodeQL
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
- develop
schedule:
# Mon/Wed/Fri at 02:00 UTC — frequent enough for dashboard freshness, not every day
- cron: '0 2 * * 1,3,5'
workflow_dispatch:
permissions:
contents: read
security-events: write
actions: read
packages: read
concurrency:
group: codeql-${{ github.ref }}
cancel-in-progress: true
jobs:
detect:
name: Detect languages
# Skip push-triggered runs for PR merge commits — the PR scan already covered them
if: github.event_name != 'push' || !startsWith(github.event.head_commit.message, 'Merge pull request')
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.detect.outputs.matrix }}
languages: ${{ steps.detect.outputs.languages }}
has-languages: ${{ steps.detect.outputs.has-languages }}
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Detect CodeQL languages from repository contents
id: detect
shell: bash
run: |
set -euo pipefail
files="$(git ls-files)"
langs=()
want() { grep -qiE "$1" <<<"$files"; }
# Map repository contents to the CodeQL languages worth analysing.
if want '(^|/)\.github/workflows/.*\.ya?ml$'; then langs+=(actions); fi
if want '\.(js|jsx|mjs|cjs|ts|tsx)$'; then langs+=(javascript-typescript); fi
if want '\.py$|(^|/)(requirements\.txt|pyproject\.toml|setup\.py|Pipfile)$'; then langs+=(python); fi
if want '\.rb$|(^|/)(Gemfile|.+\.gemspec)$'; then langs+=(ruby); fi
if want '\.(java|kt|kts)$'; then langs+=(java-kotlin); fi
if want '\.cs$|\.(csproj|sln)$'; then langs+=(csharp); fi
if want '\.(c|cc|cpp|cxx|h|hh|hpp|hxx)$'; then langs+=(c-cpp); fi
if want '\.go$|(^|/)go\.mod$'; then langs+=(go); fi
if want '\.rs$|(^|/)Cargo\.toml$'; then langs+=(rust); fi
if want '\.swift$|(^|/)Package\.swift$'; then langs+=(swift); fi
# build-mode none needs no build toolchain (actions, js/ts, python, ruby, java-kotlin, csharp, rust); compiled languages (c-cpp, go, swift) use autobuild, and swift additionally requires a macOS runner.
json='['; sep=''
for l in ${langs[@]+"${langs[@]}"}; do
case "$l" in c-cpp|go|swift) mode=autobuild ;; *) mode=none ;; esac
case "$l" in swift) runner=macos-latest ;; *) runner=ubuntu-latest ;; esac
json+="${sep}{\"language\":\"${l}\",\"build-mode\":\"${mode}\",\"runner\":\"${runner}\"}"
sep=','
done
json+=']'
{
echo "matrix=${json}"
echo "languages=${langs[*]:-none}"
if [[ ${#langs[@]} -gt 0 ]]; then echo "has-languages=true"; else echo "has-languages=false"; fi
} >> "$GITHUB_OUTPUT"
echo "Detected CodeQL languages: \`${langs[*]:-none}\`" >> "$GITHUB_STEP_SUMMARY"
analyse:
name: Analyse (${{ matrix.language }})
needs: detect
if: needs.detect.outputs.has-languages == 'true'
runs-on: ${{ matrix.runner }}
strategy:
fail-fast: false
matrix:
include: ${{ fromJSON(needs.detect.outputs.matrix) }}
steps:
- name: Checkout code
uses: actions/checkout@v7
- name: Initialise CodeQL (${{ matrix.language }} / ${{ matrix.build-mode }})
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
# security-and-quality is the most thorough suite (security-extended + maintainability).
queries: security-and-quality
config: |
paths-ignore:
- node_modules
- vendor
- vendor/bundle
- .git
- .venv
- __pycache__
- dist
- build
- name: Autobuild
if: matrix.build-mode == 'autobuild'
uses: github/codeql-action/autobuild@v4
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v4
with:
category: ${{ matrix.language }}
upload: true
summary:
name: Security Report
runs-on: ubuntu-latest
needs: [detect, analyse]
if: always()
steps:
- name: Generate security summary
run: |
{
echo "## 🔒 CodeQL Security Analysis"
echo ""
echo "- **Detected languages**: \`${{ needs.detect.outputs.languages }}\`"
echo "- **Query suite**: security-and-quality (advanced security + maintainability)"
echo "- **Build modes**: none for interpreted/actions, autobuild for compiled"
echo "- **Triggers**: pushes and PRs to the default branch, Mon/Wed/Fri 02:00 UTC, manual dispatch"
echo "- **Analysis result**: ${{ needs.analyse.result }}"
echo ""
echo "The \`actions\` pack scans the workflows themselves; ShellCheck (bash-lint-advanced) covers shell scripts CodeQL cannot."
echo ""
echo "Review findings in the [code-scanning alerts](../../security/code-scanning)."
} >> "$GITHUB_STEP_SUMMARY"
sync-templates:
name: Sync action versions to templates
runs-on: ubuntu-latest
# Only on push to default branch — keeps templates in sync after dependabot merges
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master')
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v7
- name: Sync action versions from .github/workflows to workflows-templates
run: |
set -euo pipefail
changed=()
while IFS= read -r template; do
filename=$(basename "$template")
live=".github/workflows/$filename"
[[ -f "$live" ]] || continue
cp "$template" "$template.bak"
while IFS= read -r line; do
if [[ "$line" =~ ^[[:space:]]*uses:[[:space:]]*(([^@[:space:]]+)@([^[:space:]#]+)) ]]; then
full="${BASH_REMATCH[1]}"
action="${BASH_REMATCH[2]}"
sed -i "s|uses: ${action}@[^[:space:]#]*|uses: ${full}|g" "$template"
fi
done < "$live"
if ! diff -q "$template.bak" "$template" > /dev/null 2>&1; then
changed+=("$template")
diff "$template.bak" "$template" || true
fi
rm "$template.bak"
done < <(find workflows-templates -name '*.yml' -type f | sort)
if [[ ${#changed[@]} -eq 0 ]]; then
echo "Templates already in sync" >> "$GITHUB_STEP_SUMMARY"
exit 0
fi
echo "Updated ${#changed[@]} template(s):" >> "$GITHUB_STEP_SUMMARY"
printf ' - %s\n' "${changed[@]}" >> "$GITHUB_STEP_SUMMARY"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add workflows-templates/
git commit --no-gpg-sign -m "chore(templates): sync action versions from .github/workflows"
git push