Sub topic 10
Customizing Scans with a Configuration File
This chapter assumes you can read a CodeQL workflow, know what the init and analyze steps do, and understand query suites at a basic level. You will move scan settings out of the workflow and into a configuration file.
Why use a configuration file
You can set options such as queries directly on the init step. That works for one or two settings. A configuration file keeps the settings in one place that can be reviewed like any other code. It also lets several workflows or repositories share the same settings.
The file is YAML. It can do three things that matter most:
- Add query suites or query directories to the default set.
- Exclude paths from analysis.
- Name the file once, so every matrix job uses the same rules.
A working example
This file adds the security-extended suite and a local directory of custom queries. It also skips test code and installed dependencies.
name: "Custom CodeQL configuration"
queries:
- uses: security-extended
- uses: ./.github/codeql/custom-queries
paths-ignore:
- '**/test/**'
- '**/node_modules/**'Save it as .github/codeql/codeql-config.yml. Then point the init step at it with the config-file input:
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
config-file: ./.github/codeql/codeql-config.ymlThe path is relative to the repository root. The workflow must check out the repository before init runs, or the file will not be found.
What each key does
| Key | Effect |
|---|---|
| `name` | A label for the configuration. It does not change results. |
| `queries` | Lists suites or query locations to run. Each entry uses `uses:`. |
| `paths-ignore` | Skips files that match these glob patterns. |
| `paths` | Restricts analysis to the listed paths only. |
A uses value can be a built-in suite name such as security-extended, or a path starting with ./ for queries stored in your repository.
How the pieces combine
By default, CodeQL runs the standard suite for each language. Entries in the file's queries list are added to that default. If you want to run only your own queries, put disable-default-queries: true at the top level of the file.
A queries input on the init step is also allowed. By default, it replaces the queries in the configuration file. If you prefix the value with +, for example +security-extended, it is added to them instead.
The diagram below shows how the settings combine.
Path filtering and its limits
paths-ignore is useful for generated code, vendored dependencies, and tests. Keep three limits in mind:
- Patterns are globs, so
**/test/**matches atestfolder at any depth. - For compiled languages, ignored paths are still compiled. The filter removes alerts from those files, but it does not shorten the build.
- Ignoring test code hides real problems in test helpers. Do this only when those alerts are noise for your team.
Common errors
- File not found. The checkout step is missing or comes after
init, or the path is wrong. - Query path ignored. A local directory needs a
./prefix. Without it, CodeQL reads the entry as a pack name. - Wrong indentation. Each list entry under
queriesmust be a mapping withuses:. A bare string will fail validation. - Unexpected query set. An
initqueriesinput replaced the file's list. Add the+prefix to combine them.
Exercise
Exercise
Your configuration file lists security-extended under queries, but the init step also sets queries: security-and-quality without a +. Which suite runs, and how do you run both?
Show answer
Only security-and-quality runs, because the init input replaces the file's queries. To run both, write the input as +security-and-quality so it is added to the file's list.