Skip to content

Instantly share code, notes, and snippets.

@Sutto
Last active March 31, 2016 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sutto/6e5e2d96475eb07ca44a to your computer and use it in GitHub Desktop.
Save Sutto/6e5e2d96475eb07ca44a to your computer and use it in GitHub Desktop.
Buildkite Lambda Function

We use this flow with AWS lambda, combined with CloudWatch Scheduled Events, to trigger our builds periodically.

It's nice, avoids duplication and keeps everything together easily.

{
"organization": "your-org",
"pipeline": "your-pipeline",
"token": "write-only-key-here",
"build": {
"message": "Build Something"
}
}
var https = require('https');
function extendHash(destination, source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
destination[property] = source[property];
}
}
return destination;
}
var DEFAULT_BUILD = {
commit: "origin/master",
branch: "master",
message: "Triggered via CloudWatch",
author: {
name: "Automated Builds",
email: "hello@world.tv"
}
}
function isValidInput(event) {
if(event.organization && event.pipeline && event.token) return true;
return false;
}
function buildRequestOptions(input) {
var options = extendHash({}, DEFAULT_BUILD);
var build = input.build;
if(build) extendHash(options, build);
return options;
}
function requestPath(event) {
return "/v2/organizations/" + event.organization + "/pipelines/" + event.pipeline + "/builds"
}
exports.handler = function(event, context) {
if(!isValidInput(event)) {
context.fail("Unable to generate for input data.")
return;
}
var requestBody = JSON.stringify(buildRequestOptions(event));
var path = requestPath(event);
var headers = {
'Content-Type': 'application/json',
'Content-Length': requestBody.length,
'Authorization': ("Bearer " + event.token)
};
var options = {
host: 'api.buildkite.com',
path: path,
port: 443,
method: 'POST',
headers: headers
};
var req = https.request(options, function(res) {
console.log('Status:', res.statusCode);
context.succeed();
console.log("Succeeded...")
});
req.on('error', context.fail);
req.write(requestBody);
req.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment