Skip to content

Optimize CodeQL scanning with sharding and job separation#1367

Open
5an7y-Microsoft wants to merge 6 commits intomicrosoft:mainfrom
5an7y-Microsoft:main
Open

Optimize CodeQL scanning with sharding and job separation#1367
5an7y-Microsoft wants to merge 6 commits intomicrosoft:mainfrom
5an7y-Microsoft:main

Conversation

@5an7y-Microsoft
Copy link
Copy Markdown
Contributor

This pull request updates the CodeQL Analysis GitHub Actions workflow to improve efficiency and reduce analysis time, especially for large pushes. The workflow now distinguishes between pull request and push/scheduled events: PRs analyze only changed samples in a single job, while pushes and scheduled runs use sharding to build and analyze all samples in parallel across four jobs. Several steps and dependencies have also been updated.

Workflow structure improvements:

  • Split the workflow into two jobs: analyze-pr (for pull requests, builds only changed samples in a single job) and analyze (for pushes/schedules, splits all samples across 4 parallel shards for faster analysis). [1] [2]

Performance and efficiency:

  • For push/schedule events, added logic to divide the build and analysis of all samples into 4 shards, each running in parallel with ThrottleLimit 1 to maintain accurate CodeQL tracing.
  • For pull requests, added a step to detect changed files and build only those samples, avoiding unnecessary builds.

Dependency and version updates:

  • Updated github/codeql-action/init and github/codeql-action/analyze to use version v4 instead of v3 for improved features and support.

Documentation:

  • Added detailed comments at the top of the workflow file explaining the new sharding approach and the distinction between PR and push/schedule jobs.

5an7y and others added 6 commits March 31, 2026 16:22
Split samples alphabetically into 4 equal shards using ListAllSamples.ps1
and run each on a separate machine in parallel. ThrottleLimit stays 1 per
shard for accurate CodeQL tracing. Each shard uploads SARIF with a
distinct category (shard-0..shard-3) so results merge in the Security tab.

On PRs the existing Build-ChangedSamples behavior is preserved (no sharding
needed since only changed files are built).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Job name and step name now show '1 of 4' through '4 of 4' (shard+1)
  instead of '0' through '3 of 4' which looked like an off-by-one bug
- Move build-mode back into the matrix (was there before sharding) so it
  remains explicit and consistent with the original workflow structure

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
GitHub Actions expressions don't support arithmetic operators so
'matrix.shard + 1' was invalid. Switch the matrix to [1, 2, 3, 4]
so the job name, step name, and log output all read '1 of 4' through
'4 of 4'. The PowerShell script subtracts 1 locally for the 0-based
slice math.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The previous single-job approach with a 4-shard matrix caused PRs to
spin up 4 identical runners all building the same changed files.

Split into two jobs:
- analyze-pr: runs only on pull_request, single runner, builds changed
  samples via Build-ChangedSamples.ps1. No shard matrix overhead.
- analyze: runs on push/schedule, 4-shard matrix, each shard builds its
  slice of all samples. Also drops the now-unnecessary 'get-changed-files'
  step from the push path.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ci: split CodeQL into separate PR and push/schedule jobs
@5an7y-Microsoft 5an7y-Microsoft marked this pull request as ready for review April 1, 2026 01:32
@5an7y-Microsoft 5an7y-Microsoft requested a review from a team as a code owner April 1, 2026 01:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants