Created
October 3, 2016 09:24
-
-
Save nisshiee/17d8b5a17bf16c3f4cedcb4b3510ca91 to your computer and use it in GitHub Desktop.
AWS Lambda Script for CircleCI nightly build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var projects = [ | |
['githubuser/repo1', 'circleci_api_token_for_repo1'], | |
['githubuser/repo2', 'circleci_api_token_for_repo2'], | |
]; | |
var https = require('https') | |
var callApi = function(method, path, body, callback) { | |
var options = { | |
hostname: 'circleci.com', | |
method: method, | |
path: path, | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json' | |
} | |
}; | |
console.log(method + ': ' + path); | |
var req = https.request(options, function(res) { | |
console.log('STATUS: %d', res.statusCode); | |
console.log('HEADERS: %s', JSON.stringify(res.headers)); | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
console.log('BODY: %s', chunk); | |
}); | |
res.on('end', function() { | |
console.log('No more data in response.'); | |
if (callback != null) { | |
callback(); | |
} | |
}); | |
}); | |
req.on('error', function(e) { | |
console.log('problem with request: %s', e.message); | |
}); | |
if (body != null) { | |
req.write(body); | |
} | |
req.end(); | |
}; | |
var triggerBuild = function(project, token) { | |
var path = '/api/v1.1/project/github/' + project + '/tree/master?circle-token=' + token; | |
var body = JSON.stringify({ | |
build_parameters: { | |
NIGHTLY_BUILD: true | |
} | |
}); | |
callApi('POST', path, body); | |
}; | |
var clearCache = function(project, token, callback) { | |
var path = '/api/v1.1/project/github/' + project + '/build-cache?circle-token=' + token; | |
callApi('DELETE', path, null, callback); | |
}; | |
var triggerNightlyBuild = function(project, token) { | |
clearCache(project, token, function() { | |
triggerBuild(project, token); | |
}); | |
}; | |
exports.handler = function(event, context) { | |
projects.forEach(function(project) { | |
triggerNightlyBuild(project[0], project[1]); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment