

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# GitHub GitHub 和 GitHub 企业服务器的应用程序连接
<a name="connections-github-app"></a>

您可以使用 GitHub 应用程序进行连接 CodeBuild。 GitHub 通过支持应用程序连接[Amazon CodeConnections](https://docs.amazonaws.cn/dtconsole/latest/userguide/welcome-connections.html)。

源提供商访问权限使您能够通过订阅 “使用” 或 “[教程：配置 CodeBuild-hosted GitHub 操作运行器](action-runner.md)在中[GitHub webhook 事件](github-webhook.md) CodeBuild使用 [ CreateWebhook](https://docs.amazonaws.cn/codebuild/latest/APIReference/API_CreateWebhook.html)” 来触发构建。

**注意**  
CodeConnections 在少于以下的地区可用 CodeBuild。您可以在中使用跨区域连接。 CodeBuild在可选区域中创建的连接不能用于其他区域。有关更多信息，请参阅 [Amazon CodeConnections 终端节点和限额](https://docs.amazonaws.cn/general/latest/gr/codestar_connections.html)。

**Topics**
+ [步骤 1：创建与 GitHub App 的连接（控制台）](#connections-github-console)
+ [第 2 步：授予 CodeBuild 项目 IAM 角色访问权限以使用连接](#connections-github-role-access)
+ [步骤 3：配置 CodeBuild 为使用新连接](#connections-github-account-credential)
+ [对 GitHub 应用程序进行故障排除](#connections-github-troubleshooting)

## 步骤 1：创建与 GitHub App 的连接（控制台）
<a name="connections-github-console"></a>

使用以下步骤使用 CodeBuild 控制台为您的项目添加连接 GitHub。

**创建与的连接 GitHub**
+ 按照《*开发者工具用户指南*》中的说明[创建连接 GitHub](https://docs.amazonaws.cn/dtconsole/latest/userguide/connections-create-github.html)。

**注意**  
您可以使用其他 Amazon 账户共享的连接，而不必在账户中创建或使用现有连接。有关更多信息，请参阅与 Amazon 账户[共享连接](https://docs.amazonaws.cn/dtconsole/latest/userguide/connections-share.html)。

## 第 2 步：授予 CodeBuild 项目 IAM 角色访问权限以使用连接
<a name="connections-github-role-access"></a>

您可以授予 CodeBuild 项目 IAM 角色访问权限，以使用您的连接提供的 GitHub 令牌。

**授予 CodeBuild 项目 IAM 角色访问权限**

1. 按照项目的说明为您的 CodeBuild 项目创建 IAM 角色。[CodeBuild 允许与其他人互动 Amazon 务](setting-up-service-role.md) CodeBuild 

1. 在按照说明进行操作的同时，将以下 IAM 策略添加到您的 CodeBuild 项目角色以授予连接访问权限。

------
#### [ JSON ]

****  

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Action": [
                   "codeconnections:GetConnectionToken",
                   "codeconnections:GetConnection"
               ],
               "Resource": [
                   "{{arn:aws:iam::*:role/Service*}}"
               ]
           }
       ]
   }
   ```

------

## 步骤 3：配置 CodeBuild 为使用新连接
<a name="connections-github-account-credential"></a>

您可以将连接配置为账户级别凭证，并在项目中使用。

------
#### [ Amazon Web Services 管理控制台 ]

**要将连接配置为账户级凭证，请在 Amazon Web Services 管理控制台**

1. 对于**来源提供商**，选择**GitHub**。

1. 对于**凭证**，执行以下操作之一：
   + 选择**默认来源凭证**，使用您账户的默认来源凭证应用于所有项目。

     1. 如果您未连接到 GitHub，请选择 “**管理默认来源凭证**”。

     1. 对于**凭据类型**，选择**GitHub 应用程序**。

     1. 在**连接**中，选择使用现有连接或创建新连接。
   + 选择**自定义来源凭证**，以便使用自定义来源凭证来覆盖您账户的默认设置。

     1. 对于**凭据类型**，选择**GitHub 应用程序**。

     1. 在**连接**中，选择使用现有连接或创建新连接。

------
#### [ Amazon CLI ]

**要将连接配置为账户级凭证，请在 Amazon CLI**
+ 打开终端（Linux、macOS 或 Unix）或命令提示符（Windows）。使用运行**import-source-credentials**命令，`--token`为您的连接指定`--auth-type``--server-type`、和。 Amazon CLI 

  使用以下命令：

  ```
  aws codebuild import-source-credentials --auth-type CODECONNECTIONS --server-type GITHUB --token {{<connection-arn>}}
  ```

------

您还可以为您的 CodeBuild 项目设置多个令牌。有关更多信息，请参阅 [将多个令牌配置为源级别凭证](multiple-access-tokens.md#asm-source-credential)。

## 对 GitHub 应用程序问题进行故障排除
<a name="connections-github-troubleshooting"></a>

以下信息可以帮助您解决 GitHub 应用程序的常见问题。

**Topics**
+ [安装   Amazon 不想要的 GitHub 区域的应用程序的连接器](#connections-github-troubleshooting.undesired-region)
+ [GitHub 应用程序连接无权访问存储库](#connections-github-troubleshooting.repo-access)
+ [这些区域有： Amazon 服务的 IAM 角色缺少必要的 IAM 权限。](#connections-github-troubleshooting.iam-permissions)

### 安装   Amazon 不想要的 GitHub 区域的应用程序的连接器
<a name="connections-github-troubleshooting.undesired-region"></a>

**问题：**您安装了 GitHub 来自 GitHub 商城的 Amazon 连接器，但连接是在不想要的区域创建的。如果您尝试在 GitHub 网站上重新配置该应用程序，它将无法运行，因为该应用程序已安装在您的 GitHub 帐户中。

**可能的原因：**该应用程序已安装在您的 GitHub 账户中，因此您只能重新配置应用程序权限。

**推荐的解决方案：**您可以在期望区域使用安装 ID 创建新连接。

1. 在处打开 CodeConnections 控制台，[https://console.aws.amazon.com/codesuite/settings/connections](https://console.amazonaws.cn/codesuite/settings/connections)然后使用控制 Amazon 台导航栏中的区域选择器导航到所需区域。

1. 按照《*开发者工具用户指南*》中的说明[创建连接 GitHub](https://docs.amazonaws.cn/dtconsole/latest/userguide/connections-create-github.html)。
**注意**  
由于您已经安装了适用于 GitHub 应用程序的 Amazon Connector，因此可以选择它来代替安装新的应用程序。

### GitHub 应用程序连接无权访问存储库
<a name="connections-github-troubleshooting.repo-access"></a>

**问题：使用连接**的 Amazon 服务（例如 CodeBuild 或 CodePipeline）报告说它无权访问存储库或存储库不存在。一些可能的错误消息包括：
+ `Authentication required for primary source.`
+ `Unable to create webhook at this time. Please try again later.`
+ `Failed to create webhook. GitHub API limit reached. Please try again later.`

***可能的原因：**你可能一直在使用该 GitHub 应用程序，但尚未授予 webhook 权限范围。*  
**推荐的解决方案：**要授予所需的权限范围，请按照[导航到要查看或修改的 GitHub 应用程序中的说明](https://docs.github.com/en/apps/using-github-apps/reviewing-and-modifying-installed-github-apps#navigating-to-the-github-app-you-want-to-review-or-modify)来配置已安装的应用程序。在权限部分下面，您会看到该应用程序没有 webhook 权限，并且您可以选择查看新请求的权限。查看并接受新权限。有关更多信息，请参阅[批准 GitHub 应用程序的更新权限](https://docs.github.com/en/apps/using-github-apps/approving-updated-permissions-for-a-github-app)。

***可能的原因：**连接按预期运行，但突然无法访问存储库。*  
**可能的解决方案：**首先检查您的[授权](https://docs.github.com/en/apps/using-github-apps/reviewing-and-revoking-authorization-of-github-apps)和[安装情况](https://docs.github.com/en/apps/using-github-apps/reviewing-and-modifying-installed-github-apps)，然后验证 GitHub 应用程序是否已授权并已安装。如果 GitHub 应用程序安装已暂停，则需要将其取消暂停。如果 GitHub 应用程序未获得 [ UAT（用户访问令牌）](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-with-a-github-app-on-behalf-of-a-user)连接的授权，或者未安装 [ IAT（安装访问令牌）](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/authenticating-as-a-github-app-installation)连接，则现有连接将不再可用，您需要创建新的连接。请注意，重新安装 GitHub 应用程序不会恢复先前与旧安装相关的连接。  
**可能的解决方案：**如果连接是 UAT 连接，请确保该连接不同时使用，例如在构建的多次 CodeBuild 并发运行中使用。这是因为如果连接刷新即将到期的令牌，则先前发布的 UAT 会 GitHub 立即失效。如果您需要使用 UAT 连接进行多个并发 CodeBuild 构建，则可以创建多个连接并独立使用每个连接。  
**可能的解决方案：**如果在过去 6 个月内未使用 UAT 连接，则连接将失效。 GitHub要修复此问题，请创建新的连接。

***可能的原因：**您可能在未安装应用程序的情况下使用 UAT 连接。*  
**推荐的解决方案：**尽管创建 UAT 连接不需要将连接与 GitHub 应用程序安装相关联，但需要安装才能访问存储库。按照说明[查看安装情况](https://docs.github.com/en/apps/using-github-apps/reviewing-and-modifying-installed-github-apps)，确保 GitHub 应用程序已安装。如果未安装，请导航到该[GitHub 应用程序的页面](https://github.com/marketplace/aws-connector-for-github)以安装该应用程序。有关 UAT 访问权限的更多信息，请参阅[关于用户访问令牌](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app#about-user-access-tokens)。

### 这些区域有： Amazon 服务的 IAM 角色缺少必要的 IAM 权限。
<a name="connections-github-troubleshooting.iam-permissions"></a>

**问题：**您看到以下任何错误消息：
+ `Access denied to connection {{<connection-arn>}}`
+ `Failed to get access token from {{<connection-arn>}}`

**推荐的解决方案：**通常使用与 Amazon 服务的连接，例如 CodePipeline 或 CodeBuild。当您为该 Amazon 服务提供 IAM 角色时，该 Amazon 服务可以使用该角色的权限代表您行事。确保 IAM 角色具有必要的权限。有关必需的 IAM 权限的更多信息，请参阅[授予 CodeBuild 项目 IAM 角色访问权限以使用连接](#connections-github-role-access)和 AWS CodeStar 通知的[身份和访问权限管理，以及 CodeConnections](https://docs.amazonaws.cn/dtconsole/latest/userguide/security-iam.html)*开发者工具控制台用户指南*。