개발모음집

express github webhook settings 본문

Server/node.js

express github webhook settings

void 2020. 1. 13. 10:00

 

app.js

let crypto = require('crypto');

app.post('/push', (req, res) => {
    console.log('[LOG] request received');
    res.status(400).set('Content-Type', 'application/json');

    let jsonString = JSON.stringify(req.body);
    let hash = "sha1=" + crypto.createHmac('sha1', secret).update(jsonString).digest('hex');

    if (hash != req.get('x-hub-signature')) {
        console.log('[ERROR] invalid key');
        let data = JSON.stringify({"error": "invalid key", key: hash});
        return res.end(data);
    }

    console.log("[LOG] running hook.sh");

    let deploySh = spawn('sh', ['hook.sh']);
    deploySh.stdout.on('data', function (data) {
        let buff = new Buffer(data);
        console.log(buff.toString('utf-8'));
    });

    let data = JSON.stringify({"success": true});
    console.log('[LOG] success!!');
    return res.status(200).end(data);
});

 

hook.sh

REPOSITORY = "/server/SeniorCameraBackend"     // REPOSITORY 변수에 git pull 받고자 하는 repository 설정
cd $REPOSITORY      // 해당 디렉토리로 이동
git pull origin master           // git pull

 

github 설정

 

참고 : https://github.com/cliche90/webhook_autopullhttps://velopert.com/739