Created
October 13, 2014 22:16
-
-
Save naosim/21e1a3b6fec3da10b892 to your computer and use it in GitHub Desktop.
node製簡易CI環境です。定期的にgitのアップデートを確認して、任意のスクリプトを実行できます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
実行例 | |
node gitci.js -i 10 deploy.sh | |
引数 | |
-i: interval[分] デフォルト5分 | |
最終引数: アップデートがあった場合に実行するコマンド | |
*/ | |
// 引数取得 | |
var shFile = process.argv[process.argv.length - 1]; | |
var interval = 5; | |
for(var i = 0; i < process.argv.length - 1; i++) { | |
if(process.argv[i] == '-i') { | |
interval = parseInt(process.argv[++i], 10); | |
} | |
} | |
// gitのアップデートをチェックするコマンド | |
var isUpdateCmd = ''; | |
isUpdateCmd += 'before=`git log --pretty=format:"%h" -1`\n'; | |
isUpdateCmd += 'git fetch\n'; | |
isUpdateCmd += 'after=`git log --pretty=format:"%h" origin/master -1`\n'; | |
isUpdateCmd += 'if test $before != $after; then\n'; | |
isUpdateCmd += ' exit 0\n'; | |
isUpdateCmd += 'else\n'; | |
isUpdateCmd += ' exit 1\n'; | |
isUpdateCmd += 'fi\n'; | |
var exec = require('child_process').exec; | |
var checkUpdate = function(updateAction) { | |
exec(isUpdateCmd, function(err, stdout, stderr) { | |
if (!err) { | |
console.log(new Date() + ' update'); | |
updateAction(); | |
} else { | |
console.log(new Date() + ' none'); | |
} | |
}); | |
}; | |
var runScript = function() { | |
console.log('bash ' + shFile); | |
exec('bash ' + shFile, function(err, stdout, stderr) { | |
if (!err) { | |
console.log(new Date() + ' ' + stdout); | |
console.log(new Date() + ' script success'); | |
} else { | |
console.log(new Date() + ' script error'); | |
} | |
}); | |
}; | |
var run = function() { | |
checkUpdate(runScript); | |
}; | |
setInterval(run, interval * 60 * 1000); | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment