To enable code scanning in GitHub, navigate to your repository's Settings, select Code security and analysis, and activate the tool of your choice—such as CodeQL—under the Code scanning section. This process integrates static analysis security testing (SAST) directly into your development workflow, allowing GitHub to automatically detect vulnerabilities, hardcoded secrets, and logic errors in your code every time you push a commit or open a pull request.
Code scanning transforms the way developers handle security by shifting it "left," meaning security checks happen during the coding phase rather than after deployment. By utilizing engines like CodeQL, GitHub treats your code as data, querying it to find patterns that typically lead to security breaches, such as SQL injection or cross-site scripting. This prevents insecure code from ever reaching your production environment, reducing the risk of exploits and the cost of emergency patching.
What You'll Need
- A GitHub account with administrative access to the repository.
- A repository that is either public or hosted on a GitHub Enterprise account (private repositories require specific licensing for code scanning).
- A codebase written in a supported language (e.g., C++, C#, Go, Java, JavaScript, Python, Ruby, Swift).
- Basic familiarity with GitHub Actions, as most scanning tools run via workflow files.
Step-by-Step
- Open your web browser and navigate to the main page of the GitHub repository where you want to enable scanning.
- Click on the Settings tab located in the top navigation bar of the repository. Note that labels and menu positions may differ slightly depending on your browser version or GitHub UI updates.
- In the left-hand sidebar menu, look for the Security section and click on Code security and analysis.
- Scroll down to the Code scanning section. You will see several options for setup. For the most integrated experience, locate the CodeQL analysis option.
- Click the Set up button next to CodeQL. You will typically be given two choices: Default setup or Advanced setup.
- If you choose Default, GitHub will automatically configure the analysis for you without requiring a YAML file. This is the fastest way to get started and is recommended for standard projects. Click Enable CodeQL to finalize.
- If you choose Advanced, GitHub will generate a workflow file (usually
codeql-analysis.yml) in the.github/workflowsdirectory. This allows you to customize the scan frequency, the languages being scanned, and the specific queries being run. - If using the Advanced setup, review the generated YAML file. Ensure the
languageparameter matches the primary languages used in your project. - Commit the workflow file to your default branch (usually
mainormaster). - Once the file is committed, GitHub Actions will trigger the first scan. You can monitor the progress by clicking the Actions tab at the top of your repository.
- After the scan completes, navigate back to the Security tab in the top menu and click on Code scanning in the left sidebar to view the discovered alerts.
Common Mistakes
One of the most frequent errors is attempting to enable code scanning on a private repository using a free GitHub account. Code scanning is free for public repositories, but for private ones, it requires a GitHub Advanced Security (GHAS) license. Users often spend time configuring the YAML file only to find the action fails because the repository lacks the necessary permissions or licensing.
Another common mistake is ignoring the language configuration in the Advanced setup. If your project is written in Python but the workflow file is configured for Java, the scan will complete successfully but find zero vulnerabilities because it never actually analyzed the relevant files. Always verify that the language block in your workflow file aligns with your source code.
Developers also frequently forget to check the Actions tab when a scan doesn't seem to be working. They assume the tool is broken because no alerts appear in the Security tab, when in reality, the workflow is failing due to a build error. CodeQL often requires the code to be successfully compiled (for compiled languages like C# or Java) before it can perform analysis. If your build script fails, the scan fails.
Tips
To maximize the effectiveness of code scanning, integrate it into your Pull Request (PR) workflow. By ensuring that the scan runs on every PR, you can block the merging of code that introduces new security vulnerabilities. This creates a "security gate" that forces developers to address issues before they become part of the permanent codebase.
Use the Dismiss feature in the Security alerts tab to manage false positives. Not every alert is a genuine threat; some may be intentional architectural choices or unreachable code. By dismissing these and providing a reason (e.g., "False positive" or "Risk accepted"), you clean up your security dashboard and ensure that the team focuses only on critical, actionable bugs.
For large repositories, consider scheduling your scans. While scanning on every push is ideal, it can consume a significant amount of GitHub Actions minutes. You can modify the on: schedule trigger in your YAML file to run a full deep scan once a week while keeping the PR scans for incremental changes.
Troubleshooting
If the code scanning process does not go as planned, start with the simplest checks. First, check the Actions tab. If the workflow has a red "X," click into the failed run to see the logs. Most failures are caused by environment issues, such as a missing dependency required to build the project.
If the scan completes but finds nothing when you know vulnerabilities exist, check your codeql-analysis.yml file. Ensure that the paths to your source code are correct and that you haven't accidentally excluded the directories containing your logic via a paths-ignore filter.
If you cannot find the Code security and analysis menu in Settings, verify your permissions. You must have admin or maintainer access to the repository to change security settings. If you are a contributor without these rights, you will not see the options to enable scanning.
If the scan is hanging or taking an unusually long time, check the size of your repository. Very large monorepos can sometimes time out. In these cases, try narrowing the scope of the scan by specifying only the most critical directories in the workflow configuration.
Frequently Asked Questions
Does code scanning replace manual code reviews?
No. Code scanning is designed to find common patterns and known vulnerabilities (SAST). It cannot understand business logic or detect architectural flaws that a human reviewer would notice. It should be used as a first pass to catch "low-hanging fruit," allowing human reviewers to focus on more complex logic.
Which languages are supported by GitHub CodeQL?
CodeQL supports a wide array of popular languages, including C, C++, C#, Go, Java, JavaScript, TypeScript, Python, Ruby, and Swift. Support for other languages is added periodically; check the official GitHub documentation for the most current list.
Is code scanning free for all repositories?
It is free for all public repositories. For private repositories, it is available only for organizations using GitHub Enterprise with the GitHub Advanced Security add-on.
How often does the scan run?
By default, the scan runs on every push to the default branch and on every pull request. However, you can customize this frequency in the workflow YAML file to run on a schedule or only on specific events.
What is the difference between Code Scanning and Dependabot?
Dependabot scans your manifest files (like package.json or pom.xml) to find vulnerabilities in third-party libraries you have imported. Code scanning analyzes the actual code you wrote yourself to find security flaws in your custom logic.
Can I use third-party tools instead of CodeQL?
Yes. GitHub allows you to integrate other SARIF-compatible (Static Analysis Results Interchange Format) tools. You can run your own scanner in a GitHub Action and upload the results to the Security tab using the SARIF upload action.