在Amazon的CodeDeploy上获得成功/失败构建的通知

时间:2015-10-05 18:56:24

标签: amazon-web-services aws-code-deploy

我想构建一个工具,每当通过任何通信媒介(电子邮件,冗余等)在CodeDeploy上成功或失败构建时,都会通知用户。我已经完成了他们的文件......除了长时间的民意调查之外什么都没有想到。知道是否有一些webhook选项,我可以注册一个URL并收到通知吗?

4 个答案:

答案 0 :(得分:5)

2016-04-27更新

AWS正式announced this in February 2016

  

您现在可以创建在应用程序部署过程之前,期间和之后发送Amazon SNS通知的触发器。可以为整个部署或部署所针对的各个实例设置触发器,并在成功和失败时发送。

原始答案

尚未。

this AWS forum thread中,要求CodeDeploy发出事件,以便您可以使用Lambda处理它们而不是轮询细节。

AWS员工的回答(强调我的):

  

我们在CodeDeploy上同意。   不幸的是,我无法给你一个确切的发布日期,但请关注我们的公告,即将推出。

答案 1 :(得分:2)

以下是AWS Lambda函数的要点,该函数将格式化的CodeDeploy通知发布到Slack

https://gist.github.com/MrRoyce/097edc0de2fe001288be2e8633f4b22a

var services = '/services/...';  // Update this with your Slack service...
var channel = "#aws-deployments"  // And this with the Slack channel

var https = require('https');
var util = require('util');

var formatFields = function(string) {
  var
    message = JSON.parse(string),
    fields  = [],
    deploymentOverview;

  // Make sure we have a valid response
  if (message) {
    fields = [
      {
        "title" : "Task",
        "value" : message.eventTriggerName,
        "short" : true
      },
      {
        "title" : "Status",
        "value" : message.status,
        "short" : true
      },
      {
        "title" : "Application",
        "value" : message.applicationName,
        "short" : true
      },
      {
        "title" : "Deployment Group",
        "value" : message.deploymentGroupName,
        "short" : true
      },
      {
        "title" : "Region",
        "value" : message.region,
        "short" : true
      },
      {
        "title" : "Deployment Id",
        "value" : message.deploymentId,
        "short" : true
      },
      {
        "title" : "Create Time",
        "value" : message.createTime,
        "short" : true
      },
      {
        "title" : "Complete Time",
        "value" : ((message.completeTime) ? message.completeTime : ''),
        "short" : true
      }
    ];

    if (message.deploymentOverview) {
     deploymentOverview = JSON.parse(message.deploymentOverview);
     
      fields.push(
        {
          "title" : "Succeeded",
          "value" : deploymentOverview.Succeeded,
          "short" : true
        },
        {
          "title" : "Failed",
          "value" : deploymentOverview.Failed,
          "short" : true
        },
        {
          "title" : "Skipped",
          "value" : deploymentOverview.Skipped,
          "short" : true
        },
        {
          "title" : "In Progress",
          "value" : deploymentOverview.InProgress,
          "short" : true
        },
        {
          "title" : "Pending",
          "value" : deploymentOverview.Pending,
          "short" : true
        }
      );
    }
  }

  return fields;

}

exports.handler = function(event, context) {

    var postData = {
        "channel": channel,
        "username": "AWS SNS via Lamda :: CodeDeploy Status",
        "text": "*" + event.Records[0].Sns.Subject + "*",
        "icon_emoji": ":aws:"
    };

    var fields = formatFields(event.Records[0].Sns.Message);
    var message = event.Records[0].Sns.Message;
    var severity = "good";

    var dangerMessages = [
        " but with errors",
        " to RED",
        "During an aborted deployment",
        "FAILED",
        "Failed to deploy application",
        "Failed to deploy configuration",
        "has a dependent object",
        "is not authorized to perform",
        "Pending to Degraded",
        "Stack deletion failed",
        "Unsuccessful command execution",
        "You do not have permission",
        "Your quota allows for 0 more running instance"];

    var warningMessages = [
        " aborted operation.",
        " to YELLOW",
        "Adding instance ",
        "Degraded to Info",
        "Deleting SNS topic",
        "is currently running under desired capacity",
        "Ok to Info",
        "Ok to Warning",
        "Pending Initialization",
        "Removed instance ",
        "Rollback of environment"
        ];

    for(var dangerMessagesItem in dangerMessages) {
        if (message.indexOf(dangerMessages[dangerMessagesItem]) != -1) {
            severity = "danger";
            break;
        }
    }

    // Only check for warning messages if necessary
    if (severity == "good") {
        for(var warningMessagesItem in warningMessages) {
            if (message.indexOf(warningMessages[warningMessagesItem]) != -1) {
                severity = "warning";
                break;
            }
        }
    }

    postData.attachments = [
        {
            "color": severity,
            "fields": fields
        }
    ];

    var options = {
        method: 'POST',
        hostname: 'hooks.slack.com',
        port: 443,
        path: services  // Defined above
    };

    var req = https.request(options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
        context.done(null);
      });
    });

    req.on('error', function(e) {
      console.log('problem with request: ' + e.message);
    });

    req.write(util.format("%j", postData));
    req.end();
};

答案 2 :(得分:0)

虽然没有本机解决方案,但您可以使用一种解决方法来实现此目的。您可以使用lambda来触发这些事件。在AWS博客上,他们展示了在将文件上传到S3(https://blogs.aws.amazon.com/application-management/post/Tx3TPMTH0EVGA64/Automatically-Deploy-from-Amazon-S3-using-AWS-CodeDeploy)时如何通过lambda触发codedeploy。使用相同的概念,您可以让lambda函数侦听错误/成功存储桶,并修改codedeploy包以将文件上传到s3,然后您可以将其用作事件触发器以通过SES发送电子邮件({{3 }}或联系您所需的Web服务/页面。这可能是一个小小的问题,但它完成了工作。

答案 3 :(得分:0)

从高层次上讲,您需要:

  • 设置SNS主题
  • 创建CodeDeploy触发器
  • 向CodeDeploy IAM角色添加sample_dict = { "key_1": { "sub_key_1": 1, "sub_key_2": 2, }, "key_2": { "sub_key_1": 3, "sub_key_2": { "sub_sub_key_1": 4, }, }, } def key_find(sample_dict, li=[]): for key, val in sample_dict.items(): if isinstance(val, dict): key_find(val, li=li + [key]) else: print(li + [key] + [val]) key_find(sample_dict) 权限
  • 使用传入的Webhook配置Slack
  • 编写并配置Lambda函数以处理CodeDeploy的SNS消息,构造Slack消息并将其发送到传入Webhook上的Slack

我使用了与上面的要点类似的代码来为Lambda函数设置CodeDeploy事件的Slack通知。我记录了整个过程,包括屏幕快照over here

我在其他地方找不到类似的端到端指南,因此希望对遇到这个问题的人有所帮助。