Skip to main content

Setting up code scanning for a repository

You can set up code scanning by adding a workflow to your repository.

Who can use this feature

If you have write permissions to a repository, you can set up or configure code scanning for that repository.

Code scanning is available for all public repositories on GitHub.com. Code scanning is also available for private repositories owned by organizations that use GitHub Enterprise Cloud and have a license for GitHub Advanced Security. 有关详细信息,请参阅“关于 GitHub Advanced Security”。

Options for setting up code scanning

You decide how to generate code scanning alerts, and which tools to use, at a repository level. GitHub provides fully integrated support for CodeQL analysis, and also supports analysis using third-party tools. For more information, see "About code scanning."

分析类型用于生成警报的选项
CodeQL使用 GitHub Actions(请参阅“使用操作设置 code scanning”),或在第三方持续集成 (CI) 系统中运行 CodeQL 分析(请参阅“关于 CI 系统中的 CodeQL code scanning”)。
第三方使用 GitHub Actions(请参阅“使用操作设置 code scanning”)或从外部生成并上传到 GitHub(请参阅“将 SARIF 文件上传到 GitHub”)。

如果使用多个配置运行代码扫描,则有时警报会有多个分析源。 如果警报有多个分析源,你可在警报页上查看每个分析源的警报状态。 有关详细信息,请参阅“关于分析源”。

Setting up code scanning using starter workflows

注意:Advanced Security 的初始工作流已合并到存储库的“操作”选项卡中的“安全”类别中。 这种新配置目前处于 beta 版本,可能会有变动。

GitHub 为 code scanning 等安全功能提供起始工作流。 您可以使用这些建议的工作流程来构建 code scanning 工作流程,而不是从头开始。 Code scanning starter workflows are only available for your repository if code scanning is enabled.

使用操作运行 code scanning 将消耗分钟数。 有关详细信息,请参阅“关于 GitHub Actions 的计费”。

  1. 在 GitHub.com 上,导航到存储库的主页。
  2. 在你的存储库名称下,单击 “操作”。 主存储库导航中的“操作”选项卡
  3. If the repository has already at least one workflow set up and running, click New workflow and go to step 5. If there are currently no workflows configured for the repository, go to the next step. Screenshot of the New workflow button
  4. Scroll down to the "Security" category and click Configure under the workflow you want to configure, or click View all to see all available security workflows. Screenshot of the Actions workflows security section
  5. On the right pane of the workflow page, click Documentation and follow the on-screen instructions to tailor the workflow to your needs. Screenshot of the Documentation tab for starter workflows For more information, see "Using starter workflows" and "Configuring code scanning."

Setting up code scanning manually

You can set up code scanning in any public repository where you have write access.

使用操作运行 code scanning 将消耗分钟数。 有关详细信息,请参阅“关于 GitHub Actions 的计费”。

  1. 在 GitHub.com 上,导航到存储库的主页。

  2. 在存储库名称下,单击“安全性”。 “安全”选项卡

  3. To the right of "Code scanning alerts", click Set up code scanning. For more information, see "Managing security and analysis settings for your organization" or "Managing security and analysis settings for your repository." "Set up code scanning" button to the right of "Code scanning" in the Security Overview

  4. Under "Get started with code scanning", click Set up this workflow on the CodeQL analysis workflow or on a third-party workflow. "Set up this workflow" button under "Get started with code scanning" headingWorkflows are only displayed if they are relevant for the programming languages detected in the repository. The CodeQL analysis workflow is always displayed, but the "Set up this workflow" button is only enabled if CodeQL analysis supports the languages present in the repository.

  5. To customize how code scanning scans your code, edit the workflow.

    Generally you can commit the CodeQL analysis workflow without making any changes to it. However, many of the third-party workflows require additional configuration, so read the comments in the workflow before committing.

    For more information, see "Configuring code scanning."

  6. Use the Start commit drop-down, and type a commit message. Start commit

  7. Choose whether you'd like to commit directly to the default branch, or create a new branch and start a pull request. Choose where to commit

  8. Click Commit new file or Propose new file.

In the default CodeQL analysis workflow, code scanning is configured to analyze your code each time you either push a change to the default branch or any protected branches, or raise a pull request against the default branch. As a result, code scanning will now commence.

The on:pull_request and on:push triggers for code scanning are each useful for different purposes. For more information, see "Scanning pull requests" and "Scanning on push."

Bulk set up of code scanning

You can set up code scanning in many repositories at once using a script. If you'd like to use a script to raise pull requests that add a GitHub Actions workflow to multiple repositories, see the jhutchings1/Create-ActionsPRs repository for an example using PowerShell, or nickliffen/ghas-enablement for teams who do not have PowerShell and instead would like to use NodeJS.

Viewing the logging output from code scanning

After setting up code scanning for your repository, you can watch the output of the actions as they run.

  1. 在你的存储库名称下,单击 “操作”。 主存储库导航中的“操作”选项卡

    You'll see a list that includes an entry for running the code scanning workflow. The text of the entry is the title you gave your commit message.

    Actions list showing code scanning workflow

  2. Click the entry for the code scanning workflow.

  3. Click the job name on the left. For example, Analyze (LANGUAGE).

    Log output from the code scanning workflow

  4. Review the logging output from the actions in this workflow as they run.

  5. Once all jobs are complete, you can view the details of any code scanning alerts that were identified. For more information, see "Managing code scanning alerts for your repository."

Note: If you raised a pull request to add the code scanning workflow to the repository, alerts from that pull request aren't displayed directly on the Code scanning page until the pull request is merged. If any alerts were found you can view these, before the pull request is merged, by clicking the n alerts found link in the banner on the Code scanning page.

Click the "n alerts found" link

Understanding the pull request checks

Each code scanning workflow you set to run on pull requests always has at least two entries listed in the checks section of a pull request. There is one entry for each of the analysis jobs in the workflow, and a final one for the results of the analysis.

The names of the code scanning analysis checks take the form: "TOOL NAME / JOB NAME (TRIGGER)." For example, for CodeQL, analysis of C++ code has the entry "CodeQL / Analyze (cpp) (pull_request)." You can click Details on a code scanning analysis entry to see logging data. This allows you to debug a problem if the analysis job failed. For example, for code scanning analysis of compiled languages, this can happen if the action can't build the code.

code scanning pull request checks

When the code scanning jobs complete, GitHub works out whether any alerts were added by the pull request and adds the "Code scanning results / TOOL NAME" entry to the list of checks. After code scanning has been performed at least once, you can click Details to view the results of the analysis.

Next steps

After setting up code scanning, and allowing its actions to complete, you can:

上一个指南关于代码扫描下一个指南Configuring code scanning