本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
建立 CodeCommit 來源 (AWS CloudFormation 範本) 的 EventBridge 規則
若要用 AWS CloudFormation 來建立規則,請更新範本,如下所示。
更新管線 AWS CloudFormation 範本並建立 EventBridge 規則
-
在範本中的下Resources
,使用AWS::IAM::Role
AWS CloudFormation 資源來配置允許事件啟動管道的IAM角色。此項目會建立一個使用兩個政策的角色:
-
第一個政策允許要承擔的角色。
-
第二個政策提供啟動管道的許可。
為什麼我會做出此變更? 新增資AWS::IAM::Role
源可 AWS CloudFormation 建立的權限 EventBridge。此資源會新增至您的 AWS 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"
}
]
...
-
在範本的下Resources
,使用資AWS::Events::Rule
AWS CloudFormation 源新增 EventBridge 規則。此事件模式會建立事件,以監視存放庫的推送變更。 EventBridge 偵測到存放庫狀態變更時,規則會StartPipelineExecution
在您的目標管線上叫用。
我為什麼要進行這項變更? 新增資AWS::Events::Rule
源可 AWS CloudFormation 建立事件。此資源會新增至您的 AWS 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"
}
]
}
},
-
將更新的範本儲存到本機電腦,然後開啟 AWS CloudFormation
主控台。
-
選擇您的堆疊,然後選擇 Create Change Set for Current Stack (建立目前堆疊的變更集)。
-
上傳範本,然後檢視 AWS CloudFormation中所列的變更。這些是會針對堆疊進行的變更。您應該會在清單中看到新資源。
-
選擇 Execute (執行)。
若要編輯管線的 PollForSourceChanges參數
在許多情況下,當您建立管道時,PollForSourceChanges
參數會預設為 true。當新增基於事件的變更偵測時,您必須將該參數新增到輸出,並將其設為 false 以停用輪詢。否則,您的管道會針對單一來源變更啟動兩次。如需詳細資訊,請參閱 PollForSourceChanges 參數的有效設定。