在中新增跨區域動作 CodePipeline - AWS CodePipeline

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

在中新增跨區域動作 CodePipeline

AWS CodePipeline 包含許多動作,可協助您針對自動發行程序設定建置、測試和部署資源。您可以將動作新增至管道位於與管道不同的 AWS 區域中的管道。當一個 AWS 服務 是動作的提供者,且此動作類型/提供者類型與管線位於不同的 AWS 區域時,這是跨區域動作。

注意

支援跨區域動作,且只能在支援的區 AWS 域中 CodePipeline 建立。如需支援的 AWS 區域清單 CodePipeline,請參閱配額 AWS CodePipeline

您可以使用控制台 AWS CLI、或 AWS CloudFormation 在管道中新增跨區域動作。

注意

中的某些動作類型 CodePipeline 可能只適用於特定 AWS 地區。另請注意,可能有一些 AWS 區域可以使用動作類型,但該動作類型的特定 AWS 提供者不可用。

當您建立或編輯管道時,您必須在管道區域中擁有一個成品儲存貯體,然後對於每個您計劃執行動作的區域,都必須擁有一個成品儲存貯體。如需 ArtifactStores 參數的詳細資訊,請參閱 CodePipeline 配管結構參照

注意

CodePipeline 執行跨區域動作時,會處理從一個「 AWS 區域」複製到其他區域的人工因素。

如果您使用主控台建立管線或跨區域動作,則會在您執行動作的區域 CodePipeline 中設定預設成品值區。當您使用 AWS CLI AWS CloudFormation、或 SDK 建立管道或跨區域動作時,您會為每個執行動作的「區域」提供成品值區。

注意

您必須在與「跨區域」動作相同的「 AWS 區域」中建立成品儲存貯體和加密金鑰,並在與管道相同的帳戶中建立。

您無法針對以下動作類型建立跨區域動作:

  • 來源動作

  • 第三方動作

  • 自訂動作

注意

在中使用跨區域 Lambda 叫用動作時 CodePipeline,使用PutJobSuccessResult和的 lambda 執行狀態PutJobFailureResult應傳送至存在 Lambda 函數的 AWS 區域,而不是傳送至 CodePipeline 存在 Lambda 函數的區域。

當管線包含跨區域動作作為階段的一部分時,只 CodePipeline 會將「跨區域」動作的輸入成品從管線「區域」複寫到動作的「區域」。

注意

管道區域和 CloudWatch 事件變更偵測資源所在的區域保持不變。管道託管所在的區域不會變更。

在管道中管理跨區域動作 (主控台)

您可以使用 CodePipeline 控制台將跨區域動作新增至現有管線。若要使用建立管道精靈來建立具有跨區域動作的新管道,請參閱建立管道 (主控台)

在主控台中,您可以選擇動作提供者和 Region (區域) 欄位 (其中列出您在該區域中為該提供者建立的資源),在管道階段中建立跨區域動作。新增「跨區域」動作時, CodePipeline會在動作的區域中使用個別的成品值區域。如需跨區域成品儲存貯體的詳細資訊,請參閱 CodePipeline 配管結構參照

新增跨區域動作至管道階段 (主控台)

使用主控台將跨區域動作新增至管道。

注意

如果儲存變更時管道正在執行中,該項執行便不會完成。

新增跨區域動作
  1. http://console.aws.amazon.com/codesuite/codepipeline/home 登入主控台。

  2. 選取您的管道,然後選擇 Edit (編輯)

  3. 如果您要新增新的階段,請在圖表底部選擇 + Add stage (新增階段),或是如果您希望新增動作到現有的階段中,請選擇 Edit stage (編輯階段)

  4. Edit: <Stage> (編輯:<階段>) 上,選擇 + Add action group (新增動作群組) 以新增序列動作。或者,選擇 + Add action (新增動作) 以新增平行動作。

  5. Edit action (編輯動作) 頁面:

    1. Action name (動作名稱) 中,輸入跨區域動作的名稱。

    2. Action provider (動作供應商),選擇動作供應商。

    3. 在「區域」中,選擇您已建立或計劃為動作建立資源的「 AWS 區域」。當選定區域時,會列出該區域可用的資源以供選擇。[Re gion] 欄位會指定針對此動作類型和提供者類型建立 AWS 資源的位置。此欄位僅針對動作提供者為的動作顯示 AWS 服務。「區域」(Region) 欄位預設 AWS 區域 為與您的管道相同。

    4. Input artifacts (輸入成品) 中,選擇前一階段的適當輸入。例如,如果上一個階段是來源階段,請選擇SourceArtifact

    5. 為您設定的動作供應商完成所有必要的欄位。

    6. Output artifacts (輸出成品) 中,選擇下一階段的適當輸出。例如,如果下一個階段是部署階段,請選擇BuildArtifact

    7. 選擇儲存

  6. Edit: <Stage> (編輯:<階段>) 上,選擇 Done (完成)

  7. 選擇儲存

編輯管道階段中的跨區域動作 (主控台)

使用主控台來編輯管道中現有的跨區域動作。

注意

如果儲存變更時管道正在執行中,該項執行便不會完成。

如何編輯跨區域動作
  1. 前往 https://console.aws.amazon.com/codesuite/codepipeline/home. 登入主控台

  2. 選取您的管道,然後選擇 Edit (編輯)

  3. 選擇 Edit stage (編輯階段)

  4. Edit: <Stage> (編輯:<階段>) 上,選擇圖示以編輯現有動作。

  5. Edit action (編輯動作) 頁面上,適當地對欄位進行變更。

  6. Edit: <Stage> (編輯:<階段>) 上,選擇 Done (完成)

  7. 選擇儲存

從管道階段刪除跨區域動作 (主控台)

使用主控台從管道刪除現有的跨區域動作。

注意

如果儲存變更時管道正在執行中,該項執行便不會完成。

刪除跨區域動作
  1. http://console.aws.amazon.com/codesuite/codepipeline/home 登入主控台。

  2. 選取您的管道,然後選擇 Edit (編輯)

  3. 選擇 Edit stage (編輯階段)

  4. Edit: <Stage> (編輯:<階段>) 上,選擇圖示以刪除現有動作。

  5. Edit: <Stage> (編輯:<階段>) 上,選擇 Done (完成)

  6. 選擇儲存

將跨區域動作新增至管道 (CLI)

您可以使用 AWS CLI 將「跨區域」動作新增至現有管線。

若要使用在管線階段中建立跨區域動作 AWS CLI,請新增組態動作以及選用region欄位。您亦須已在動作的區域中建立成品儲存貯體。您不用提供單一區域管道的 artifactStore 參數,而是利用 artifactStores 參數,包含每個區域的成品儲存貯體清單。

注意

在這個逐步解說及其範例中,RegionA 是建立管道的區域。它可以存取用於存放管道成品的 RegionA Amazon S3 儲存貯體以及所 CodePipeline使用的服務角色。Region B 是建立所使用之 CodeDeploy 應用程式、部署群組和服務角色的 CodeDeploy 區域。

必要條件

您必須已建立下列項目:

  • RegionA 中的管道。

  • Region B 中的 Amazon S3 神器存儲桶。

  • Re gionB 中的動作資源,例如跨區域部署動作的 CodeDeploy 應用程式和部署群組。

將跨區域動作新增至管道 (CLI)

使用 AWS CLI 可將跨區域動作新增至管線。

新增跨區域動作
  1. 對於 RegionA 中的管道,執行 get-pipeline 命令,以將管道結構複製到 JSON 檔案。例如,針對名為 MyFirstPipeline 的管道,執行下列命令:

    aws codepipeline get-pipeline --name MyFirstPipeline >pipeline.json

    此命令不會傳回任何內容,但您建立的檔案應該會顯示在您執行命令的目錄中。

  2. 新增 region 欄位來新增階段與跨區域動作,其中包括動作的區域和資源。下列 JSON 範例會在新區域中新增具有跨區域部署動作的部署階段 CodeDeploy,其中提供者所在的位us-east-1置。

    { "name": "Deploy", "actions": [ { "inputArtifacts": [ { "name": "SourceArtifact" } ], "name": "Deploy", "region": "RegionB", "actionTypeId": { "category": "Deploy", "owner": "AWS", "version": "1", "provider": "CodeDeploy" }, "outputArtifacts": [], "configuration": { "ApplicationName": "name", "DeploymentGroupName": "name" }, "runOrder": 1 }
  3. 在管道結構中,移除 artifactStore 欄位,並為新的跨區域動作新增 artifactStores 對應。對映必須包含您執行動作之每個「 AWS 區域」的項目。對於映射中的每個項目,資源必須位於個別 AWS 區域中。在以下範例中,ID-ARegionA 的加密金鑰 ID,而 ID-BRegionB 的加密金鑰 ID。

    "artifactStores":{ "RegionA":{ "encryptionKey":{ "id":"ID-A", "type":"KMS" }, "location":"Location1", "type":"S3" }, "RegionB":{ "encryptionKey":{ "id":"ID-B", "type":"KMS" }, "location":"Location2", "type":"S3" } }

    以下 JSON 範例將 us-west-2 儲存貯體顯示為 my-storage-bucket,並新增命名 my-storage-bucket-us-east-1 的 us-east-1 儲存貯體。

    "artifactStores": { "us-west-2": { "type": "S3", "location": "my-storage-bucket" }, "us-east-1": { "type": "S3", "location": "my-storage-bucket-us-east-1" } },
  4. 如果您使用的是使用 get-pipeline 命令擷取的管道結構,請從 JSON 檔案中移除 metadata 行。否則,update-pipeline 命令無法使用它。移除 "metadata": { } 行,以及 "created""pipelineARN""updated" 欄位。

    例如,從結構中移除下列幾行:

    "metadata": { "pipelineArn": "arn:aws:codepipeline:region:account-ID:pipeline-name", "created": "date", "updated": "date" }

    儲存檔案。

  5. 若要套用您的變更,請執行 update-pipeline 命令、指定管道 JSON 檔案:

    重要

    請確認在檔案名稱之前包含 file://。這是此命令必要項目。

    aws codepipeline update-pipeline --cli-input-json file://pipeline.json

    此命令會傳回所編輯管道的整個結構。輸出類似如下。

    { "pipeline": { "version": 4, "roleArn": "ARN", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "Source", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "CodeCommit" }, "outputArtifacts": [ { "name": "SourceArtifact" } ], "configuration": { "PollForSourceChanges": "false", "BranchName": "main", "RepositoryName": "MyTestRepo" }, "runOrder": 1 } ] }, { "name": "Deploy", "actions": [ { "inputArtifacts": [ { "name": "SourceArtifact" } ], "name": "Deploy", "region": "us-east-1", "actionTypeId": { "category": "Deploy", "owner": "AWS", "version": "1", "provider": "CodeDeploy" }, "outputArtifacts": [], "configuration": { "ApplicationName": "name", "DeploymentGroupName": "name" }, "runOrder": 1 } ] } ], "name": "AnyCompanyPipeline", "artifactStores": { "us-west-2": { "type": "S3", "location": "my-storage-bucket" }, "us-east-1": { "type": "S3", "location": "my-storage-bucket-us-east-1" } } } }
    注意

    update-pipeline 命令將終止管道。若在您執行 update-pipeline 命令時有修訂正在透過管道執行,該執行將停止。您必須手動啟動管道,以透過更新的管道執行該修訂。使用 start-pipeline-execution 命令來手動啟動您的管道。

  6. 更新管線後,「跨區域」動作會顯示在主控台中。

    管道的高階檢視,其中包含跨區域動作。

將跨區域動作新增至管道 (AWS CloudFormation)

您可以使用 AWS CloudFormation 將「跨區域」動作新增至現有管線。

若要新增跨區域動作,請使用 AWS CloudFormation
  1. Region 參數新增到範本中的 ActionDeclaration 資源,如以下範例所示:

    ActionDeclaration: Type: Object Properties: ActionTypeId: Type: ActionTypeId Required: true Configuration: Type: Map InputArtifacts: Type: Array ItemType: Type: InputArtifact Name: Type: String Required: true OutputArtifacts: Type: Array ItemType: Type: OutputArtifact RoleArn: Type: String RunOrder: Type: Integer Region: Type: String
  2. Mappings 下,新增區域地圖,如這個範例所示,其中名為 SecondRegionMap 的映射會對映金鑰 RegionARegionB 的值。在 Pipeline 資源下,於 artifactStore 欄位下,為新的跨區域動作新增 artifactStores 對應,如下所示:

    Mappings: SecondRegionMap: RegionA: SecondRegion: "RegionB" RegionB: SecondRegion: "RegionA" ... Properties: ArtifactStores: - Region: RegionB ArtifactStore: Type: "S3" Location: test-cross-region-artifact-store-bucket-RegionB - Region: RegionA ArtifactStore: Type: "S3" Location: test-cross-region-artifact-store-bucket-RegionA

    下列 YAML 範例會將 RegionA 儲存貯體顯示為 us-west-2,並新增 RegionB 儲存貯體 eu-central-1

    Mappings: SecondRegionMap: us-west-2: SecondRegion: "eu-central-1" eu-central-1: SecondRegion: "us-west-2" ... Properties: ArtifactStores: - Region: eu-central-1 ArtifactStore: Type: "S3" Location: test-cross-region-artifact-store-bucket-eu-central-1 - Region: us-west-2 ArtifactStore: Type: "S3" Location: test-cross-region-artifact-store-bucket-us-west-2
  3. 將更新的範本儲存到本機電腦,然後開啟 AWS CloudFormation 主控台。

  4. 選擇您的堆疊,然後選擇 Create Change Set for Current Stack (建立目前堆疊的變更集)

  5. 上傳範本,然後檢視 AWS CloudFormation中所列的變更。這些是會針對堆疊進行的變更。您應該會在清單中看到新資源。

  6. 選擇 Execute (執行)