Skip to content

Instantly share code, notes, and snippets.

@WhoAteDaCake
Last active March 2, 2018 18:07
Show Gist options
  • Save WhoAteDaCake/fb0973e3fd1bab1786495428e44aedd9 to your computer and use it in GitHub Desktop.
Save WhoAteDaCake/fb0973e3fd1bab1786495428e44aedd9 to your computer and use it in GitHub Desktop.
Deployment scripts
const util = require("util");
const exec = util.promisify(require("child_process").exec);
function getImageId(str) {
return str.match(/^(Successfully built)\s(.*)$/gm)[0].replace('Successfully built ', '')
}
function getContainerId(str) {
return (/^([^\s]*).*group17$/gm).exec(str)[1];
}
async function run(command) {
let resp;
let error;
const { stdout, stderr } = await exec(command.replace('\n', ''));
if (stderr) {
console.error(stderr);
}
return stdout;
}
const containerName = 'group17';
module.exports = async () => {
const imageId = await run(`
cd ./group-17 &&
git pull origin master &&
docker build -t group17-docker .
`).then(getImageId);
const containerId = await run('docker ps').then(getContainerId);
await run(`docker stop ${containerId}`)
.then(() => run(`docker rm ${containerId}`));
const runResp = await run(`
docker run --name ${containerName} -d -p 3030:8080 -e BASE_URL='http://group17.augustinas.me' ${imageId}
`);
}
var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler({ path: '/', secret: 'my-secret' })
const deploy = require('./deploy');
let deploying = false;
http.createServer(function (req, res) {
handler(req, res, function (err) {
res.statusCode = 404
res.end('no such location')
})
}).listen(3001)
handler.on('error', function (err) {
console.error('Error:', err.message)
})
handler.on('push', async (event) => {
if (event.payload.ref.includes('master') && !deploying) {
console.log('Pushed to master will redeploy');
deploying = true;
const time = Date.now();
await deploy();
deploying = false;
console.log(`Deployed, took ${Date.now() - time}ms`);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment