

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

# 共享构建项目
<a name="project-sharing"></a>

项目共享允许项目所有者与其他 AWS 账户或用户共享他们的 AWS CodeBuild 项目。在此模型中，拥有项目的账户（拥有者）将与其他账户（使用者）共享项目。使用者无法编辑或运行项目。

**Topics**
+ [共享项目](#project-sharing-share)
+ [相关服务](#project-sharing-related)
+ [访问与您共享的 CodeBuild 项目](project-sharing-access-prereqs.md)
+ [取消共享已共享的项目](project-sharing-unshare.md)
+ [标识已共享的项目](project-sharing-identify.md)
+ [共享项目权限](project-sharing-perms.md)

## 共享项目
<a name="project-sharing-share"></a>

使用者可以使用 AWS CLI 和 AWS CodeBuild 控制台来查看您共享的项目和构建。使用者无法编辑或运行项目。

您可以将项目添加到现有资源共享，也可以在 [AWS RAM 控制台](https://console.aws.amazon.com/ram)中创建资源共享。

**注意**  
您不能删除其构建已添加到资源共享的项目。

要与组织单位或整个组织共享项目，您必须启用与 AWS Organizations的共享。有关更多信息，请参阅《AWS RAM 用户指南》中的[允许与 AWS Organizations共享](https://docs.aws.amazon.com/ram/latest/userguide/getting-started-sharing.html)。

您可以使用 AWS CodeBuild 控制台、 AWS RAM 控制台或共享您拥有的项目。 AWS CLI 

**共享项目的先决条件**  
在开始共享项目之前，请确保您的 AWS 账户拥有该项目。无法共享已与您共享的项目。

**共享您拥有的项目（CodeBuild 控制台）**

1. 打开 AWS CodeBuild 控制台，网址为[https://console.aws.amazon.com/codesuite/codebuild/home](https://console.aws.amazon.com/codesuite/codebuild/home)。

1. 在导航窗格中，选择**构建项目**。
**注意**  
默认情况下，仅显示 10 个最新的构建项目。要查看更多构建项目，请选择齿轮图标，然后为**每页项目数**选择不同值，或使用向后和向前箭头。

1. 选择要共享的项目，然后选择**共享**。有关更多信息，请参阅《AWS RAM 用户指南》中的[创建资源共享](https://docs.aws.amazon.com/ram/latest/userguide/getting-started-sharing.html#getting-started-sharing-create)。

**共享您拥有的项目（AWS RAM 控制台）**  
请参阅《AWS RAM 用户指南**》中的[创建资源共享](https://docs.aws.amazon.com/ram/latest/userguide/working-with-sharing.html#working-with-sharing-create)。

**共享您拥有的项目（AWS RAM 命令）**  
使用 [create-resource-share](https://docs.aws.amazon.com/cli/latest/reference/ram/create-resource-share.html) 命令。

**共享您拥有的项目（CodeBuild 命令）**<a name="codebuild-command"></a>

使用 [put-resource-policy](https://docs.aws.amazon.com/cli/latest/reference/codebuild/put-resource-policy.html) 命令：

1. 创建一个名为 `policy.json` 的文件，并将以下内容复制到该文件中。

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

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement":[{
       "Effect":"Allow",
       "Action":[
         "codebuild:BatchGetProjects",
         "codebuild:BatchGetBuilds",
         "codebuild:ListBuildsForProject"],
       "Resource":"{{arn:aws:iam::*:role/Service*}}"
     }]
   }
   ```

------

1. 使用项目 ARN 和标识符更新 `policy.json` 以便共享项目。以下示例向根用户授予由 123456789012 标识的 AWS 账户的只读访问权限。

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

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement":[{
       "Effect":"Allow",
       "Principal":{
         "AWS": [
           "123456789012"
         ]
       },
       "Action":[
         "codebuild:BatchGetProjects",
         "codebuild:BatchGetBuilds",
         "codebuild:ListBuildsForProject"],
       "Resource":"arn:aws:codebuild:us-west-2:123456789012:project/my-project"
     }]
   }
   ```

------

1. 运行 [put-resource-policy](https://docs.aws.amazon.com/cli/latest/reference/codebuild/put-resource-policy.html) 命令：

   ```
   aws codebuild put-resource-policy --resource-arn {{<project-arn>}} --policy file://policy.json
   ```

1. 获取 AWS RAM 资源共享 ARN。

   ```
   aws ram list-resources --resource-owner SELF --resource-arns {{<project-arn>}}
   ```

   这将返回与以下内容类似的响应：

   ```
   {
     "resources": [
       {
         "arn": "{{<project-arn>}}",
         "type": "{{<type>}}",
         "resourceShareArn": "{{<resource-share-arn>}}",
         "creationTime": "{{<creation-time>}}",
         "lastUpdatedTime": "{{<last-update-time>}}"
       }
     ]
   }
   ```

   从响应中复制该{{<resource-share-arn>}}值以在下一步中使用。

1. 运行 promote-r AWS RAM [esource-share-from-](https://docs.aws.amazon.com/cli/latest/reference/ram/promote-resource-share-created-from-policy.html) policy 创建的命令。

   ```
   aws ram promote-resource-share-created-from-policy --resource-share-arn {{<resource-share-arn>}}
   ```

## 相关服务
<a name="project-sharing-related"></a>

项目共享与 AWS Resource Access Manager (AWS RAM) 集成，该服务使您可以与任何 AWS 账户或通过任何账户共享 AWS 资源 AWS Organizations。通过使用 AWS RAM，您可以通过创建*资源共享* 来共享资源，该共享指定要共享的资源和要与其共享资源的使用者。消费者可以是个人 AWS 帐户 AWS Organizations、中的组织单位或中的整个组织 AWS Organizations。

有关更多信息，请参阅 *[AWS RAM 《用户指南》](https://docs.aws.amazon.com/ram/latest/userguide/)*。