Skip to content

Instantly share code, notes, and snippets.

@alexcheng1982
Created December 17, 2017 21:14
Show Gist options
  • Save alexcheng1982/508282536f597df59bdd35f509d59388 to your computer and use it in GitHub Desktop.
Save alexcheng1982/508282536f597df59bdd35f509d59388 to your computer and use it in GitHub Desktop.
Hubot handler
const yargs = require('yargs');
const spawn = require('child_process').spawn;
module.exports = (robot) => {
robot.respond(/deploy (.*)/i, (res) => {
let parser = yargs.command('deploy <build_num>', 'deploy a version', (yargs) => {
yargs
.positional('build_num', {
describe: 'build number',
})
.option('debug', {
alias: 'd',
describe: 'enable debug output',
type: 'boolean',
default: false,
});
}, (argv) => {
const user = res.envelope.user.name.replace(/\W+/, '_');
const buildNum = argv.build_num;
res.send(`(waiting) Deploying #${argv.build_num} (tea)`);
const process = spawn(`ansible-playbook -i hosts app.yml --extra-vars "build_num=${buildNum} owner=${user}"`, [], {
cwd: '/etc/ansible',
shell: true,
});
process.stdout.on('data', (data) => {
if (argv.debug) {
res.send(`${data}`);
}
});
process.stderr.on('data', (data) => {
res.send(`${data}`);
});
process.on('close', (code) => {
if (code !== 0) {
res.send('(boom) Deployment failed');
} else {
res.send(`(successful) Deployment completed. You can access it using http://${buildNum}.mycompany.com`);
}
});
})
.help();
parser.parse(`deploy ${res.match[1]}`, (error, argv, output) => {
if (output) {
res.reply(output)
}
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment