You decide how to generate 代码扫描 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 代码扫描."
|CodeQL||使用 GitHub Actions（请参阅“使用操作设置 代码扫描 ”）或在第三方持续集成 (CI) 系统中运行 CodeQL 分析（请参阅“关于 CI 系统中的 CodeQL 代码扫描”）。|
|第三方||使用 GitHub Actions （请参阅“使用操作设置 代码扫描”）或在外部生成并上传到 GitHub （请参阅“将 SARIF 文件上传到 GitHub”）。|
If you run code scanning using multiple configurations, then sometimes an alert will have multiple analysis origins. If an alert has multiple analysis origins, you can view the status of the alert for each analysis origin on the alert page. 更多信息请参阅“关于分析源”。
Note: Starter workflows for Advanced Security have been consolidated in a "Security" category in the Actions tab of a repository. This new configuration is currently in beta and subject to change.
GitHub provides starter workflows for security features such as 代码扫描. 您可以使用这些建议的工作流程来构建 代码扫描 工作流程，而不是从头开始。 代码扫描 starter workflows are only available for your repository if 代码扫描 is enabled.
使用操作运行 代码扫描 将消耗分钟数。 更多信息请参阅“关于 GitHub Actions 的计费.”
- 在 GitHub.com 上，导航到仓库的主页面。
- 在仓库名称下，单击 Actions（操作）。
- 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.
- 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.
- On the right pane of the workflow page, click Documentation and follow the on-screen instructions to tailor the workflow to your needs. For more information, see "Using starter workflows" and "Configuring 代码扫描."
You can set up 代码扫描 in any public repository where you have write access.
使用操作运行 代码扫描 将消耗分钟数。 更多信息请参阅“关于 GitHub Actions 的计费.”
在 GitHub.com 上，导航到仓库的主页面。
To the right of "代码扫描 alerts", click Set up 代码扫描. For more information, see "Managing security and analysis settings for your organization" or "Managing security and analysis settings for your repository."
Under "Get started with 代码扫描", click Set up this workflow on the CodeQL 分析工作流程 or on a third-party workflow. Workflows are only displayed if they are relevant for the programming languages detected in the repository. The CodeQL 分析工作流程 is always displayed, but the "Set up this workflow" button is only enabled if CodeQL analysis supports the languages present in the repository.
To customize how 代码扫描 scans your code, edit the workflow.
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.
For more information, see "Configuring 代码扫描."
Use the Start commit drop-down, and type a commit message.
Choose whether you'd like to commit directly to the default branch, or create a new branch and start a pull request.
Click Commit new file or 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 can set up 代码扫描 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.
After setting up 代码扫描 for your repository, you can watch the output of the actions as they run.
You'll see a list that includes an entry for running the 代码扫描 workflow. The text of the entry is the title you gave your commit message.
Click the entry for the 代码扫描 workflow.
Click the job name on the left. For example, Analyze (LANGUAGE).
Review the logging output from the actions in this workflow as they run.
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.
Each 代码扫描 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 代码扫描 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.
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 an "Analysis not found" message when you click Details on the "代码扫描 results / TOOL NAME" check.
The table lists one or more categories. Each category relates to specific analyses, for the same tool and commit, performed on a different language or a different part of the code. For each category, the table shows the two analyses that 代码扫描 attempted to compare to determine which alerts were introduced or fixed in the pull request.
For example, in the screenshot above, 代码扫描 found an analysis for the merge commit of the pull request, but no analysis for the head of the main branch.
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 "Analysis not found" 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.
The solution in this situation is to add the name of the base branch to the
on:pull_requestspecification in the 代码扫描 workflow on that branch and then make a change that updates the open pull request that you want to scan.
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 setting up 代码扫描, and allowing its actions to complete, you can:
- View all of the 代码扫描 alerts generated for this repository. For more information, see "Managing 代码扫描 alerts for your repository."
- View any alerts generated for a pull request submitted after you set up 代码扫描. For more information, see "Triaging 代码扫描 alerts in pull requests."
- Set up notifications for completed runs. For more information, see "Configuring notifications."
- View the logs generated by the 代码扫描 analysis. For more information, see "Viewing 代码扫描 logs."
- Investigate any problems that occur with the initial setup of CodeQL 代码扫描. For more information, see "Troubleshooting the CodeQL workflow."
- Customize how 代码扫描 scans the code in your repository. For more information, see "Configuring 代码扫描."