gpt4 book ai didi

amazon-web-services - AWS 事件桥 : Get branch name and last commit author for AWS CodeBuild event rule

转载 作者:行者123 更新时间:2023-12-05 04:24:00 24 4
gpt4 key购买 nike

我们正在使用 CodeBuild 并实现了我们自己的电子邮件构建通知系统(如 seems to be required currently)。

我们使用同一个 CodeBuild 项目为同一个 Bitbucket 存储库构建我们的所有分支,并且我们使用一个 EventBridge 规则,该规则在该 CodeBuild 项目失败时触发。然后调用 Lambda 函数通过 SNS 主题发出通知。

我们可以从 EventBridge 中获取传递给 Lambda 的事件中的一些基本信息,例如构建号、项目和状态。但是我们需要包括触发构建的分支,因为当这些电子邮件到达时会造成混淆,并且不清楚错误发生在哪个分支上。此外,让提交的作者触发构建将有助于仅通过电子邮件发送给他们,而不是我们目前正在做的更一般的通知。

不幸的是,分支机构和作者似乎都没有参与其中。有没有办法包含或获取它们?

我们的 EventBridge 事件模式是

{
"detail": {
"build-status": ["FAILED"],
"project-name": ["api"]
},
"detail-type": ["CodeBuild Build State Change"],
"source": ["aws.codebuild"]
}

我们的 Lambda 是

const AWS = require("aws-sdk");

const snsTopicArn = process.env.TOPIC_ARN;

exports.handler = (event, context, callback) => {
const status = event.detail["build-status"];
const project = event.detail["project-name"];
const buildLabel = event.detail["additional-information"]["build-number"];

const account = event.account;
const region = event.region;

// determine the link to the build
const buildFullId = event.detail["build-id"];
const buildIdParts = buildFullId.split(":");
const buildId = buildIdParts[buildIdParts.length - 1];
const buildLink = `https://${region}.console.aws.amazon.com/codesuite/codebuild/${account}/projects/${project}/build/${project}%3A${buildId}/?region=${region}`;

const subject = `${project} build ${buildLabel} ${status}`;

const message = `${subject}\n\n${buildLink}`;

const sns = new AWS.SNS();
sns.publish(
{
TopicArn: snsTopicArn,
Message: message,
Subject: subject,
},
function (error, data) {
if (error) console.log(error, error.stack);
callback(error, data);
}
);
};

我们正在将这样的事件传递给我们的 Lambda:

{
"version": "0",
"id": "...",
"detail-type": "CodeBuild Build State Change",
"source": "aws.codebuild",
"account": "...",
"time": "2022-08-24T09:01:14Z",
"region": "eu-west-2",
"resources": [
"arn:aws:codebuild:eu-west-2:...:build/demo-release-deployment:..."
],
"detail": {
"build-status": "FAILED",
"project-name": "demo-release-deployment",
"build-id": "arn:aws:codebuild:eu-west-2:...:build/demo-release-deployment:...",
"additional-information": {
"cache": {
"type": "NO_CACHE"
},
"build-number": 18,
"timeout-in-minutes": 10,
"build-complete": true,
"initiator": "...",
"build-start-time": "Aug 24, 2022 8:57:39 AM",
"source": {
"report-build-status": true,
"buildspec": "",
"location": "...@bitbucket.org/.../build-test.git>https://...@bitbucket.org/.../build-test.git",
"git-clone-depth": 1,
"type": "BITBUCKET",
"git-submodules-config": {
"fetch-submodules": true
}
},
"artifact": {
"location": ""
},
"environment": {
"image": "aws/codebuild/amazonlinux2-x86_64-standard:4.0",
"privileged-mode": false,
"image-pull-credentials-type": "CODEBUILD",
"compute-type": "BUILD_GENERAL1_SMALL",
"type": "LINUX_CONTAINER",
"environment-variables": []
},
"logs": {
"group-name": "demo-deployment",
"stream-name": "demo-deployment/...",
"deep-link": "https://console.aws.amazon.com/cloudwatch/home?region=eu-west-2#logEvent:group=demo-deployment;stream=demo-deployment/..."
},
"phases": [
{
"phase-context": [],
"start-time": "Aug 24, 2022 8:57:39 AM",
"end-time": "Aug 24, 2022 8:57:39 AM",
"duration-in-seconds": 0,
"phase-type": "SUBMITTED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [],
"start-time": "Aug 24, 2022 8:57:39 AM",
"end-time": "Aug 24, 2022 8:58:51 AM",
"duration-in-seconds": 72,
"phase-type": "QUEUED",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 8:58:51 AM",
"end-time": "Aug 24, 2022 9:00:59 AM",
"duration-in-seconds": 127,
"phase-type": "PROVISIONING",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:00:59 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 6,
"phase-type": "DOWNLOAD_SOURCE",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 0,
"phase-type": "INSTALL",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:06 AM",
"duration-in-seconds": 0,
"phase-type": "PRE_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [
"COMMAND_EXECUTION_ERROR: Error while executing command: npm publish. Reason: exit status 1"
],
"start-time": "Aug 24, 2022 9:01:06 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 4,
"phase-type": "BUILD",
"phase-status": "FAILED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 0,
"phase-type": "POST_BUILD",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:11 AM",
"duration-in-seconds": 0,
"phase-type": "UPLOAD_ARTIFACTS",
"phase-status": "SUCCEEDED"
},
{
"phase-context": [": "],
"start-time": "Aug 24, 2022 9:01:11 AM",
"end-time": "Aug 24, 2022 9:01:13 AM",
"duration-in-seconds": 2,
"phase-type": "FINALIZING",
"phase-status": "SUCCEEDED"
},
{
"start-time": "Aug 24, 2022 9:01:13 AM",
"phase-type": "COMPLETED"
}
],
"queued-timeout-in-minutes": 480
},
"current-phase": "COMPLETED",
"current-phase-context": "[: ]",
"version": "1"
}
}

最佳答案

我有同样的任务。我试图找到任何解决方案。我尝试执行以下脚本:https://github.com/thii/aws-codebuild-extras .但是将环境变量移动到 lambda 会出现问题。

所以最后我使用以下步骤解决了它:

  1. 我在 buildSpec.yml 文件中打印作者电子邮件:
  pre_build:
commands:
- AUTHOR_EMAIL="$(git log -1 --pretty=%ae)"
- echo "CODEBUILD_GIT_AUTHOR_EMAIL=$AUTHOR_EMAIL"
  1. 我在我的 lambda 处理程序中从 CloudWatch 获取所有日志,并按模式查找字符串 ^CODEBUILD_GIT_AUTHOR_EMAIL=(.*)$
  2. 我在我的 lambda 处理程序中使用 AWS SES 将消息发送到此电子邮件

非常糟糕的解决方案,但它有效。我不能花更多的时间来完成这项任务

关于amazon-web-services - AWS 事件桥 : Get branch name and last commit author for AWS CodeBuild event rule,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73582610/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com