我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

为仓库启用代码扫描

您可以对项目的仓库启用 代码扫描。

If you have write permissions to a repository, you can enable 代码扫描 for that repository.

代码扫描 可用于您有 Advanced Security 许可的组织拥有的私有仓库。

本文内容

注意:代码扫描 目前处于测试阶段,可能会更改。 有关参加公测的更多信息,请在此注册

注:站点管理员必须为 your GitHub Enterprise Server instance 启用 代码扫描,然后您才可使用此功能。 如果要使用 GitHub Actions 扫描代码,站点管理员还必须启用 GitHub Actions 并设置所需的基础结构。 更多信息请参阅“为设备配置 代码扫描”。

启用 代码扫描 的选项

在仓库级别决定如何生成 代码扫描 警报,以及使用哪些工具。 GitHub Enterprise Server 对 CodeQL 分析提供完全集成的支持,还支持使用第三方工具进行分析。 更多信息请参阅“关于 CodeQL”。

分析类型 用于生成警报的选项
CodeQL 使用 GitHub Actions(请参阅“使用操作启用 代码扫描”)或在第三方持续集成 (CI) 系统中使用 CodeQL runner(请参阅“在 CI 系统中运行代码扫描”)。
第三方 使用 GitHub Actions(请参阅“使用操作启用 代码扫描”)或者外部生成并上传到 GitHub Enterprise Server(请参阅“上传 SARIF 文件到 GitHub”)。

使用操作启用 代码扫描

  1. 在 GitHub Enterprise Server 上,导航到仓库的主页面。

  2. 在仓库名称下,单击 Security(安全)

    Security 选项卡

  3. To the right of "Code scanning", click Set up code scanning.

    "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 分析工作流程 or on a third-party workflow.

    "Set up this workflow" button under "Get started with code scanning" heading

  5. Optionally, to customize how 代码扫描 scans your code, edit the workflow. For more information, see "Configuring 代码扫描."

    Generally you can commit the CodeQL 分析工作流程 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.

    更多信息请参阅“配置 代码扫描。”

  6. 使用 Start commit(开始提交)下拉菜单,并键入提交消息。

    开始提交

  7. 选择您是想直接提交到默认分支,还是创建新分支并启动拉取请求。

    选择提交位置

  8. 单击 Commit new file(提交新文件)Propose new file(提议新文件)

In the default CodeQL 分析工作流程, 代码扫描 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, 代码扫描 will now commence.

You decide how you generate 代码扫描 alerts, and which tools you use, at a repository level. GitHub Enterprise Server provides fully integrated support for CodeQL analysis, and also supports analysis using third-party tools. For more information, see "About CodeQL."

After you enable 代码扫描, you can monitor analysis, view results, and further customize how you scan your code.

  1. 在仓库名称下,单击 Actions(操作)

    主仓库导航中的操作选项卡

    You can view the run status of 代码扫描 and get notifications for completed runs. For more information, see "Managing a workflow run" and "Configuring notifications."

    After you commit the workflow file or create a pull request, 代码扫描 will analyze your code according to the frequency you specified in your workflow file. If you created a pull request, 代码扫描 will only analyze the code on the pull request's topic branch until you merge the pull request into the default branch of the repository.

  2. Click the entry for the 代码扫描 workflow.

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

    Log output from the 代码扫描 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 代码扫描 alerts that were identified. For more information, see "Managing 代码扫描 alerts for your repository."

Note: If you raised a pull request to add the 代码扫描 workflow to the repository, alerts from that pull request aren't displayed directly on the 代码扫描 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 代码扫描 page.

Click the "n alerts found" link

Understanding the pull request checks

Each 代码扫描 workflow you enable 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 代码扫描 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 代码扫描 analysis entry to see logging data. This allows you to debug a problem if the analysis job failed. For example, for 代码扫描 analysis of compiled languages, this can happen if the action can't build the code.

After a scan completes, you can view alerts from a completed scan. For more information, see "<a href="/github/finding-security-vulnerabilities-and-errors-in-your-code/managing-alerts-from-code-scanning">Managing alerts from 代码扫描</a>."

When the 代码扫描 jobs complete, GitHub works out whether any alerts were added by the pull request and adds the "代码扫描 results / TOOL NAME" entry to the list of checks. After 代码扫描 has been performed at least once, you can click Details to view the results of the analysis. If you used a pull request to add 代码扫描 to the repository, you will initially see a "Missing analysis" message when you click Details on the "代码扫描 results / TOOL NAME" check.

Missing analysis for commit message

Reasons for the "missing analysis" message

After 代码扫描 has analyzed the code in a pull request, it needs to compare the analysis of the topic branch (the branch you used to create the pull request) with the analysis of the base branch (the branch into which you want to merge the pull request). This allows 代码扫描 to compute which alerts are newly introduced by the pull request, which alerts were already present in the base branch, and whether any existing alerts are fixed by the changes in the pull request. Initially, if you use a pull request to add 代码扫描 to a repository, the base branch has not yet been analyzed, so it's not possible to compute these details. In this case, when you click through from the results check on the pull request you will see the "Missing analysis for base commit SHA-HASH" message.

There are other situations where there may be no analysis for the latest commit to the base branch for a pull request. These include:

  • The pull request has been raised against a branch other than the default branch, and this branch hasn't been analyzed.

    To check whether a branch has been scanned, go to the 代码扫描 page, click the Branch drop-down and select the relevant branch.

    Choose a branch from the Branch drop-down menu

  • The latest commit on the base branch for the pull request is currently being analyzed and analysis is not yet available.

    Wait a few minutes and then push a change to the pull request to retrigger 代码扫描.

  • An error occurred while analyzing the latest commit on the base branch and analysis for that commit isn't available.

    Merge a trivial change into the base branch to trigger 代码扫描 on this latest commit, then push a change to the pull request to retrigger 代码扫描.

后续步骤

After enabling 代码扫描, and allowing its actions to complete, you can:

此文档对您有帮助吗?

Privacy policy

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或, 了解如何参与。