簡體   English   中英

在 Amazon 的 CodeDeploy 上獲得成功/失敗構建的通知

[英]Get notified of successful/failed builds on Amazon's CodeDeploy

我想構建一個工具,每次在 CodeDeploy 上構建成功或失敗時都會通過任何通信媒介(電子郵件、松弛等)通知用戶。 我已經瀏覽了他們的文檔......除了長輪詢之外什么都沒有想到。 知道是否有一些 webhook 選項可以讓我注冊 URL 並收到通知嗎?

2016-04-27 更新

AWS 在 2016 年 2 月正式宣布了這一點

您現在可以創建觸發器,在應用程序的部署過程之前、期間和之后發送 Amazon SNS 通知。 可以為整個部署或部署所針對的單個實例設置觸發器,並在成功和失敗時發送。

原答案

還沒有。

此 AWS 論壇主題 中,要求 CodeDeploy 發出事件,以便您可以使用 Lambda 來處理它們,而不是輪詢詳細信息。

AWS 員工的回答(重點是我的):

我們在這里對 CodeDeploy 表示同意。 不幸的是,我不能給你一個確切的發布日期,但請密切關注我們的公告,它很快就會發布。

這是一個 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(); };

盡管沒有本機解決方案,但您可以采用一種變通方法來實現此目的。 您可以使用 lambda 來觸發這些事件。 在 AWS 博客上,他們展示了當您將文件上傳到 S3 時如何通過 lambda 觸發 codedeploy ( https://blogs.aws.amazon.com/application-management/post/Tx3TPMTH0EVGA64/Automatically-Deploy-from-Amazon- S3-using-AWS-CodeDeploy )。 使用相同的概念,您可以讓 lambda 函數偵聽錯誤/成功存儲桶,並修改您的 codedeploy 包以將文件上傳到 s3,然后您可以將其用作事件觸發器以通過 SES ( https:/ /peekandpoke.wordpress.com/2015/02/26/dancing-the-lambada-with-aws-lambda-or-sending-emails-on-s3-events/ ) 或聯系可以執行您想要的操作的網絡服務/頁面. 這可能有點矯情,但它完成了工作。

在高層次上,您需要:

  • 設置 SNS 主題
  • 創建 CodeDeploy 觸發器
  • 向 CodeDeploy IAM 角色添加sns:Publish權限
  • 使用傳入的 Webhook 配置 Slack
  • 編寫並配置一個 Lambda 函數來處理 CodeDeploy 的 SNS 消息,構造 Slack 消息並將它們發送到傳入 Webhook 的 Slack

我使用類似於上述要點的代碼為 CodeDeploy 事件的 Slack 通知設置了一個 Lambda 函數。 我記錄了整個過程,包括這里的屏幕截圖。

我在其他地方找不到類似的端到端指南,所以我希望這能幫助那些偶然發現這個問題的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM