はじめに
DevOps(CICD)の一環でCode CommitのPRがマージされた時にBuild・Deployを実行させるような仕組みを構築したのでその備忘録を残す
仕組み
Cloud Watch EventsのEventオブジェクトの中身
例えば、devブランチからmainブランチへのPRのマージを行うと以下のようなEventオブジェクトがLambda関数に渡る
{
"version": "0",
"id": "c0931ce3-ee9f-d9f5-205c-a0ab8e3c0f9f",
"detail-type": "CodeCommit Pull Request State Change",
"source": "aws.codecommit",
"account": "xxxxxxxxxxxx",
"time": "2021-08-16T08:32:25Z",
"region": "ap-northeast-1",
"resources": [
"arn:aws:codecommit:ap-northeast-1:xxxxxxxxxxxx:hoge-test"
],
"detail": {
"author": "arn:aws:iam::xxxxxxxxxxxx:user/----------",
"callerUserArn": "arn:aws:iam::xxxxxxxxxxxx:user/----------",
"creationDate": "Mon Aug 16 08:31:23 UTC 2021",
"destinationCommit": "dfdaf17904c816c9826d431187b4746d21406319",
"destinationReference": "refs/heads/main",
"event": "pullRequestMergeStatusUpdated",
"isMerged": "True",
"lastModifiedDate": "Mon Aug 16 08:32:14 UTC 2021",
"mergeOption": "FAST_FORWARD_MERGE",
"notificationBody": "A pull request event occurred in the following AWS CodeCommit repository: hoge-test. User: arn:aws:iam::xxxxxxxxxxxx:user/----------. Event: Updated. Pull request name: 4. Additional information: The pull request merge status has been updated. The status is merged. For more information, go to the AWS CodeCommit console https://ap-northeast-1.console.aws.amazon.com/codesuite/codecommit/repositories/hoge-test/pull-requests/4?region=ap-northeast-1.",
"pullRequestId": "4",
"pullRequestStatus": "Closed",
"repositoryNames": ["hoge-test"],
"revisionId": "9dc053117e28d1da9cbb7044eddb2c6e815fc978d61183e8cb62a59997599037",
"sourceCommit": "dced724ba1e54ef52319050306015d23a47f42d2",
"sourceReference": "refs/heads/dev",
"title": "test"
}
}
上記のJSONの中でPRマージ時を判定するには
- "isMerged": "True"
- "event": "pullRequestMergeStatusUpdated"
- "pullRequestStatus": "Closed"
あたりのKeyで値がそれぞれ上記の値であればPRマージ時という判別が可能になる
・参考:pullRequestMergeStatusUpdated イベント
Lambdaをトリガーする設定
以下の記事を参照
Lambdaの実装
以下の記事の内容を参照(LambdaでEventオブジェクトを受け取る部分の実装は全く同じでevent.detail.・・・
のようにして、isMerged
やevent
、pullRequestStatus
といったキーの値を取得する)