Sub topic 4
Anatomy of a CodeQL workflow: triggers and permissions
This chapter assumes you can read YAML and know the basics of GitHub Actions: workflows, jobs, steps and triggers. You should also know how branches and pull requests work. Here you will read the top half of an advanced-setup CodeQL workflow. That covers when it runs and what it is allowed to do.
The overall shape
An advanced-setup workflow is a normal file in .github/workflows/. It has three parts that matter for this chapter.
on:lists the events that start a run.permissions:lists what the job's token may do.jobs:holds the steps that check out code, build a CodeQL database and analyze it.
The next chapter covers the steps. This one covers the first two parts.
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@v4Triggers: when scans run
Each trigger answers a different question about your code.
| Trigger | Question it answers | Notes |
|---|---|---|
| `push` to `main` | Is the code on the default branch clean? | Gives a baseline of alerts for the branch. |
| `pull_request` targeting `main` | Does this change add new problems? | Results appear on the pull request. |
| `schedule` | Did new queries or new disclosures find old problems? | Runs even when nobody pushes. |
The scheduled trigger matters because CodeQL's query sets are updated over time. Code that was clean last month can produce alerts today without a single commit.
The cron value '17 3 * * 1' means 03:17 UTC every Monday. Scheduled workflows run in UTC and only on the default branch. An off-the-hour minute such as 17 is deliberate. Many workflows are scheduled at minute 0, and busy start times can delay runs.
Scoping the branches
The branches: [main] filter on pull_request matches the pull request's target branch. A pull request into main is scanned. A pull request into a feature branch is not. If you merge into several long-lived branches, list each one.
Permissions: what the job may do
Every job gets a GITHUB_TOKEN. The permissions: block sets what that token can do. Anything you do not list is set to none once you declare the block. That gives you least privilege by default.
| Permission | Why CodeQL needs it |
|---|---|
| `security-events: write` | Upload results so they appear as code scanning alerts. |
| `contents: read` | Let `actions/checkout` read the repository. |
| `actions: read` | Let the action read workflow run details. Needed in private repositories. |
Without security-events: write, the analysis can finish and the upload still fails. The usual error is a message that the integration or resource is not accessible by the token.
Put permissions at the job level
You can set permissions: at the top of the file or on a job. Job level is the better choice here. If you later add a second job, such as a linter, it does not inherit security-events: write by accident.
Common errors
- No alerts appear and the upload step fails. Check
security-events: writeon the job. - Checkout fails in a private repository. Add
contents: readback. Declaringpermissions:removes the defaults. - The pull request has no scan. Confirm the pull request targets a branch listed under
pull_request.branches. - No scheduled runs. Scheduled workflows run only from the default branch. They can also be disabled after a long period without repository activity.
Exercise
A teammate's CodeQL workflow declares permissions: contents: read and nothing else. The analysis runs, but no alerts show up in the Security tab and the upload fails. What is missing, and why is job-level placement a good habit?
Show answer
The job needs security-events: write so it can upload results (and actions: read in private repositories). Once a permissions: block exists, unlisted permissions become none. Placing the block on the job keeps this write access from being inherited by other jobs in the workflow.