\u30c8\u30ea\u30ac\u30fc
\n\u30c6\u30b9\u30c8\u5b9f\u884c\u306e\u969b\u306b\u5229\u7528\u3059\u308b\u3082\u306e\u3068\u540c\u3058\u3067\u69cb\u6210\u3068\u3057\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001main \u30d6\u30e9\u30f3\u30c1\u3078\u306e PR \u304c\u4f5c\u6210\u3055\u308c\u305f\u6642\u306b\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306b\u8a2d\u5b9a\u3057\u3066\u3044\u307e\u3059\u3002
\n\u203b${}\u306e\u90e8\u5206\u306f\u81ea\u8eab\u306e\u74b0\u5883\u306b\u5408\u308f\u305b\u3066\u5909\u66f4\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/li>\n<\/ol>\n\u30b5\u30f3\u30d7\u30eb<\/p>\n
{\n "detail-type": ["CodeCommit Pull Request State Change"],\n "resources": ["arn:aws:codecommit:ap-northeast-1:${accountID}:${repositoryName}"],\n "source": ["aws.codecommit"],\n "detail": {\n "sourceReference": [{\n "anything-but": "refs\/heads\/main"\n }],\n "pullRequestStatus": ["Open"],\n "destinationReference": "refs\/heads\/main",\n "event": ["pullRequestCreated", "pullRequestSourceBranchUpdated"]\n }\n}<\/code><\/pre>\n\n- \u30bf\u30fc\u30b2\u30c3\u30c8
\n\u6b21\u306b\u5b9f\u884c\u3059\u308b Step Functions \u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\nStep Functions<\/h3>\n
Step Functions \u3092\u9593\u306b\u631f\u3093\u3067\u3044\u308b\u306e\u306f\u3001\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u3067\u4f5c\u6210\u3059\u308b Lambda \u3092\u9045\u5ef6\u5b9f\u884c\u3059\u308b\u305f\u3081\u3067\u3059\u3002Lambda\u306e\u30b3\u30fc\u30c9\u5185\u3067CodeBuild\u306e\u30d3\u30eb\u30c9\u8a73\u7d30\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u5148\u306bCodeBuild\u306e\u51e6\u7406\u304c\u59cb\u307e\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u540c\u4e00\u6761\u4ef6\u306eEventBridge\u3092\u5229\u7528\u3057\u3066\u3044\u308b\u305f\u3081\u9806\u5e8f\u304c\u60f3\u5b9a\u901a\u308a\u306b\u306a\u3089\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002
\n\u305d\u3053\u3067\u3001Step Functions\u306e\u5f85\u6a5f\u51e6\u7406\u3092\u884c\u3046\u3053\u3068\u306b\u3088\u308a\u3001Lambda\u5b9f\u884c\u3088\u308a\u5148\u306b\u78ba\u5b9f\u306bCodeBuild\u306b\u3088\u308b\u30c6\u30b9\u30c8\u304c\u5148\u306b\u59cb\u307e\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002\u5b9a\u7fa9\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u30013\u79d2\u9593\u306e\u5f85\u6a5f\u51e6\u7406\u306e\u5f8c\u306bLambda\u5b9f\u884c\u3092\u958b\u59cb\u3059\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n
\n- \u5b9a\u7fa9<\/li>\n<\/ol>\n
\u30b5\u30f3\u30d7\u30eb<\/p>\n
{\n "StartAt": "WaitSecJob",\n "States": {\n "WaitSecJob": {\n "Type": "Wait",\n "Seconds": 3,\n "Next": "LambdaJob"\n },\n "LambdaJob": {\n "End": true,\n "Retry": [\n {\n "ErrorEquals": [\n "Lambda.ServiceException",\n "Lambda.AWSLambdaException",\n "Lambda.SdkClientException"\n ],\n "IntervalSeconds": 2,\n "MaxAttempts": 6,\n "BackoffRate": 2\n }\n ],\n "Type": "Task",\n "Resource": "arn:aws:states:::lambda:invoke",\n "Parameters": {\n "FunctionName": "arn:aws:lambda:ap-northeast-1:${accountID}:function:${functionName}",\n "Payload.$": "$"\n }\n }\n },\n "TimeoutSeconds": 300\n}<\/code><\/pre>\nLambda<\/h3>\n
\u9806\u756a\u306b\u4ee5\u4e0b\u306e\u51e6\u7406\u3092\u5b9f\u884c\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n
\n- \u53d7\u3051\u53d6\u3063\u305f event \u304b\u3089\u5fc5\u8981\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u53d6\u5f97<\/li>\n
- \u30c6\u30b9\u30c8\u5b9f\u884c\u306b\u5229\u7528\u3057\u3066\u3044\u308b CodeBuild \u304b\u3089\u30d3\u30eb\u30c9\u30d0\u30c3\u30c1\u306e\u60c5\u5831\u3068\u30d3\u30eb\u30c9\u8a73\u7d30\u3092\u53d6\u5f97<\/li>\n
- \u30d3\u30eb\u30c9\u8a73\u7d30\u304b\u3089\u30d3\u30eb\u30c9\u30ed\u30b0 URL \u3092\u7d44\u307f\u7acb\u3066\u308b<\/li>\n
- \u30d3\u30eb\u30c9\u30d0\u30c3\u30c1\u3068\u30d3\u30eb\u30c9\u30ed\u30b0 URL \u3092 CodeCommit \u306e\u30a2\u30af\u30c6\u30a3\u30d3\u30c6\u30a3\u306b\u53cd\u6620<\/li>\n<\/ol>\n
\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9(python)<\/p>\n
import os\nimport boto3\n\ndef codebuild_project_info(client):\n'''\ncodebuild \u306e\u30d0\u30c3\u30b8 URL \u3068\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u5374\nbadge_url -> str \u578b\u306e\u30d0\u30c3\u30b8 URL\nproject_name -> str \u578b\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u540d\n'''\ncodebuild_arn = os.environ.get('CODEBUILD_ARN')\nbatch_get_response = client.batch_get_projects(\nnames=[\ncodebuild_arn,\n]\n)\nbadge_url = batch_get_response['projects'][0]['badge']['badgerequesturl']\nproject_name = batch_get_response['projects'][0]['name']\nreturn badge_url, project_name\n\ndef codebuild_build_details_arr(client, project_name):\n'''\n\u6700\u65b0\u306e\u30d3\u30eb\u30c9\u5c65\u6b74 10 \u4ef6\u306e\u8a73\u7d30\u60c5\u5831\u3092\u53d6\u5f97\u3057\u3066\u8fd4\u5374\nbuild_details -> dict \u578b\u306e\u30d3\u30eb\u30c9\u60c5\u5831\u8a73\u7d30\n''' # \u6700\u65b0\u306e\u30d3\u30eb\u30c9\u5c65\u6b74\u306e ID \u3092\u53d6\u5f97\nlist_build_response = client.list_builds_for_project(\nprojectName=project_name,\nsortOrder='DESCENDING' # \u6700\u65b0\u306e\u3082\u306e\u304b\u3089\u53d6\u5f97\n)\nbuild_ids = list_build_response['ids'][:10]\n\n # \u30bd\u30fc\u30b9\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u60c5\u5831\u3092\u542b\u3081\u305f\u8a73\u7d30\u60c5\u5831\u3092\u53d6\u5f97\n build_details = client.batch_get_builds(\n ids=build_ids\n )\n return build_details\n\ndef extract_build_id(builds_details, source_branch):\n'''\n\u30d7\u30eb\u30ea\u30af\u306e\u30bd\u30fc\u30b9\u30d6\u30e9\u30f3\u30c1\u3068\u30d3\u30eb\u30c9 ID \u306e\u30bd\u30fc\u30b9\u30d6\u30e9\u30f3\u30c1\u3092\u7a81\u5408\u3002\n\u8907\u6570\u30bd\u30fc\u30b9\u304b\u3089\u30d7\u30eb\u30ea\u30af\u5b9f\u884c\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u91cd\u306a\u3089\u306a\u3051\u308c\u3070\u57fa\u672c\u7684\u306b\u306f 1 \u56de\u76ee\u306e\u30eb\u30fc\u30d7\u3067\u4e00\u81f4\u3059\u308b\u3002\ndetail['id'] -> str \u578b\u306e\u30d3\u30eb\u30c9 ID\n'''\nfor detail in builds_details:\nsource_verison = detail['sourceVersion']\nif source_verison == source_branch:\nreturn detail['id']\n\ndef lambda_handler(event, context): # \u5fc5\u8981\u306a\u60c5\u5831\u3092 eventBridge \u304b\u3089\u9023\u643a\u3055\u308c\u308b event \u3088\u308a\u53d6\u5f97\naccount = event['account']\nregion = event['region']\npr_info = event['detail']\nsource_reference = pr_info['sourceReference'] # e.g.refs\/heads\/feature\/addXX\nsource_branch = '\/'.join(source_reference.split('\/')[2:]) # refs\/heads \u5f8c\u306e\u30d6\u30e9\u30f3\u30c1\u540d\npull_request_id = pr_info['pullRequestId']\nrepository_name = pr_info['repositoryNames'][0]\nafter_commit_id = pr_info['destinationCommit']\nbefore_commit_id = pr_info['sourceCommit']\n\n print(f"source repository is {repository_name}.source branch is {source_branch}")\n\n # codebuild\u95a2\u9023\u306e\u51e6\u7406\n codebuild_client = boto3.client('codebuild')\n badge_url, project_name = codebuild_project_info(codebuild_client)\n build_details = codebuild_build_details_arr(codebuild_client, project_name)\n build_id = extract_build_id(build_details['builds'], source_branch)\n\n build_url = f"https:\/\/{region}.console.aws.amazon.com\/codesuite\/codebuild\/{account}\/projects\/{project_name}\/build\/{build_id}"\n comment_template = f"[\u30d3\u30eb\u30c9\u8a73\u7d30]({build_url}) Build Status:![BuildBadge]({badge_url})"\n # batchurl\u306f\u901a\u5e38\u30c7\u30d5\u30a9\u30eb\u30c8\u30d6\u30e9\u30f3\u30c1(main)\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6301\u3063\u3066\u3044\u308b branch=main\n # \u3053\u306e\u90e8\u5206\u3092\u7f6e\u63db\u3059\u308b\u3053\u3068\u3067\u30bd\u30fc\u30b9\u30d6\u30e9\u30f3\u30c1\u306e\u30d3\u30eb\u30c9\u72b6\u6cc1\u3092\u78ba\u8a8d\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\n content = comment_template.replace('main', source_branch, 1)\n\n # codecommit\u3078\u306e\u30b3\u30e1\u30f3\u30c8\u306e\u53cd\u6620\n codecommit_clinet = boto3.client('codecommit')\n _ = codecommit_clinet.post_comment_for_pull_request(\n pullRequestId=pull_request_id,\n repositoryName=repository_name,\n beforeCommitId=before_commit_id,\n afterCommitId=after_commit_id,\n content=content\n )\n<\/code><\/pre>\ncodebuild_arn = os.environ.get('CODEBUILD_ARN')<\/code> \u3067\u306f\u3001\u30c6\u30b9\u30c8\u3092\u5b9f\u884c\u3057\u3066\u3044\u308bCodeBuild\u306earn\u3092\u74b0\u5883\u5909\u6570\u304b\u3089\u53d6\u5f97\u3057\u3066\u3044\u308b\u305f\u3081\u3001\u4e8b\u524d\u306b\u30b3\u30f3\u30bd\u30fc\u30eb\u306a\u3069\u304b\u3089\u8a2d\u5b9a\u3059\u308b\u304barn\u3092\u76f4\u63a5\u57cb\u3081\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n\u7d42\u308f\u308a\u306b<\/h2>\n
\u3053\u308c\u3089\u306eAWS\u30ea\u30bd\u30fc\u30b9\u3092\u4f5c\u6210\u30fb\u30c7\u30d7\u30ed\u30a4\u3059\u308b\u3068PR\u3092\u30c8\u30ea\u30ac\u30fc\u3068\u3057\u3066\u5b8c\u6210\u5f62\u306e\u30a4\u30e1\u30fc\u30b8<\/a>\u3067\u5b9f\u73fe\u3057\u305f\u304b\u3063\u305f\u3053\u3068\u304c\u53ef\u80fd\u306b\u306a\u308a\u3001\u7c21\u5358\u306b\u30d3\u30eb\u30c9\u8a73\u7d30\u304a\u3088\u3073\u30d3\u30eb\u30c9\u30b9\u30c6\u30fc\u30bf\u30b9\u304c\u78ba\u8a8d\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u3060\u3044\u3076\u4fbf\u5229\u306b\u306a\u308a\u307e\u3057\u305f\uff01
\n\u305f\u3060\u3001GitHub\u3068Github Actions\u3092\u5229\u7528\u3059\u308b\u3068\u30ea\u30bd\u30fc\u30b9\u3092\u8ffd\u52a0\u3057\u305f\u308a\u3001\u30b3\u30fc\u30c9\u3092\u66f8\u3044\u305f\u308a\u3059\u308b\u307e\u3067\u3082\u306a\u304f\u3053\u308c\u3089\u306e\u6a5f\u80fd\u304c\u6e96\u5099\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u3001CodeCommit\u3082\u66f4\u306b\u4fbf\u5229\u306b\u306a\u3063\u3066\u6b32\u3057\u3044\u306a\u3068\u601d\u3044\u307e\u3059\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"\u6c17\u3065\u3051\u3070 DWS \u306b\u5165\u793e\u3057\u3066 4 \u30f6\u6708\u304c\u7d4c\u904e\u3057\u3066\u3044\u307e\u3057\u305f\u3002\u30c0\u30a4\u3067\u3059\uff01 \u4eca\u56de\u306f\u3001\u5c11\u3005\u7279\u6b8a\u306a\u8981\u4ef6\u3067\u3059\u304c \u3001\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8(\u4ee5\u4e0b\u300cPR\u300d)\u3092\u30c8\u30ea\u30ac\u30fc\u3068\u3057\u3066CodeBuild\u3067\u5b9f\u884c\u3055\u308c\u308b\u30c6\u30b9\u30c8\u306e\u7d50\u679c\u3092 CodeCommit \u306e\u30a2 […]<\/p>\n","protected":false},"author":2,"featured_media":4380,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jinr_url_youtube":"","_jinr_pip_youtube":false,"_jinr_time_youtube":"","_jinr_thumb_youtube":"","_jinr_media_youtube":"","_jinr_category_edit":false,"_jinr_category":"","_jinr_title_display":false,"_jinr_snsbutton_display":false,"_jinr_ads_display":false,"_jinr_thumbnail_display":false,"_jinr_profile_display":false,"_jinr_relatedpost_display":false,"_jinr_sidebar1col_display":false,"_jinr_sidebar2col_display":false,"_jinr_seotitle_display":"","_jinr_description_display":"","_jinr_keyword_display":"","_jinr_hastag_display":"","_jinr_canonical_display":"","_jinr_noindex_display":false,"_jinr_paidpost":false,"_jinr_paidpost_product_id":"","_jinr_headtag_article":""},"categories":[3],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/4349"}],"collection":[{"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=4349"}],"version-history":[{"count":0,"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/4349\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=4349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=4349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cms-mordpress.test.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=4349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}