GITLAB UPDATE HOOK in Node.js

علی ذوالفقار
1401/01/20 08:59:53 (512)
update.js 
--------------------------------------------------------------
const {exec} = require('child_process');

const update = ()=>{    
    const command = 'git pull';
    exec(command, (error, stdout, stderr) => {
        if(stderr) console.error(`stderr: ${stderr}`);
        if(error)  console.error(`exec error: ${error}`);
        console.log('upate app : '); 
        console.log(stdout); 
    });// exec
};

const checkUpdate = async () => {
    return new Promise((resolve , reject)=>{
        const command = 'git fetch && git show-ref';
        exec(command, (error, stdout, stderr) => {
            if(stderr) console.error(`stderr: ${stderr}`);
            if(error)  console.error(`exec error: ${error}`);

            if (error) return reject(error);
                // fetch and get hash data 
                const data = stdout.split('\n').filter(item => item.length > 0).map(item => item.split(' ')[0]);
                // console.log(data); 
                if (data[0] != data[1]) {
                    resolve(true);
                } else {
                    resolve(false);
                }
        });// exec        
    });// promis
};//checkUpdate


const check = async()=>{
    var needUpdate = await checkUpdate();
    if(needUpdate){
        console.log('app need update ?' , needUpdate);
        update();
    }else{
        console.log('app need update ?' , needUpdate);
    }
};

// check()
module.exports = {check , checkUpdate , update}




router.js
--------------------------------------------------------------
const express = require('express');
const router = express.Router();
const update = require('../cli/update');

router.all('/updatehook',(req,res)=>{
    console.log('updatehook'); 
    update.check();
    res.json({success : true , message : 'update process initiated'})
})



Back