实现推送jenkins通知打包功能


#!/usr/bin/env zx
 
import 'zx/globals';
 
// $.shell = 'C:\Program Files\Git\bin\bash.exe';
// $.cmd = "/usr/bin/zsh";
$.verbose = false;
 
console.log(chalk.blue('==== 准备部署 ===='));
 
const gitStatus = (await $`git status`).stdout
 
// 推送并发布
const pushAndDeploy = async () => {
    let branch = await question('请输入要推送的分支名称: ');
    await $`git push origin ${branch}`
        const pushStatus = (await $`git push origin ${branch}`).stdout;
        if(!pushStatus.includes('error: failed')) {
            console.log(chalk.green(`==== 提交${branch}分支到gitlab成功 ====`));
            console.log(chalk.green('==== 通知Jenkins打包 ===='));
            await fetch(
                'http://10.5.1.144:8080/generic-webhook-trigger/invoke?token=test12341234'
            )
                .then(res => res.json())
                .then(json => {
                    if(json.message === 'Triggered jobs.') {
                        console.log(chalk.green('==== 通知完成,请等待1-3分钟构建 ===='));
                    }else{
                        console.log(chalk.red(`==== 出现错误${json.message} ====`));
                    }
                })
                .catch(err => {
                    console.log(chalk.red(`==== 出现错误${err.message} ====`));
                });
        }
}
// console.log('请输入要推送的分支名称: ', branch);
// 没有commit 可以部署
// 判断是否有未提交的commit
if(gitStatus.includes('nothing to commit, working tree clean')) {
    await pushAndDeploy()
    console.log(chalk.blue('==== 部署结束 ===='));
}else{
    console.log(chalk.red('==== 有更新将自动add所有文件并commit ===='));
    let msg = await question("Commit message: ");
 
    await $`git add .`;
 
    await $`git commit -m  "${msg || 'init'}"`;
 
    await pushAndDeploy()
}
 
console.log(chalk.blue('==== 部署完成 ===='));

效果请输入图片描述

原理:
使用shell托管用户git commit/push 等操作,并利用Jenkins Web Hooks通知触发 执行构建。

最后修改:2023 年 03 月 06 日
如果觉得我的文章对你有用,请随意赞赏