Sub topic 5
Initializing, building and analyzing with the CodeQL actions
This chapter assumes you can read a GitHub Actions workflow, know what a job and a step are, and have seen the workflow skeleton from the previous chapter (triggers and permissions). Here you will look at the three CodeQL steps that do the work: init, build, and analyze.
The three phases
A CodeQL job runs the same sequence every time. The order matters, because each phase depends on the one before it.
- Checkout copies your source into the runner.
initinstalls the CodeQL tools and prepares an empty database for each language.- Build is needed only for compiled languages. CodeQL watches the build to learn how your code fits together.
analyzefinishes the database, runs the queries, and uploads the results as SARIF (the standard results file format).
The init step
github/codeql-action/init must run before anything else from CodeQL. Its main inputs are:
| Input | Purpose |
|---|---|
| `languages` | Which languages to create databases for |
| `build-mode` | How CodeQL observes the build: `none`, `autobuild`, or `manual` |
| `queries` | Extra query suites or packs to run |
| `config-file` | Path to a configuration file |
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}The languages value must be a supported CodeQL identifier. For example, JavaScript and TypeScript share the single identifier javascript-typescript.
The build step and build modes
The build-mode input decides what happens between init and analyze.
nonemeans CodeQL reads the source without building it. No build step is needed. Interpreted languages such as Python and JavaScript use this mode.autobuildmeans CodeQL guesses the build command and runs it for you.manualmeans you write the build command yourself. Use it when the guess would be wrong, for example when you need a specific build target.
Here is a manual build for a compiled project, with the security-extended suite added through the queries input:
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
queries: security-extended
- name: Build (compiled languages only)
if: matrix.build-mode == 'manual'
run: |
make clean
make all
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@v4The if condition keeps the build step from running for languages that use none. In a matrix with mixed languages, this lets one job definition serve all of them.
The analyze step
github/codeql-action/analyze runs the queries against the database and uploads the results. It needs security-events: write permission, or the upload fails.
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{ matrix.language }}"The category input labels the results. When one repository uploads several analyses, such as one per language, each needs its own category. Without distinct categories, a later upload can overwrite an earlier one, and alerts from one language may appear to close when they haven't.
Putting it together
The full workflow from the topic runs the steps once per language through a matrix:
name: CodeQL
on:
push:
branches: [main]
pull_request:
branches: [main]
schedule:
- cron: '17 3 * * 1'
jobs:
analyze:
name: Analyze (${{ matrix.language }})
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read
actions: read
strategy:
fail-fast: false
matrix:
include:
- language: javascript-typescript
build-mode: none
- language: python
build-mode: none
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v4
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
- name: Perform CodeQL analysis
uses: github/codeql-action/analyze@v4
with:
category: "/language:${{ matrix.language }}"Setting fail-fast: false lets the Python job finish even if the JavaScript job fails, so one broken language does not hide results from the other.
Common errors
analyzeruns withoutinit. The job fails because no database exists.- Missing
security-events: write. Analysis succeeds but the upload is rejected. - A compiled language with
build-mode: noneset wrongly, or a manual build that skips compilation. The scan finishes with few or no results, and the repository looks clean when it isn't. - Two analyses sharing one category. Results overwrite each other.
Exercise
A matrix job scans Java with build-mode: manual. The scan passes, but it reports almost no code was analyzed. The build step runs make all using a cached build directory. What is the likely cause, and what is the fix?
Show answer
CodeQL learns about compiled code by observing the compilation. With the cache, make all finds everything up to date and compiles nothing, so the database is almost empty. Fix it by forcing a full rebuild, for example running make clean before make all, or by not restoring the build cache for this job.