

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

# 在 CodeCommit 中自動偵測變更並啟動單一儲存庫的不同 CodePipeline 管道 CodeCommit
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit"></a>

*Helton Ribeiro、Petrus Batalha 和 Amazon Web Services Ricardo Morais*

## 總結
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-summary"></a>

**注意**： AWS Cloud9 不再提供給新客戶。的現有客戶 AWS Cloud9 可以繼續正常使用服務。[進一步了解](https://aws.amazon.com/blogs/devops/how-to-migrate-from-aws-cloud9-to-aws-ide-toolkits-or-aws-cloudshell/)

此模式可協助您在 中自動偵測單一儲存庫型應用程式的原始碼變更， AWS CodeCommit 然後在 中啟動管道 AWS CodePipeline ，以針對每個微服務執行持續整合和持續交付 (CI/CD) 自動化。此方法表示單一儲存庫型應用程式中的每個微服務都可以有專用的 CI/CD 管道，以確保更佳的可見性、更輕鬆地共用程式碼，並改善協同合作、標準化和可探索性。

此模式中描述的解決方案不會在 monorepo 內的微服務之間執行任何相依性分析。它只會偵測原始程式碼中的變更，並啟動相符的 CI/CD 管道。

模式使用 AWS Cloud9 做為整合式開發環境 (IDE) AWS Cloud Development Kit (AWS CDK) ，並使用兩個 CloudFormation 堆疊定義基礎設施： `MonoRepoStack`和 `PipelinesStack`。`MonoRepoStack` 堆疊會在 中建立單儲存庫， AWS CodeCommit 以及啟動 CI/CD 管道的 AWS Lambda 函數。`PipelinesStack` 堆疊會定義您的管道基礎設施。

**重要**  
此模式的工作流程是一種概念驗證 (PoC)。建議您只在測試環境中使用它。如果您想要在生產環境中使用此模式的方法，請參閱《 AWS Identity and Access Management (IAM) 文件[》中的 IAM 中的安全最佳實務](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html)，並對 IAM 角色和 進行必要的變更 AWS 服務。 

## 先決條件和限制
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-prereqs"></a>

**先決條件**
+ 作用中 AWS 的帳戶。
+ AWS Command Line Interface (AWS CLI)，已安裝並設定。如需詳細資訊，請參閱 AWS CLI 文件[AWS CLI中的安裝、更新和解除安裝](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) 。 
+ Python 3 和 `pip`，安裝在本機電腦上。如需詳細資訊，請參閱 [Python 文件](https://www.python.org/)。 
+ AWS CDK，已安裝並設定。如需詳細資訊，請參閱 AWS CDK 文件中的[開始使用 AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html)。 
+ 安裝和設定的 AWS Cloud9 IDE。如需詳細資訊，請參閱 AWS Cloud9 文件中的[設定 AWS Cloud9](https://docs.aws.amazon.com/cloud9/latest/user-guide/setting-up.html)。 
+ GitHub [AWS CodeCommit monorepo 多管道觸發程式](https://github.com/aws-samples/monorepo-multi-pipeline-trigger)儲存庫，在您的本機電腦上複製。 
+ 包含您要使用 CodePipeline 建置和部署之應用程式程式碼的現有目錄。
+ 具備 DevOps 最佳實務的熟悉度和經驗 AWS 雲端。若要提高您對 DevOps 的熟悉度，您可以使用 模式 [使用 DevOps 實務和 規範指引網站建置鬆散耦合的架構與微服務 AWS Cloud9](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/build-a-loosely-coupled-architecture-with-microservices-using-devops-practices-and-aws-cloud9.html)。 AWS  

## Architecture
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-architecture"></a>

下圖顯示如何使用 AWS CDK 來定義具有兩個 AWS CloudFormation 堆疊的基礎設施： `MonoRepoStack`和 `PipelinesStack`。

![\[使用 AWS CDK 定義具有兩個 CloudFormation 堆疊之基礎設施的工作流程。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/a3397158-a208-4033-844e-969af13ae8b6/images/b0bb1094-b598-4b3d-ab8b-ad9b0eb45f38.png)


該圖顯示以下工作流程：

1. 引導程序會使用 AWS CDK 來建立 AWS CloudFormation 堆疊 `MonoRepoStack`和 `PipelinesStack`。

1. `MonoRepoStack` 堆疊會為您的應用程式建立 CodeCommit 儲存庫，以及在每次遞交後啟動的 `monorepo-event-handler` Lambda 函數。

1. `PipelinesStack` 堆疊會在 CodePipeline 中建立由 Lambda 函數啟動的管道。每個微服務都必須有定義的基礎設施管道。

1. 的管道由 Lambda 函數`microservice-n`啟動，並啟動以 CodeCommit 中的原始程式碼為基礎的隔離 CI/CD 階段。

1. 的管道由 Lambda 函數`microservice-1`啟動，並啟動以 CodeCommit 中的原始程式碼為基礎的隔離 CI/CD 階段。

下圖顯示 `PipelinesStack`帳戶中 AWS CloudFormation 堆疊 `MonoRepoStack`和 的部署。

![\[在 AWS 帳戶中部署 CloudFormation 堆疊 MonoRepoStack 和 PipelinesStack。\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/images/pattern-img/a3397158-a208-4033-844e-969af13ae8b6/images/39e60e49-dea2-486d-8a2c-6cae438f69b4.png)


1. 使用者變更其中一個應用程式的微服務中的程式碼。

1. 使用者會將變更從本機儲存庫推送到 CodeCommit 儲存庫。

1. 推送活動會啟動接收所有推送至 CodeCommit 儲存庫的 Lambda 函數。

1. Lambda 函數會讀取 參數存放區中的參數，這是 的一項功能 AWS Systems Manager，用於擷取最新的遞交 ID。參數具有命名格式：`/MonoRepoTrigger/{repository}/{branch_name}/LastCommit`。如果找不到 參數，Lambda 函數會從 CodeCommit 儲存庫讀取最後一個遞交 ID，並將傳回的值儲存在參數存放區中。

1. 識別遞交 ID 和變更的檔案之後，Lambda 函數會識別每個微服務目錄的管道，並啟動所需的 CodePipeline 管道。

## 工具
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-tools"></a>
+ [AWS Cloud Development Kit (AWS CDK)](https://docs.aws.amazon.com/cdk/latest/guide/home.html) 是一種軟體開發架構，用於在程式碼中定義雲端基礎設施並透過其佈建 CloudFormation。
+ [Python](https://www.python.org/) 是一種程式設計語言，可讓您快速工作並更有效地整合系統。

**Code**

此模式的原始程式碼和範本可在 GitHub [AWS CodeCommit monorepo 多管道觸發](https://github.com/aws-samples/monorepo-multi-pipeline-trigger)程式儲存庫中使用。

## 最佳實務
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-best-practices"></a>
+ 此範例架構不包含已部署基礎設施的監控解決方案。如果您想要在生產環境中部署此解決方案，建議您啟用監控。如需詳細資訊，請參閱《 AWS Serverless Application Model (AWS SAM) 文件》中的[使用 CloudWatch Application Insights 監控無伺服器應用程式](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/monitor-app-insights.html)。
+ 當您編輯此模式提供的範例程式碼時，請遵循 AWS CDK 文件中[開發和部署雲端基礎設施的最佳實務](https://docs.aws.amazon.com/cdk/v2/guide/best-practices.html)。
+ 當您定義微服務管道時，請檢閱 AWS CodePipeline 文件中的[安全最佳實務](https://docs.aws.amazon.com/codepipeline/latest/userguide/security-best-practices.html)。
+ 您也可以使用 [cdk-nag](https://github.com/cdklabs/cdk-nag) 公用程式來檢查 AWS CDK 程式碼是否有最佳實務。此工具使用一組依套件分組的規則來評估您的程式碼。可用的套件包括：
  + [AWS 解決方案程式庫](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#awssolutions)
  + [健康保險流通與責任法案 (HIPAA) 安全性](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#hipaa-security)
  + [國家標準技術研究所 (NIST) 800-53 修訂版 4](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#nist-800-53-rev-4)
  + [NIST 800-53 修訂版 5](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#nist-800-53-rev-5)
  + [支付卡產業資料安全標準 (PCI DSS) 3.2.1](https://github.com/cdklabs/cdk-nag/blob/main/RULES.md#pci-dss-321)

## 史詩
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-epics"></a>

### 設定環境
<a name="set-up-the-environment"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 建立虛擬 Python 環境。 | 在您的 AWS Cloud9 IDE 中，透過執行下列命令，建立虛擬 Python 環境並安裝所需的相依性：`make install` | 開發人員 | 
|  AWS 區域 為 引導 AWS 帳戶 和 AWS CDK。 | 執行下列命令來引導所需的 AWS 帳戶 和 區域：`make bootstrap account-id=<your-AWS-account-ID> region=<required-region>` | 開發人員 | 

### 新增微服務的新管道
<a name="add-a-new-pipeline-for-a-microservice"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
|  將範例程式碼新增至應用程式目錄。 | 將包含範例應用程式程式碼的目錄新增至複製的 GitHub [AWS CodeCommit monorepo 多管道觸發](https://github.com/aws-samples/monorepo-multi-pipeline-trigger)程式儲存庫中的`monorepo-sample`目錄。 | 開發人員 | 
| 編輯 `monorepo-main.json` 檔案。 | 將應用程式程式碼的目錄名稱和管道名稱新增至複製儲存庫 中的 `monorepo-main.json` 檔案。 | 開發人員 | 
| 建立管道。 | 在儲存庫的 `Pipelines`目錄中，`class`為您的應用程式新增管道。目錄包含兩個範例檔案 `pipeline_hotsite.py`和 `pipeline_demo.py`。每個檔案都有三個階段：來源、建置和部署。您可以複製其中一個檔案，並根據應用程式的需求對其進行變更。  | 開發人員 | 
| 編輯 `monorepo_config.py` 檔案。 | 在 中`service_map`，為您的應用程式新增目錄名稱，以及您為管道建立的類別。例如，下列程式碼顯示 `Pipelines`目錄中的管道定義，該定義使用名為 的檔案`pipeline_mysample.py`搭配 `MySamplePipeline`類別：<pre>...<br /># Pipeline definition imports<br />from pipelines.pipeline_demo import DemoPipeline<br />from pipelines.pipeline_hotsite import HotsitePipeline<br />from pipelines.pipeline_mysample import MySamplePipeline<br /><br />### Add your pipeline configuration here<br />service_map: Dict[str, ServicePipeline]  = {<br />    # folder-name -> pipeline-class<br />    'demo': DemoPipeline(),<br />    'hotsite': HotsitePipeline(),<br />    'mysample': MySamplePipeline()<br />}</pre> | 開發人員 | 

### 部署 MonoRepoStack 堆疊
<a name="deploy-the-monorepostack-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 AWS CloudFormation 堆疊。 | 執行 AWS CloudFormation `MonoRepoStack``make deploy-core`命令，在複製儲存庫的根目錄中部署具有預設參數值的堆疊。您可以執行 `make deploy-core monorepo-name=<repo_name>`命令來變更儲存庫的名稱。您可以使用 `make deploy monorepo-name=<repo_name>`命令同時部署這兩個管道。 | 開發人員 | 
| 驗證 CodeCommit 儲存庫。 | 透過執行 `aws codecommit get-repository --repository-name <repo_name>`命令來驗證您的資源是否已建立。由於 CloudFormation 堆疊會建立儲存 monorepo 的 CodeCommit 儲存庫，因此如果您已開始將修改推送至其中，請勿執行 `cdk destroy MonoRepoStack `命令。 | 開發人員 | 
| 驗證 CloudFormation 堆疊結果。 | 執行下列命令， CloudFormation `MonoRepoStack`驗證堆疊是否已正確建立和設定：<pre>aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE --query 'StackSummaries[?StackName == 'MonoRepoStack']'</pre> | 開發人員 | 

### 部署 PipelinesStack 堆疊
<a name="deploy-the-pipelinesstack-stack"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 部署 CloudFormation 堆疊。 | 部署 AWS CloudFormation `PipelinesStack`堆疊之後，必須部署`MonoRepoStack`堆疊。將新的微服務新增至 monorepo 的程式碼基底時，堆疊的大小會增加，並在加入新的微服務時重新部署。執行 `make deploy-pipelines`命令來部署 PipelinesStack 堆疊。您也可以執行 `make deploy monorepo-name=<repo_name>`命令，同時部署這兩個管道。下列範例輸出顯示`PipelinesStacks`部署如何在實作結束時列印微服務 URLs：<pre>Outputs:<br />PipelinesStack.demourl = .cloudfront.net<br />PipelinesStack.hotsiteurl = .cloudfront.net</pre> | 開發人員 | 
| 驗證 AWS CloudFormation 堆疊結果。 | 執行下列命令， AWS CloudFormation `PipelinesStacks`驗證堆疊是否已正確建立和設定：<pre>aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE --query 'StackSummaries[?StackName == 'PipelinesStack']'</pre> | 開發人員 | 

### 清除資源
<a name="clean-up-resources"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 刪除您的 AWS CloudFormation 堆疊。 | 執行 `make destroy` 命令。 | 開發人員 | 
| 刪除管道的 S3 儲存貯體。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html) | 開發人員 | 

## 疑難排解
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| 我遇到 AWS CDK 問題。 | 請參閱 AWS CDK 文件中的[疑難排解 AWS CDK 常見問題](https://docs.aws.amazon.com/cdk/v2/guide/troubleshooting.html)。 | 
| 我推送了微服務程式碼，但微服務管道未執行。 | **設定驗證***驗證分支組態：*[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)*驗證組態檔案：*[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)**在主控台進行故障診斷***AWS CodePipeline 檢查：*[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)*AWS Lambda 故障診斷：*[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html) | 
| 我需要重新部署所有微服務。 | 強制重新部署所有微服務的方法有兩種。選擇符合您需求的選項。**方法 1：刪除參數存放區中的參數**此方法涉及在 Systems Manager 參數存放區中刪除特定參數，以追蹤用於部署的最後一個遞交 ID。當您移除此參數時，系統會強制在下一次觸發時重新部署所有微服務，因為它會將其視為全新狀態。步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)優點：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)缺點：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)**方法 2：在每個 monorepo 子資料夾中推送遞交**此方法涉及進行次要變更，並在單一儲存庫中的每個微服務子資料夾中推送，以啟動其個別管道。步驟：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)優點：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html)缺點：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit.html) | 

## 相關資源
<a name="automatically-detect-changes-and-initiate-different-codepipeline-pipelines-for-a-monorepo-in-codecommit-resources"></a>
+ [使用 CDK 管道的持續整合和交付 (CI/CD)](https://docs.aws.amazon.com/cdk/latest/guide/cdk_pipeline.html) (AWS CDK 文件）
+ [aws-cdk/pipelines 模組](https://docs.aws.amazon.com/cdk/api/latest/docs/pipelines-readme.html) (AWS CDK API 參考）