forked from lambci/lambci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (38 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var utils = require('./utils')
var config = require('./utils/config')
var log = require('./utils/log')
var cfn = require('./cfn')
var actions = require('./actions')
var sns = require('./sources/sns')
exports.handler = function(event, context, cb) {
log.init(`LambCI v${config.VERSION} triggered on stack "${config.STACK}"\n`) // STACK is usually 'lambci'
// Check if it's the CloudFormation stack calling us
if (event.ResourceType == 'Custom::ConfigUpdater') {
return cfn.update(event, context, cb)
// Or a custom (manual) event
} else if (typeof actions[event.action] == 'function') {
return actions[event.action](event, context, cb)
// Otherwise it should be SNS
} else if (event.Records && event.Records[0] && event.Records[0].Sns) {
return snsBuild(event.Records[0].Sns, context, cb)
}
log.error('Unknown event, ignoring:\n%j', event)
return cb(new Error('Unknown event'))
}
function snsBuild(snsEvent, context, cb) {
// Lambda/SNS currently has no setting to determine whether errors should be retried
// By default they are, which we don't want, so always try to callback successfully
var done = utils.once(function snsDone(err, data) {
log.logIfErr(err)
cb(null, data)
})
sns.parseEvent(snsEvent, function(err, buildData) {
if (err) return done(err)
if (buildData.ignore) {
log.info(buildData.ignore)
log.info('Not running build')
return done()
}
actions.build(buildData, context, done)
})
}