为 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 片段:以下示例配置了覆盖,其中:

    • actionName,本示例中的 Source,是在创建管道时定义的动态值,不是从源事件派生的。

    • revisionType,本示例中的 COMMIT_ID,是在创建管道时定义的动态值,不是从源事件派生的。

    • revisionValue,本示例中的 <revisionValue>,是从源事件变量派生的。

    • BranchNameValue 的输出变量已指定。

    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. 选择堆栈,然后选择为当前堆栈创建更改集

  6. 上传模板,然后查看 CloudFormation 中列出的更改。这些是要对堆栈进行的更改。您应在列表中看到新资源。

  7. 选择执行

编辑您的管道的 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 } ] },