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: "[email protected]" | |
| } | |
| } | |
| 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(); | |
| } |