有没有一种方法可以通过nodejs获取git origin分支代码

时间:2019-09-30 05:59:14

标签: javascript node.js git koa

当触发事件触发时,我在项目中添加了一个git钩子。

触发钩子并且ref值为refs/heads/master时,将更新prod index.html文件。

我想阅读新的index.html内容

router.post('/gitHook', async (ctx, next) => {
    const body = ctx.request.body;
    const matches = body.ref.match(/^refs\/heads\/(.*)/);
    const branchName = matches[1];
    console.log(branchName);
    if(branchName === 'master'){
        console.log('should get new code from git origin master')
    }
    await next();
});

1 个答案:

答案 0 :(得分:0)

我使用index.html软件包获得了gitlab的内容。

const { Gitlab } = require('gitlab');
const api = new Gitlab({
    host: 'myhost',
    token: 'myToken',
});

api.RepositoryFiles.show(body.project_id, '/build/index.html', body.ref).then(res => {
    const content = res.content;
    const stringContent = new Buffer(content, 'base64').toString();
    console.log(stringContent);
});