

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在 CodeBuild 中使用 NuGet 套件
<a name="using-nuget-packages-in-codebuild"></a>

下列步驟已使用 [ CodeBuild 所提供 Docker 映像](https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-available.html)中列出的作業系統進行測試。

**Topics**
+ [使用 IAM 角色設定許可](#nuget-packages-in-codebuild-iam)
+ [使用 NuGet 套件](#consume-nuget-packages-in-codebuild)
+ [使用 NuGet 套件建置](#build-nuget-packages-in-codebuild)
+ [發佈 NuGet 套件](#publish-nuget-packages-in-codebuild)

## 使用 IAM 角色設定許可
<a name="nuget-packages-in-codebuild-iam"></a>

在 CodeBuild 中使用 CodeArtifact 中的 NuGet 套件時，需要這些步驟。 CodeBuild

1. 登入 AWS 管理主控台 並開啟位於 https：//[https://console.aws.amazon.com/iam/](https://console.aws.amazon.com/iam/) 的 IAM 主控台。

1. 在導覽窗格中，選擇**角色**。在**角色**頁面上，編輯 CodeBuild 組建專案所使用的角色。此角色必須具有下列許可。

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

****  

   ```
   {
     "Version":"2012-10-17",		 	 	 
     "Statement": [
         {
             "Effect": "Allow",
             "Action": [ "codeartifact:GetAuthorizationToken",
                         "codeartifact:GetRepositoryEndpoint",
                         "codeartifact:ReadFromRepository"
                         ],
             "Resource": "*"
         },
         {       
             "Effect": "Allow",
             "Action": "sts:GetServiceBearerToken",
             "Resource": "*",
             "Condition": {
                 "StringEquals": {
                     "sts:AWSServiceName": "codeartifact.amazonaws.com"
                 }
             }
         }
     ]
   }
   ```

------
**重要**  
 如果您也想要使用 CodeBuild 發佈套件，請新增 **codeartifact:PublishPackageVersion**許可。

   如需詳細資訊，請參閱《*IAM 使用者指南*》中的[修改角色](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_manage_modify.html)。

## 使用 NuGet 套件
<a name="consume-nuget-packages-in-codebuild"></a>

若要從 CodeBuild 使用 NuGet 套件，請在專案的 `buildspec.yaml` 檔案中包含下列項目。

1. 在 `install`區段中，安裝 CodeArtifact 登入資料提供者來設定命令列工具，例如 `msbuild`和 `dotnet`，以建置套件並將其發佈至 CodeArtifact。

1. 在 `pre-build`區段中，將 CodeArtifact 儲存庫新增至 NuGet 組態。

請參閱下列`buildspec.yaml`範例。如需詳細資訊，請參閱[搭配 NuGet 使用 CodeArtifact](using-nuget.md)。

安裝登入資料提供者並新增儲存庫來源後，您可以從 `build`區段執行 NuGet CLI 工具命令，以使用 NuGet 套件。

### Linux
<a name="consume-nuget-packages-in-codebuild-linux"></a>

 若要使用 使用 NuGet 套件`dotnet`：

```
version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: latest
    commands:
      - export PATH="$PATH:/root/.dotnet/tools"
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      -  dotnet nuget add source -n codeartifact $(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)"v3/index.json"
  build:
    commands:
      - dotnet add package <packageName> --source codeartifact
```

### Windows
<a name="consume-nuget-packages-in-codebuild-windows"></a>

 若要使用 使用 NuGet 套件`dotnet`：

```
version: 0.2

phases:
  install:
    commands:
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      - dotnet nuget add source -n codeartifact "$(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)v3/index.json"
  build:
    commands:
      - dotnet add package <packageName> --source codeartifact
```

## 使用 NuGet 套件建置
<a name="build-nuget-packages-in-codebuild"></a>

若要從 CodeBuild 使用 NuGet 套件建置 ，請在專案的 `buildspec.yaml` 檔案中包含下列項目。

1. 在 `install`區段中，安裝 CodeArtifact 登入資料提供者來設定命令列工具，例如 `msbuild`和 `dotnet`，以建置套件並將其發佈至 CodeArtifact。

1. 在 `pre-build`區段中，將 CodeArtifact 儲存庫新增至 NuGet 組態。

請參閱下列`buildspec.yaml`範例。如需詳細資訊，請參閱[搭配 NuGet 使用 CodeArtifact](using-nuget.md)。

安裝登入資料提供者並新增儲存庫來源後，您可以從 `dotnet build` `build`區段執行 NuGet CLI 工具命令，例如 。

### Linux
<a name="build-nuget-packages-in-codebuild-linux"></a>

 若要使用 建置 NuGet 套件`dotnet`：

```
version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: latest
    commands:
      - export PATH="$PATH:/root/.dotnet/tools"
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      -  dotnet nuget add source -n codeartifact $(aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format nuget --query repositoryEndpoint --output text)"v3/index.json"
  build:
    commands:
      - dotnet build
```

 若要使用 建置 NuGet 套件`msbuild`：

```
version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: latest
    commands:
      - export PATH="$PATH:/root/.dotnet/tools"
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      - dotnet nuget add source -n codeartifact $(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)"v3/index.json"
  build:
    commands:
      - msbuild -t:Rebuild -p:Configuration=Release
```

### Windows
<a name="build-nuget-packages-in-codebuild-windows"></a>

 若要使用 建置 NuGet 套件`dotnet`：

```
version: 0.2

phases:
  install:
    commands:
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      - dotnet nuget add source -n codeartifact "$(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)v3/index.json"
  build:
    commands:
      - dotnet build
```

 若要使用 建置 NuGet 套件`msbuild`：

```
version: 0.2

phases:
  install:
    commands:
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      - dotnet nuget add source -n codeartifact "$(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)v3/index.json"
  build:
    commands:
      - msbuild -t:Rebuild -p:Configuration=Release
```

## 發佈 NuGet 套件
<a name="publish-nuget-packages-in-codebuild"></a>

若要從 CodeBuild 發佈 NuGet 套件，請在專案的 `buildspec.yaml` 檔案中包含下列項目。

1. 在 `install`區段中，安裝 CodeArtifact 登入資料提供者來設定命令列工具，例如 `msbuild`和 `dotnet`，以建置套件並將其發佈至 CodeArtifact。

1. 在 `pre-build`區段中，將 CodeArtifact 儲存庫新增至 NuGet 組態。

請參閱下列`buildspec.yaml`範例。如需詳細資訊，請參閱[搭配 NuGet 使用 CodeArtifact](using-nuget.md)。

安裝登入資料提供者並新增儲存庫來源後，您可以從 `build`區段執行 NuGet CLI 工具命令，並發佈 NuGet 套件。

### Linux
<a name="publish-nuget-packages-in-codebuild-linux"></a>

 若要使用 發佈 NuGet 套件`dotnet`：

```
version: 0.2

phases:
  install:
    runtime-versions:
      dotnet: latest
    commands:
      - export PATH="$PATH:/root/.dotnet/tools"
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      - dotnet nuget add source -n codeartifact $(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)"v3/index.json"
  build:
    commands:
      - dotnet pack -o .
      - dotnet nuget push *.nupkg -s codeartifact
```

### Windows
<a name="publish-nuget-packages-in-codebuild-windows"></a>

 若要使用 發佈 NuGet 套件`dotnet`：

```
version: 0.2

phases:
  install:
    commands:
      - dotnet tool install -g AWS.CodeArtifact.NuGet.CredentialProvider
      - dotnet codeartifact-creds install
  pre_build:
    commands:
      - dotnet nuget add source -n codeartifact "$(aws codeartifact get-repository-endpoint --domain {{my_domain}} --domain-owner {{111122223333}} --repository {{my_repo}} --format nuget --query repositoryEndpoint --output text)v3/index.json"
  build:
    commands:
      - dotnet pack -o .
      - dotnet nuget push *.nupkg -s codeartifact
```