trigger the build only if specific branches are pushed

This commit is contained in:
Kasra Bigdeli
2017-12-18 11:54:18 -08:00
parent 709b3a99c1
commit 06e1e1da6d
2 changed files with 54 additions and 7 deletions
+52 -7
View File
@@ -7,6 +7,31 @@ const ApiStatusCodes = require('../api/ApiStatusCodes');
const Logger = require('../utils/Logger');
const CaptainConstants = require('../utils/CaptainConstants');
router.post('/triggerbuild', function (req, res, next) {
// find which branch is pushed
// inject it in locals.pushedBranches
let isGithub = req.header('X-GitHub-Event') === 'push';
let isBitbucket = (req.header('X-Event-Key') === 'repo:push') && req.header('X-Request-UUID') && req.header('X-Hook-UUID');
res.locals.pushedBranches = [];
if (isGithub) {
let ref = req.body.ref; // "ref/heads/somebranch"
res.locals.pushedBranches.push(ref.substring(11, ref.length));
}
else if (isBitbucket) {
for (let i = 0; i < req.body.push.changes.length; i++) {
res.locals.pushedBranches.push(req.body.push.changes[i].new.name);
}
}
next();
});
router.post('/triggerbuild', function (req, res, next) {
res.sendStatus(200);
@@ -15,7 +40,7 @@ router.post('/triggerbuild', function (req, res, next) {
let app = res.locals.app;
let namespace = res.locals.user.namespace;
if (!app || !serviceManager || !namespace|| !appName) {
if (!app || !serviceManager || !namespace || !appName) {
Logger.e('Something went wrong during trigger build. Cannot extract app information from the payload.');
return;
}
@@ -28,14 +53,34 @@ router.post('/triggerbuild', function (req, res, next) {
})
.then(function (repoInfo) {
return serviceManager.createImage(appName, {
repoInfo: repoInfo
});
// if we didn't detect any branches, the POST might have come from another source that we don't
// explicitly support. Therefore, we just let it go through and triggers a build anyways
if (res.locals.pushedBranches.length > 0) {
let branchIsTracked = false;
})
.then(function (version) {
for (let i = 0; i < res.locals.pushedBranches.length; i++) {
if (res.locals.pushedBranches[i] === repoInfo.branch) {
branchIsTracked = true;
break;
}
}
return serviceManager.ensureServiceInitedAndUpdated(appName, version);
// POST call was triggered due to another branch being pushed. We don't need to trigger the build.
if (!branchIsTracked) {
return;
}
}
return serviceManager
.createImage(appName, {
repoInfo: repoInfo
})
.then(function (version) {
return serviceManager.ensureServiceInitedAndUpdated(appName, version);
});
})
.catch(function (error) {
+2
View File
@@ -16,6 +16,7 @@ function getTagsForImage(imageBaseName, url, allTags) {
function (error, response, body) {
if (error || !body) {
Logger.e(error);
reject(error);
return;
}
@@ -29,6 +30,7 @@ function getTagsForImage(imageBaseName, url, allTags) {
}
if (!results) {
Logger.e('NO RESULT');
reject(new Error('NO RESULT'));
return;
}