1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/content/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/setting-up-code-scanning-for-a-repository.md

13 KiB

title, shortTitle, intro, product, permissions, redirect_from, versions, type, topics
title shortTitle intro product permissions redirect_from versions type topics
Setting up code scanning for a repository Set up code scanning You can set up {% data variables.product.prodname_code_scanning %} by adding a workflow to your repository. {% data reusables.gated-features.code-scanning %} If you have write permissions to a repository, you can set up or configure {% data variables.product.prodname_code_scanning %} for that repository.
/github/managing-security-vulnerabilities/configuring-automated-code-scanning
/github/finding-security-vulnerabilities-and-errors-in-your-code/enabling-code-scanning
/github/finding-security-vulnerabilities-and-errors-in-your-code/enabling-code-scanning-for-a-repository
/github/finding-security-vulnerabilities-and-errors-in-your-code/setting-up-code-scanning-for-a-repository
/code-security/secure-coding/setting-up-code-scanning-for-a-repository
/code-security/secure-coding/automatically-scanning-your-code-for-vulnerabilities-and-errors/setting-up-code-scanning-for-a-repository
fpt ghes ghae
* >=3.0 *
how_to
Advanced Security
Code scanning
Actions
Repositories

{% data reusables.code-scanning.beta %} {% data reusables.code-scanning.enterprise-enable-code-scanning-actions %}

Options for setting up {% data variables.product.prodname_code_scanning %}

You decide how to generate {% data variables.product.prodname_code_scanning %} alerts, and which tools to use, at a repository level. {% data variables.product.product_name %} provides fully integrated support for {% data variables.product.prodname_codeql %} analysis, and also supports analysis using third-party tools. For more information, see "About {% data variables.product.prodname_code_scanning %}."

{% data reusables.code-scanning.enabling-options %}

Setting up {% data variables.product.prodname_code_scanning %} using actions

{% ifversion fpt %}Using actions to run {% data variables.product.prodname_code_scanning %} will use minutes. For more information, see "About billing for {% data variables.product.prodname_actions %}."{% endif %}

{% data reusables.repositories.navigate-to-repo %} {% data reusables.repositories.sidebar-security %} 3. To the right of "{% data variables.product.prodname_code_scanning_capc %} alerts", click Set up {% data variables.product.prodname_code_scanning %}. {% ifversion fpt or ghes > 3.0 %}If {% data variables.product.prodname_code_scanning %} is missing, you need to ask an organization owner or repository administrator to enable {% data variables.product.prodname_GH_advanced_security %}. For more information, see "Managing security and analysis settings for your organization" or "Managing security and analysis settings for your repository."{% endif %} "Set up {% data variables.product.prodname_code_scanning %}" button to the right of "{% data variables.product.prodname_code_scanning_capc %}" in the Security Overview 4. Under "Get started with {% data variables.product.prodname_code_scanning %}", click Set up this workflow on the {% data variables.product.prodname_codeql_workflow %} or on a third-party workflow. "Set up this workflow" button under "Get started with {% data variables.product.prodname_code_scanning %}" heading{% ifversion fpt or ghes > 2.22 %}Workflows are only displayed if they are relevant for the programming languages detected in the repository. The {% data variables.product.prodname_codeql_workflow %} is always displayed, but the "Set up this workflow" button is only enabled if {% data variables.product.prodname_codeql %} analysis supports the languages present in the repository.{% endif %} 5. To customize how {% data variables.product.prodname_code_scanning %} scans your code, edit the workflow.

Generally you can commit the {% data variables.product.prodname_codeql_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 {% data variables.product.prodname_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 {% data variables.product.prodname_codeql_workflow %}, {% data variables.product.prodname_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, {% data variables.product.prodname_code_scanning %} will now commence.

{% ifversion fpt or ghes > 3.2 or ghae-issue-5093 %} The on:pull_request and on:push triggers are each useful for different purposes. Code scanning on the pull_request trigger is more efficient than scanning on each push, and results are more accurate because they reflect the future state of the code by comparing the merge commit to the target branch baseline. But push scans will also flag alerts on any open pull requests for the branch, and if your third-party CI/CD system can only be configured to trigger on push, you will still be able to see alerts on pull requests. For more information, see "Scanning on pull requests" and "Scanning on push." {% endif %}

Bulk set up of {% data variables.product.prodname_code_scanning %}

You can set up {% data variables.product.prodname_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 {% data variables.product.prodname_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.

Understanding the pull request checks

Each {% data variables.product.prodname_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 {% data variables.product.prodname_code_scanning %} analysis checks take the form: "TOOL NAME / JOB NAME (TRIGGER)." For example, for {% data variables.product.prodname_codeql %}, analysis of C++ code has the entry "{% data variables.product.prodname_codeql %} / Analyze (cpp) (pull_request)." You can click Details on a {% data variables.product.prodname_code_scanning %} analysis entry to see logging data. This allows you to debug a problem if the analysis job failed. For example, for {% data variables.product.prodname_code_scanning %} analysis of compiled languages, this can happen if the action can't build the code.

{% data variables.product.prodname_code_scanning %} pull request checks

When the {% data variables.product.prodname_code_scanning %} jobs complete, {% data variables.product.prodname_dotcom %} works out whether any alerts were added by the pull request and adds the "{% data variables.product.prodname_code_scanning_capc %} results / TOOL NAME" entry to the list of checks. After {% data variables.product.prodname_code_scanning %} 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 {% data variables.product.prodname_code_scanning %} to the repository, you will initially see a "Missing analysis" message when you click Details on the "{% data variables.product.prodname_code_scanning_capc %} results / TOOL NAME" check.

Missing analysis for commit message

Reasons for the "missing analysis" message

After {% data variables.product.prodname_code_scanning %} 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 {% data variables.product.prodname_code_scanning %} 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 {% data variables.product.prodname_code_scanning %} 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 {% data variables.product.prodname_code_scanning_capc %} page, click the Branch drop-down and select the relevant branch.

{% ifversion fpt or ghes > 3.1 %} Choose a branch from the Branch drop-down menu {% else %} Choose a branch from the Branch drop-down menu {% endif %}

The solution in this situation is to add the name of the base branch to the on:push and on:pull_request specification in the {% data variables.product.prodname_code_scanning %} 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 {% data variables.product.prodname_code_scanning %}.

  • 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 {% data variables.product.prodname_code_scanning %} on this latest commit, then push a change to the pull request to retrigger {% data variables.product.prodname_code_scanning %}.

Next steps

After setting up {% data variables.product.prodname_code_scanning %}, and allowing its actions to complete, you can: