為 CodeCommit 來源建立 EventBridge 規則 (CloudFormation 範本) - AWS CodePipeline

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

為 CodeCommit 來源建立 EventBridge 規則 (CloudFormation 範本)

若要使用 CloudFormation 建立規則,請更新您的範本,如下所示。

更新您的管道 CloudFormation 範本並建立 EventBridge 規則
  1. 在 範本的 下Resources,使用 AWS::IAM::Role CloudFormation 資源來設定允許事件啟動管道的 IAM 角色。此項目會建立一個使用兩個政策的角色:

    • 第一個政策允許要承擔的角色。

    • 第二個政策提供啟動管道的許可。

    為什麼我會做出此變更? 新增 AWS::IAM::Role 資源 CloudFormation 可讓 建立 EventBridge 的許可。此資源會新增至您的 CloudFormation 堆疊。

    YAML
    EventRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - events.amazonaws.com Action: sts:AssumeRole Path: / Policies: - PolicyName: eb-pipeline-execution PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: codepipeline:StartPipelineExecution Resource: !Join [ '', [ 'arn:aws:codepipeline:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref AppPipeline ] ]
    JSON
    "EventRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": [ "events.amazonaws.com" ] }, "Action": "sts:AssumeRole" } ] }, "Path": "/", "Policies": [ { "PolicyName": "eb-pipeline-execution", "PolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "codepipeline:StartPipelineExecution", "Resource": { "Fn::Join": [ "", [ "arn:aws:codepipeline:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":", { "Ref": "AppPipeline" } ] ...
  2. 在 範本的 下Resources,使用 AWS::Events::Rule CloudFormation 資源來新增 EventBridge 規則。此事件模式會建立監控推送變更至儲存庫的事件。當 EventBridge 偵測到儲存庫狀態變更時,規則會在您的目標管道StartPipelineExecution上叫用 。

    為什麼要進行這項變更? 新增 AWS::Events::Rule 資源 CloudFormation 可讓 建立事件。此資源會新增至您的 CloudFormation 堆疊。

    YAML
    EventRule: Type: AWS::Events::Rule Properties: EventPattern: source: - aws.codecommit detail-type: - 'CodeCommit Repository State Change' resources: - !Join [ '', [ 'arn:aws:codecommit:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref RepositoryName ] ] detail: event: - referenceCreated - referenceUpdated referenceType: - branch referenceName: - main Targets: - Arn: !Join [ '', [ 'arn:aws:codepipeline:', !Ref 'AWS::Region', ':', !Ref 'AWS::AccountId', ':', !Ref AppPipeline ] ] RoleArn: !GetAtt EventRole.Arn Id: codepipeline-AppPipeline
    JSON
    "EventRule": { "Type": "AWS::Events::Rule", "Properties": { "EventPattern": { "source": [ "aws.codecommit" ], "detail-type": [ "CodeCommit Repository State Change" ], "resources": [ { "Fn::Join": [ "", [ "arn:aws:codecommit:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":", { "Ref": "RepositoryName" } ] ] } ], "detail": { "event": [ "referenceCreated", "referenceUpdated" ], "referenceType": [ "branch" ], "referenceName": [ "main" ] } }, "Targets": [ { "Arn": { "Fn::Join": [ "", [ "arn:aws:codepipeline:", { "Ref": "AWS::Region" }, ":", { "Ref": "AWS::AccountId" }, ":", { "Ref": "AppPipeline" } ] ] }, "RoleArn": { "Fn::GetAtt": [ "EventRole", "Arn" ] }, "Id": "codepipeline-AppPipeline" } ] } },
  3. (選用) 若要為特定映像 ID 設定具有來源覆寫的輸入轉換器,請使用下列 YAML 程式碼片段。下列範例會設定覆寫,其中:

    • 在此actionNameSource範例中, 是在管道建立時定義的動態值,不是衍生自來源事件。

    • 在此revisionTypeCOMMIT_ID範例中, 是在管道建立時定義的動態值,不是衍生自來源事件。

    • 此範例中的 revisionValue<revisionValue> 衍生自來源事件變數。

    • Value 指定 BranchName和 的輸出變數。

    Rule: my-rule Targets: - Id: MyTargetId Arn: pipeline-ARN InputTransformer: sourceRevisions: actionName: Source revisionType: COMMIT_ID revisionValue: <revisionValue> variables: - name: BranchName value: value
  4. 將更新後的範本儲存至本機電腦,然後開啟 CloudFormation 主控台。

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

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

  7. 選擇 Execute (執行)

編輯管道的 PollForSourceChanges 參數
重要

在許多情況下,當您建立管道時,PollForSourceChanges 參數會預設為 true。當新增基於事件的變更偵測時,您必須將該參數新增到輸出,並將其設為 false 以停用輪詢。否則,您的管道會針對單一來源變更啟動兩次。如需詳細資訊,請參閱PollForSourceChanges 參數的有效設定

  • 在範本中,將 PollForSourceChanges 變更為 false。如果您並未在管道定義中包含 PollForSourceChanges,請新增它,並將其設為 false

    為什麼我會做出此變更? 將此參數變更為 false 會關閉定期檢查,因此您只能使用事件型變更偵測。

    YAML
    Name: Source Actions: - Name: SourceAction ActionTypeId: Category: Source Owner: AWS Version: 1 Provider: CodeCommit OutputArtifacts: - Name: SourceOutput Configuration: BranchName: !Ref BranchName RepositoryName: !Ref RepositoryName PollForSourceChanges: false RunOrder: 1
    JSON
    { "Name": "Source", "Actions": [ { "Name": "SourceAction", "ActionTypeId": { "Category": "Source", "Owner": "AWS", "Version": 1, "Provider": "CodeCommit" }, "OutputArtifacts": [ { "Name": "SourceOutput" } ], "Configuration": { "BranchName": { "Ref": "BranchName" }, "RepositoryName": { "Ref": "RepositoryName" }, "PollForSourceChanges": false }, "RunOrder": 1 } ] },