Code scanning in an Azure DevOps pipeline means adding an automated security and quality check that runs every time code is pushed or a pull request is opened, instead of relying on someone remembering to run a scanner locally. The usual way to do this is to enable GitHub Advanced Security for Azure DevOps on your organisation (which brings CodeQL code scanning and dependency scanning into Azure Repos), or, if that is not available to you, to run a scanner such as CodeQL CLI, Microsoft Security DevOps or a third-party tool as a pipeline task. Both approaches produce findings that appear as alerts and can be set to block a pull request. It achieves two things: it catches injection flaws, hardcoded secrets, unsafe deserialisation and vulnerable dependencies before they reach production, and it gives you an audit trail of what was scanned and when.

What You'll Need

  • An Azure DevOps organisation with at least one project and a Git repository in Azure Repos (or a GitHub repo connected to your pipeline).
  • Project Collection Administrator or organisation owner rights to enable Advanced Security, plus repository administrator rights to configure branch policies.
  • A pipeline definition file (azure-pipelines.yml) in the default branch, or permission to create one.
  • For CodeQL CLI or Microsoft Security DevOps, a pipeline agent that can reach the internet or a self-hosted agent with the tool installed.
  • Knowledge of which languages your repository contains, since CodeQL supports a fixed set of compiled and interpreted languages and each needs its own build step.

Step-by-Step

  1. Check which scanning route is open to you. GitHub Advanced Security for Azure DevOps is sold as an add-on per active committer and is not available on every plan or region. Open Organisation settings and look for the Advanced Security or Repositories area; if the option is present, you can enable CodeQL scanning natively. If it is absent, use the pipeline-task route instead. Menu labels differ slightly between Azure DevOps versions and regions, so look for the closest match rather than an exact string.
  2. Enable Advanced Security for the repositories you care about. In organisation settings, turn on Advanced Security, then go to Project settings > Repositories, select the repository, and enable it under the repository's Advanced Security options. Enabling it at organisation level does not automatically enable every repo; check each one.
  3. Add the CodeQL task to your pipeline. In azure-pipelines.yml, insert an AdvancedSecurity-Prepare task before your build and an AdvancedSecurity-Analyze task after it, both from the AdvancedSecurity task family. The prepare step must run before compilation so that CodeQL can observe the build. For interpreted languages such as JavaScript or Python, the analyse step performs the extraction itself and no separate compile is needed.
  4. Match the task to your language and build tool. Set the language input on the prepare task (for example csharp, java, javascript, python, go, cpp, ruby, swift). For compiled languages the build between prepare and analyse must actually compile the code being scanned; a pipeline that only restores packages will produce an empty database and no results.
  5. Configure a scheduled or all-branch scan. Native scanning runs on the default branch by default and on pull requests once policies are set. If you want continuous coverage on feature branches, add a scheduled trigger or a branch filter to the pipeline so a scan runs at least daily. Scanning only on pull requests leaves the default branch unscanned when changes merge from a branch nobody validated.
  6. Set the branch policy to surface and block findings. Go to Project settings > Repositories, pick the repo, open Policies for the target branch, and add a status policy for the code scanning build. Once a scan has produced results, Advanced Security adds a pull request check that reports new alerts. Choose whether new high or critical alerts must be resolved before merge; blocking on everything from day one tends to stall teams, so start with high and critical severity.
  7. Run the pipeline and check the results. Queue a build manually the first time and watch the analyse task's log. When it finishes, open the repository's Advanced Security alerts view in the project to see findings grouped by severity, rule and file. Click into a finding to see the data-flow path CodeQL traced from source to sink.
  8. Triage and dismiss deliberately. For each alert, either fix the code, or dismiss it with a documented reason (false positive, used in tests, risk accepted). Dismissals are stored in the repository and apply to future scans, so a dismissed finding does not reappear on every pull request. Review dismissals periodically because code changes can make an old justification wrong.
  9. Add dependency scanning if you use packages. Advanced Security dependency scanning reviews your package manifests and lock files for known vulnerable versions and reports them alongside code alerts. It complements CodeQL: CodeQL finds flaws in your own code, dependency scanning finds flaws in code you did not write.
  10. If you are using the pipeline-task route instead, install the Microsoft Security DevOps extension from the Visual Studio Marketplace, add its task to the pipeline, and let it run the bundled scanners. Alternatively call the CodeQL CLI directly in a script step: create a database with database create, run database analyze with a query suite, and publish the resulting SARIF file with the PublishBuildArtifacts or SARIF upload task so results appear in the build summary.

Common Mistakes

  • Analysing before building. For compiled languages, the prepare task must come before the compile step and the analyse task after, or the database will be empty and the scan will silently pass with zero findings.
  • Excluding the source you actually care about. Build steps that only compile a subset of projects, or pipelines that check out with a shallow path filter, scan less than you think.
  • Leaving the scan non-blocking forever. A pipeline task that reports alerts but never fails means the first hundred findings become background noise nobody reads.
  • Scanning only pull requests. Merges, hotfixes and direct pushes to the default branch then go unscanned.
  • Dismissing alerts without a reason. Undocumented dismissals are indistinguishable from ignored real bugs when the next person reviews the repository.
  • Treating a clean scan as proof of safety. Static analysis finds classes of bug, not every bug; logic errors and access-control mistakes in business rules routinely pass.

Tips

  • Start with the default query suite, then add security-extended queries once the baseline is clean; moving straight to extended suites buries real issues in volume.
  • Schedule the scan nightly as well as on pull requests so new query updates catch old code.
  • Keep the scan in the same pipeline as the build where possible, so the code that is scanned is the code that is compiled and shipped.
  • Use a dedicated, least-privileged pipeline identity for scanning, and never print secrets in scan logs or SARIF output.
  • Put the alert dashboard on a team screen or a weekly review slot; findings nobody looks at do not get fixed.

Troubleshooting

The scan succeeds but reports zero results. Almost always the database was created before or without a build. Reorder the tasks so prepare comes first, the real compile runs in between, and analyse comes last. Also confirm the language input matches the actual code and that the compile step built the project you expect.

The pipeline fails with a licence or permission error. Advanced Security is not enabled for that repository, or the build service identity lacks access. Check organisation-level enablement first, then the repository-level setting, then the pipeline's permissions on the repo.

Pull request checks do not appear. The branch policy is missing, or the scan has not yet run against the base branch to establish a baseline. Run a scan on the default branch, then re-open or push to the pull request.

The scan is very slow. Narrow the query suite, exclude generated code, vendored libraries and test fixtures from analysis, and cache package restores. Very large monorepos may need splitting into several pipelines, one per language or service.

An alert is clearly wrong. Dismiss it with a reason rather than disabling the query globally; a blanket exclusion hides real findings across the whole repository.

Frequently Asked Questions

Does this replace a manual security review?

No. Static analysis catches specific classes of defect in code it can see. Design flaws, business-logic abuse and infrastructure misconfiguration still need human review.

Can I scan a GitHub repository with an Azure DevOps pipeline?

Yes. A pipeline can check out a GitHub repo and run the same scanning tasks. Note that native Advanced Security alerts for GitHub repos live in GitHub's own security view, not in Azure DevOps, so pick one place to review findings.

Which languages does CodeQL support?

Compiled languages such as C#, C/C++, Go, Java, Kotlin and Swift, and interpreted ones such as JavaScript/TypeScript, Python and Ruby. Support varies by tool version, so confirm the current list for your scanner before planning.

Will scanning slow down every pull request?

It adds time to the pipeline. Incremental or changed-file scanning, a narrower query set on pull requests and the full suite nightly is a common compromise.

Do alerts on existing code need fixing before I enable blocking?

No. Baselining means only new alerts block merges, so existing findings can be worked down over time while new ones are stopped immediately.