Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #115 from Financial-Times/travis-redeploy
Browse files Browse the repository at this point in the history
Travis redeploy
  • Loading branch information
matthew-andrews committed Jun 10, 2015
2 parents 99dba4d + 477b41d commit ca569ac
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
run [options] Runs the local app through the router
about Creates an __about.json file for the app
deploy-static [options] <source> [otherSources...] Deploys static <source> to [destination] on S3 (where [destination] is a full S3 URL). Requires AWS_ACCESS and AWS_SECRET env vars
rebuild [apps...] Trigger a rebuild of the latest master on Travis
*

Options:
Expand Down
10 changes: 10 additions & 0 deletions bin/next-build-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var deployHashedAssets = require('../tasks/deploy-hashed-assets');
var deployStatic = require('../tasks/deploy-static');
var run = require('../tasks/run');
var about = require('../tasks/about');
var rebuild = require('../tasks/rebuild');

function list(val) {
return val.split(',');
Expand Down Expand Up @@ -192,6 +193,15 @@ program
}).catch(exit);
});

program
.command('rebuild [apps...]')
.description('Trigger a rebuild of the latest master on Travis')
.action(function(apps) {
return rebuild({
apps: apps
}).catch(exit);
});

program
.command('*')
.description('')
Expand Down
91 changes: 91 additions & 0 deletions tasks/rebuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'use strict';

var fetchres = require('fetchres');
var denodeify = require('denodeify');
var fs = require('fs');
var readFile = denodeify(fs.readFile);

var travisToken;

function travisFetch(path, opts) {
opts = opts || {};
path = 'https://api.travis-ci.org' + path;
opts.timeout = opts.timeout || 3000;
opts.headers = opts.headers || {};
opts.headers['Content-Type'] = opts.headers['Content-Type'] || 'application/json';
opts.headers.Accept = opts.headers.Accept || 'application/vnd.travis-ci.2+json';
if (travisToken && !opts.headers.Authorization) {
opts.headers.Authorization = 'token ' + travisToken;
}
return fetch(path, opts)
.then(function(res) {
var ok = res.ok;
var status = res.status;
return res.json()
.then(function(data) {
if (ok) {
return data;
} else {
console.log("Response not OK for " + path + ", got: " + status);
console.log(data);
throw new Error(status);
}

});
});
}

module.exports = function(options) {
var apps = options.apps;

return readFile(process.env.HOME + '/.github_token', { encoding: 'UTF8' })
.then(function(githubToken) {
return travisFetch('/auth/github', {
method: 'POST',
body: JSON.stringify({ github_token: githubToken })
});
})
.then(function(data) {
travisToken = data.access_token;

if (apps.length === 0) {
return fetch('http://next-registry.ft.com/services')
.then(fetchres.json)
.then(function(data) {
apps = data
.filter(function(apps) { return apps.versions['1']; })
.map(function(app) {
var repo;
Object.keys(app.versions).forEach(function(version) {
version = app.versions[version];
if (/https?:\/\/github\.com\/Financial-Times\//.test(version.repo)) {
repo = version.repo.replace(/https?:\/\/github\.com\/Financial-Times\//, '');
repo = repo.replace(/\/$/, '');
}
});
return repo;
})
.filter(function(repo) { return repo; });
});
}
})
.then(function() {
return Promise.all(apps.map(function(app) {
console.log("Considering whether to rebuild " + app);
return travisFetch('/repos/Financial-Times/' + app + '/branches/master')
.then(function(data) {
if (data.branch.state === 'passed' || data.branch.state === 'failed') {
console.log("Triggering rebuild of last master build of " + app + " (" + data.commit.author_name + ": " + data.commit.message.replace(/\n/g, " ") + ")");
return travisFetch('/builds/' + data.branch.id + '/restart', {
method: 'POST'
});
} else {
console.log("Skipping rebuild of " + app + " because job already exists.");
}
})
.catch(function() {
console.log("Skipped rebuild of " + app + " probably because Travis CI not set up for this repo");
});
}));
});
};

0 comments on commit ca569ac

Please sign in to comment.