はじめに
プルリクエストで提案されたコードの変更を、マージの前にテストすることで、コードの品質を保ち、動かないコードをマージするリスクを回避する事ができます。
AWS 認定 DevOps Engineer Professionalの対策をしていると、以下のブログを発見し、
今ならもっとシンプルな構成で作れるのではと思い構築してみました。
Validating AWS CodeCommit Pull Requests with AWS CodeBuild and AWS Lambda
GitHubのプルリクをCodeBuildでビルドする記事はよくありますが、CodeCommitのプルリクをビルドする詳細な記事はあまりなく、CodeCommitをリポジトリにCIを構築しようとする方の参考になれば幸いです。
SAMとLambdaのソースはGitHubのこちらのリポジトリに上げているので、興味がある方はそちらもご確認ください。
こちらのServerlessApplicationRepositryにアプリケーションを公開しているので、
取りえあえずデプロイして試してみたい方は、下記のAWS Chatbotの手順だけマネジメントコンソールで行い、ワークスペースIDとチャンネルIDを指定し、デプロイできます。
今回作るもの
CodeCommitのプルリクエストをトリガーに、
CodeBuildでテストを行い、結果をSlackへ通知する以下の仕組みを構築します。
- developブランチからmasterブランチへのプルリクの作成・更新
- CodeCommitの全てのイベントをAWS Chatbot経由でSlackへ通知
- CodeBuildでテストを開始
- Lambdaでプルリクに、ビルドステータスのバッジをコメント
- CodeBuildのビルド進行状況を、AWS Chatbot経由でSlackへ通知
構築にはSAM(Serverless Application Model)を使用し、LambdaはGolangで書いてみました。
前提条件
- macOS Catalina 10.15.6
- go version go1.15.2 darwin/amd64
- aws-cli/2.0.17
- SAM CLI, version 1.2.0
- Slack 4.8.0
実装
AWS Chatbot
この作業のみ、マネジメントコンソールより手動で行う必要があります。
チャットクライアントの設定
すると、Slackの画面に遷移し、ログイン済みの組織に対してAWS Chatbotへの許可を求められるので、許可します。
SlackのワークスペースIDとチャンネルIDをメモしておく
ワークスペースIDはAWS Chatbotの設定済みクライアントから確認
チャンネルIDは、コピーしたリンクの末尾の英数字です
SAM テンプレート
sam template クリックで開きます
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Setup AWS CI/CD resources
Parameters:
TargetWorkspaceId:
Type: String
TargetChannelId:
Type: String
Resources:
# ------------------------------------
# SNS Topic
# ------------------------------------
CodeCommitPullRequestForChatbotTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: codecommit-pullrequest-for-chabot-topic
# ------------------------------------
# AWS Chatbot
# ------------------------------------
ChatbotRole:
Type: "AWS::IAM::Role"
Properties:
RoleName: codecommit-pullrequest-chatbot-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service: chatbot.amazonaws.com
Action: "sts:AssumeRole"
Policies:
- PolicyName: codecommit-pullrequest-chatbot-policy
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- codecommit:Describe*
- cloudwatch:Get*
- cloudwatch:List*
Resource:
- "*"
CodeCommitPullRequestChatbot:
Type: AWS::Chatbot::SlackChannelConfiguration
Properties:
ConfigurationName: CodeCommitPullRequestSampleChatbotConfiguration
IamRoleArn: !GetAtt ChatbotRole.Arn
SlackChannelId: !Ref TargetChannelId
SlackWorkspaceId: !Ref TargetWorkspaceId
SnsTopicArns:
- !Ref CodeCommitPullRequestForChatbotTopic
# ------------------------------------
# AWS CodeCommit
# ------------------------------------
CodeCommitPullRequestSampleRepository:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryDescription: This repository contains sample code that tested in CodeBuild project.
RepositoryName: codecommit-pullrequest-sample-repository
# 「トリガー」による通知ではAWS ChatBotの通知がうまく動きませんでした。 「通知」を使用する必要があります。
# Triggers:
# - Name: MainTrigger
# DestinationArn:
# !Ref CodeCommitPullRequestForChatbotTopic
# Events:
# - all
CodeCommitPullRequestCodeStarNotifications:
Type: AWS::CodeStarNotifications::NotificationRule
Properties:
DetailType: FULL
EventTypeIds:
- codecommit-repository-comments-on-commits
- codecommit-repository-comments-on-pull-requests
- codecommit-repository-approvals-status-changed
- codecommit-repository-approvals-rule-override
- codecommit-repository-pull-request-created
- codecommit-repository-pull-request-source-updated
- codecommit-repository-pull-request-status-changed
- codecommit-repository-pull-request-merged
- codecommit-repository-branches-and-tags-created
- codecommit-repository-branches-and-tags-deleted
- codecommit-repository-branches-and-tags-updated
Name: codecommit-pullrequests-codestar-notification-rule
Resource:
!GetAtt CodeCommitPullRequestSampleRepository.Arn
Targets:
- TargetType: AWSChatbotSlack
TargetAddress:
!GetAtt CodeCommitPullRequestChatbot.Arn
# ------------------------------------
# AWS CodeBuild
# ------------------------------------
CodeBuildServiceRole:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service: codebuild.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: codecommit-pullrequest-codebuild-execute-role
PolicyDocument:
Version: 2012-10-17
Statement:
- Resource: "*"
Effect: Allow
Action:
- codecommit:*
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
- Resource: "*"
Effect: Allow
Action:
- s3:GetObject
- s3:PutObject
- s3:GetObjectVersion
CodeBuildProjectPullRequest:
Type: AWS::CodeBuild::Project
Properties:
Artifacts:
Type: NO_ARTIFACTS
BadgeEnabled: true
Description: This build project is triggered on create or update pull request in AWS CodeCommit.
Environment:
ComputeType: BUILD_GENERAL1_SMALL
Image: aws/codebuild/amazonlinux2-x86_64-standard:3.0-20.08.14
Type: LINUX_CONTAINER
Name: codecommit-pullrequests-codebuild-project
ServiceRole:
!Ref CodeBuildServiceRole
Source:
Type: CODECOMMIT
Location: !GetAtt CodeCommitPullRequestSampleRepository.CloneUrlHttp
SourceVersion: refs/heads/develop
CodeBuildCodeStarNotifications:
Type: AWS::CodeStarNotifications::NotificationRule
Properties:
DetailType: FULL
EventTypeIds:
- codebuild-project-build-state-failed
- codebuild-project-build-state-succeeded
- codebuild-project-build-state-in-progress
- codebuild-project-build-state-stopped
- codebuild-project-build-phase-failure
- codebuild-project-build-phase-success
Name: codebuild-codestar-notification-rule
Resource:
!GetAtt CodeBuildProjectPullRequest.Arn
Targets:
- TargetType: AWSChatbotSlack
TargetAddress:
!GetAtt CodeCommitPullRequestChatbot.Arn
# ------------------------------------
# Lambda
# ------------------------------------
LambdaManagedPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
Description: "Lambda policy that allows post comments in pull requests on CodeCommit."
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "codebuild:*"
- "codecommit:*"
- "logs:CreateLogGroup"
- "logs:CreateLogStream"
- "logs:PutLogEvents"
- "logs:GetLogEvents"
- "xray:*"
Resource: "*"
LambdaRole:
Type: "AWS::IAM::Role"
Properties:
Description: "Lambda role that allows post comments in pull requests on CodeCommit."
RoleName: codecommit-pullrequest-lambda-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "lambda.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- !Ref "LambdaManagedPolicy"
CommentBuildStatusToPullRequest:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/comment-build-status-to-pull-request/
Handler: main
Runtime: go1.x
FunctionName: "comment-build-status-to-pull-request"
Description: "Comment build badge url to pullrequest."
Tracing: Active
Environment:
Variables:
CODEBUILD_ARN:
!GetAtt CodeBuildProjectPullRequest.Arn
Role:
!GetAtt LambdaRole.Arn
Events:
OnPullRequestEvent:
Type: EventBridgeRule
Properties:
Pattern:
source:
- "aws.codecommit"
detail-type:
- "CodeCommit Pull Request State Change"
detail:
event:
- "pullRequestCreated"
- "pullRequestSourceBranchUpdated"
pullRequestStatus:
- "Open"
sourceReference:
- "refs/heads/develop"
destinationReference:
- "refs/heads/master"
resources:
- !GetAtt CodeCommitPullRequestSampleRepository.Arn
# ------------------------------------
# CloudWatch Event Rule
# ------------------------------------
OnPullRequestEventRuleManagedPolicy:
Type: "AWS::IAM::ManagedPolicy"
Properties:
Description: "CloudWatch Event policy that allows invoking lambda functions and starting CodeBuild projects."
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Action:
- "codebuild:*"
- "lambda:*"
Resource: "*"
OnPullRequestEventRuleRole:
Type: "AWS::IAM::Role"
Properties:
Description: "CloudWatch Event role that allows invoking lambda functions and starting CodeBuild projects."
RoleName: codecommit-pullrequest-cloudwatch-event-role
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- "events.amazonaws.com"
Action:
- "sts:AssumeRole"
ManagedPolicyArns:
- !Ref OnPullRequestEventRuleManagedPolicy
OnPullRequestEventRule:
Type: AWS::Events::Rule
Properties:
Description: "Trigger CodeBuild project and lambda function on PR's in CodeCommit."
EventPattern:
source:
- "aws.codecommit"
detail-type:
- "CodeCommit Pull Request State Change"
detail:
event:
- "pullRequestCreated"
- "pullRequestSourceBranchUpdated"
pullRequestStatus:
- "Open"
sourceReference:
- "refs/heads/develop"
destinationReference:
- "refs/heads/master"
resources:
- !GetAtt CodeCommitPullRequestSampleRepository.Arn
State: "ENABLED"
Targets:
-
Arn: !GetAtt CodeBuildProjectPullRequest.Arn
Id: CodeBuildProject
RoleArn: !GetAtt OnPullRequestEventRuleRole.Arn
いくつか注意点を挙げるとすると、
ワークスペースIDとチャンネルIDはデプロイ時にパラメータの上書きで指定します。
Parameters:
TargetWorkspaceId:
Type: String
TargetChannelId:
Type: String
CodeCommitPullRequestChatbot:
Type: AWS::Chatbot::SlackChannelConfiguration
Properties:
ConfigurationName: CodeCommitPullRequestSampleChatbotConfiguration
IamRoleArn: !GetAtt ChatbotRole.Arn
SlackChannelId: !Ref TargetChannelId
SlackWorkspaceId: !Ref TargetWorkspaceId
SnsTopicArns:
- !Ref CodeCommitPullRequestForChatbotTopic
CodeCommit→AWS ChatBotの連携は、「トリガー」ではなく、「通知」を用いています。
「トリガー」からもターゲットにAWS Chatbotを指定することができますが、そちらではうまく動きませんでした。
# ------------------------------------
# AWS CodeCommit
# ------------------------------------
CodeCommitPullRequestSampleRepository:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryDescription: This repository contains sample code that tested in CodeBuild project.
RepositoryName: codecommit-pullrequest-sample-repository
# 「トリガー」による通知ではAWS ChatBotの通知がうまく動きませんでした。 「通知」を使用する必要があります。
# Triggers:
# - Name: MainTrigger
# DestinationArn:
# !Ref CodeCommitPullRequestForChatbotTopic
# Events:
# - all
Lambda
Lambda ソース クリックで開きます
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/codebuild"
"github.com/aws/aws-sdk-go/service/codecommit"
)
type EventDetail struct {
SourceReference string `json:"sourceReference"`
LastModifiedDate string `json:"lastModifiedDate"`
Author string `json:"author"`
PullRequestStatus string `json:"pullRequestStatus"`
IsMerged string `json:"isMerged"`
NotificationBody string `json:"notificationBody"`
DestinationReference string `json:"destinationReference"`
PullRequestId string `json:"pullRequestId"`
CallerUserArn string `json:"callerUserArn"`
Title string `json:"title"`
CreationDate string `json:"creationDate"`
RepositoryNames []string `json:"repositoryNames"`
DestinationCommit string `json:"destinationCommit"`
Event string `json:"event"`
SourceCommit string `json:"sourceCommit"`
}
const region = "ap-northeast-1"
func HandleRequest(ctx context.Context, event events.CloudWatchEvent) (string, error) {
mySession := session.Must(session.NewSession())
// Get PR informations from CloudWatchEvents
var eventDetail EventDetail
err := json.Unmarshal(event.Detail, &eventDetail)
if err != nil {
print(err.Error())
return "", err
}
pullRequestId := eventDetail.PullRequestId
repositoryName := eventDetail.RepositoryNames[0]
afterCommitId := eventDetail.DestinationCommit
beforeCommitId := eventDetail.SourceCommit
log.Printf("Target repository name is %s\n", repositoryName)
log.Printf("PullRequests title is %s\n", eventDetail.Title)
// Get CodeBuild badge url.
codebuildSvc := codebuild.New(mySession, aws.NewConfig().WithRegion(region))
codeBuildArn := os.Getenv("CODEBUILD_ARN")
names := []*string{&codeBuildArn}
batchGetProjectsOutput, err := codebuildSvc.BatchGetProjects(&codebuild.BatchGetProjectsInput{
Names: names,
})
if err != nil {
print(err.Error())
return "", err
}
badgeUrl := batchGetProjectsOutput.Projects[0].Badge.BadgeRequestUrl
// Set content to post to PR's comment
commentTemplate := `Unit tests have been started in CodeBuild.
Build Status:
![BuildBadge](%s)
`
//Branch in badge URL is master by default, so, replace to 'develop branch'
content := fmt.Sprintf(commentTemplate, strings.Replace(*badgeUrl, "master", "develop", -1))
// Post comment for PR
codecommitSvc := codecommit.New(mySession, aws.NewConfig().WithRegion(region))
output, err := codecommitSvc.PostCommentForPullRequest(
&codecommit.PostCommentForPullRequestInput{
RepositoryName: &repositoryName,
AfterCommitId: &afterCommitId,
BeforeCommitId: &beforeCommitId,
Content: &content,
PullRequestId: &pullRequestId,
})
if err != nil {
print(err.Error())
return "", err
}
log.Printf("Result for post comment for PullRequests: %s\n", output)
return output.GoString(), nil
}
func main() {
lambda.Start(HandleRequest)
}
EventBridgeから、コメントを投稿するプルリクの情報を取得、
CodeBuildのARNを環境変数から受け取り、LambdaのなかでBadgeURLを取得し、
プルリクにコメントを投稿します。
ディレクトリ構成
samテンプレートとLambdaのソースを以下のような構成で格納しています
% tree
.
├── Makefile
├── src
│ └── comment-build-status-to-pull-request
│ ├── go.mod
│ ├── go.sum
│ ├── main.go
│ └── tests
└── template.yml
Makefile
lambdaのデプロイにgoのbuildも必要なため、下記のようなMakefileを用意しました。
all: gobuild sambuild
sambuild:
sam build
gobuild:
cd ./src/comment-build-status-to-pull-request && \
GOOS=linux go build main.go
デプロイ
ビルド実行
makeコマンドでgoの実行ファイルとsamのビルドを行います
% make
cd ./src/comment-build-status-to-pull-request && \
GOOS=linux go build main.go
sam build
Building function 'CommentBuildStatusToPullRequest'
Running GoModulesBuilder:Build
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Deploy: sam deploy --guided
デプロイ実行
--guided オプションで、ガイドに従い設定値を入力しデプロイします。
% sam deploy --guided
Configuring SAM deploy
======================
Looking for samconfig.toml : Not found
Setting default arguments for 'sam deploy'
=========================================
Stack Name [sam-app]: sam-app-codecommit-pull-request
AWS Region [us-east-1]: ap-northeast-1
Parameter TargetWorkspaceId []: 前項で確認したワークスペースIDを指定
Parameter TargetChannelId []: 前項で確認したチャンネルIDを指定
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
Confirm changes before deploy [y/N]: y
#SAM needs permission to be able to create roles to connect to the resources in your template
Allow SAM CLI IAM role creation [Y/n]: Y
Save arguments to samconfig.toml [Y/n]: Y
Looking for resources needed for deployment: Found!
Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-xxxx
A different default S3 bucket can be set in samconfig.toml
Saved arguments to config file
Running 'sam deploy' for future deployments will use the parameters saved above.
The above parameters can be changed by modifying samconfig.toml
Learn more about samconfig.toml syntax at
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
Uploading to sam-app-codecommit-pull-request/d5209e02222bf6a54a6a3ba6e56e58ae 7290759 / 7290759.0 (100.00%)
Deploying with following values
===============================
Stack name : sam-app-codecommit-pull-request
Region : ap-northeast-1
Confirm changeset : True
Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-xxxx
Capabilities : ["CAPABILITY_IAM"]
Parameter overrides : {'TargetWorkspaceId': '前項で確認したワークスペースIDを指定', 'TargetChannelId': '前項で確認したチャンネルIDを指定'}
Initiating deployment
=====================
Uploading to sam-app-codecommit-pull-request/fa83cb8561abe30cc609828a3ae3c87b.template 9174 / 9174.0 (100.00%)
Waiting for changeset to be created..
Error: Failed to create changeset for the stack: sam-app-codecommit-pull-request, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state Status: FAILED. Reason: Requires capabilities : [CAPABILITY_NAMED_IAM]
名前付きIAMRoleを作成しているため、CapabilitiesはCAPABILITY_IAM
なので、エラーが発生しました。
CapabilitiesをCAPABILITY_NAMED_IAM
上書きして再デプロイします。
Capabilitiesの設定については下記の公式ドキュメントに記載があります。
AWS Identity and Access Management によるアクセスの制御
% sam deploy --capabilities CAPABILITY_NAMED_IAM
〜中略〜
Successfully created/updated stack - sam-app-codecommit-pull-request in ap-northeast-1
無事デプロイに成功しました!
動作確認
CodeCommitへサンプルコードの格納
先ほど作成したcodecommitの空のリポジトリをクローンします
% git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-pullrequest-sample-repository
Cloning into 'codecommit-pullrequest-sample-repository'...
warning: You appear to have cloned an empty repository.
index.htmlとbuildspec.ymlを作成し、コミットします。
今回はサンプルなので、
buildspec.ymlでは、index.htmlの中にsucceeded
の文字列があるかどうかだけをテストしています。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Codecommit PullRequests Test</title>
</head>
<body>
<div align="center">
<h1>sample build has been succeeded!!</h1>
</div>
</body>
</html>
version: 0.2
phases:
install:
runtime-versions:
nodejs: 10
commands:
- echo "installing something"
pre_build:
commands:
- echo "we are in the pre build phase"
build:
commands:
- echo "we are in the build block"
- echo "we will run some tests"
- grep -Fq "succeeded" index.html
post_build:
commands:
- echo "we are in the post build phase"
ファイルを作成したら、CodeCommitにPushします
% git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
buildspec.yml
index.html
nothing added to com
% git add -A
% git commit -m"Add: index.html and buildspec.yml"
[master (root-commit) 552df43] Add: index.html and buildspec.yml
2 files changed, 31 insertions(+)
create mode 100644 buildspec.yml
create mode 100644 index.html
% git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 615 bytes | 615.00 KiB/s, done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-pullrequest-sample-repository
* [new branch] master -> master
Developブランチ → Masterブランチのプルリクエスト作成
developブランチを作成します
% git checkout -b develop
Switched to a new branch 'develop'
index.htmlを下記のように修正します
<h1>sample build has been succeeded!!</h1>
↓
<h1>sample build has been succeeded!! v2</h1>
修正内容をコミットしリモートリポジトリへプッシュします
% git status
On branch develop
Your branch is up to date with 'origin/develop'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: index.html
% git add -A
% git commit -m"fix: index.html v1 to v2"
[develop 8d1a621] fix: index.html v1 to v2
1 file changed, 1 insertion(+), 1 deletion(-)
% git push -u origin develop
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-pullrequest-sample-repository
* [new branch] develop -> develop
Branch 'develop' set up to track remote branch 'develop' from 'origin'.
develop → masterのプルリクエストをaws-cliで作成します
% aws codecommit create-pull-request \
--title "Test pull request" \
--description "pull request from develop to master branch." \
--targets repositoryName=codecommit-pullrequest-sample-repository,sourceReference=develop,destinationReference=master \
--output table
----------------------------------------------------------------------------------------------
| CreatePullRequest |
+--------------------------------------------------------------------------------------------+
|| pullRequest ||
|+--------------------+---------------------------------------------------------------------+|
|| authorArn | arn:aws:iam::123456789012:user/joe-king-sh ||
|| clientRequestToken| 47cbaf00-bd2d-4805-a344-xxxxxxxxxxxx ||
|| creationDate | 2020-10-03T16:42:54.023000+00:00 ||
|| description | pull request from develop to master branch. ||
|| lastActivityDate | 2020-10-03T16:42:54.023000+00:00 ||
|| pullRequestId | 8 ||
|| pullRequestStatus | OPEN ||
|| revisionId | 12e9f4e685a56132264f0f50cda6d4bafa9055b04c02b697bb76xxxxxxxxxxxx ||
|| title | Test pull request ||
|+--------------------+---------------------------------------------------------------------+|
||| pullRequestTargets |||
||+------------------------------+---------------------------------------------------------+||
||| destinationCommit | 552df43e0634ee022df8a8a0aeb4xxxxxxxxxxxx |||
||| destinationReference | refs/heads/master |||
||| mergeBase | 552df43e0634ee022df8a8a0aeb4xxxxxxxxxxxx |||
||| repositoryName | codecommit-pullrequest-sample-repository |||
||| sourceCommit | 8d1a621ee5d921a8ad63a03dfe90xxxxxxxxxxxx |||
||| sourceReference | refs/heads/develop |||
||+------------------------------+---------------------------------------------------------+||
|||| mergeMetadata ||||
|||+------------------------------------------------+-------------------------------------+|||
|||| isMerged | False ||||
|||+------------------------------------------------+-------------------------------------+|||
プルリクエストへのコメントの確認
プルリクエストが作成されると、Lambdaでテスト開始の旨と、CodeBuildのバッジURLがコメントされます。
ビルドが正常に完了するとバッジのステータスがpassing
変わります🎉
試しにdevelopブランチのindex.htmlを下記のように変更しプッシュすると、
<h1>sample build has been succeeded!! v2</h1>
↓
<h1>sample build has been failed!! v3</h1>
プルリクエストが更新され、再度ビルドが走りますが、
テストに失敗するため、ステータスがfailing
に変わります
Slack通知の確認
CodeCommitの通知
プルリクエストの登録時、更新時共に、通知が届いています!🔔
CodeBuildの通知
後片付け
要らなくなったら、デプロイしたCloudformationスタックを削除します。
% aws cloudformation delete-stack --stack-name sam-app-codecommit-pull-request
おわりに
元ネタのブログでは、
ビルド結果を別のLambdaで書き込んでおり、そのLambdaへコメント投稿先のプルリクエストの情報を渡すため?に、
CodeBuildのトリガーにCodePipelineを使用していましたが、
CodeBuildのバッチURLを使用することで、Lambdaは1本で、CodePipelineも必要のない今回の構成になりました。
ただしBadgeURLは指定したブランチの最後のビルド結果しか残さないので、プルリクエスト更新時に、全てのバッジが変わる問題は微妙だなと感じつつも、一旦ここをゴールにしたいと思います。
AWS Chatbotはこの春GAになり、使ってみたいと思っていましたが、
SNSや開発者ツールの通知設定から直接Slackチャンネルを指定できて、とても便利でした!
通知のためだけのLambdaの実装などが不要になるので、面倒で入れていなかったプロジェクトにもすぐに導入したいと思いました。