L
O
A
D
I
N
G

生成tag号的脚本

平时改完bug重新部署测试环境,总要重新打tag,虽然也就几行git命令的事,但能用一句命令解决还是舒服的

在scripts新建文件 generateTag.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 基于当前分支生成 tag 号,并推送至远程
*/
const { execSync } = require('child_process');
const readline = require('readline');

// 生成当前时间的 tag,格式为 YYYYMMDDHHmmss
function getCurrentTag() {
const now = new Date();
const pad = n => n < 10 ? '0' + n : n;
return [
now.getFullYear(),
pad(now.getMonth() + 1),
pad(now.getDate()),
pad(now.getHours()),
pad(now.getMinutes()),
pad(now.getSeconds())
].join('');
}


try {
// 获取当前分支名
const branch = execSync('git rev-parse --abbrev-ref HEAD').toString().trim();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

rl.question(`当前分支为 "${branch}",确定要在该分支上打 tag 并推送吗?(y/N): `, (answer) => {
if (answer.toLowerCase() !== 'y') {
console.log('操作已取消。');
rl.close();
process.exit(0);
}
try {
const tag = getCurrentTag();
execSync(`git tag ${tag}`, { stdio: 'inherit' });
execSync(`git push origin ${tag}`, { stdio: 'inherit' });
console.log(`Tag ${tag} 已创建并推送到远端`);
} catch (err) {
console.error('创建或推送 tag 失败:', err.message);
process.exit(1);
}
rl.close();
process.exit(0);
});
return;
} catch (err) {
console.error('创建或推送 tag 失败:', err.message);
process.exit(1);
}

在package.json中的’scripts’添加

1
"tag": "node ./scripts/generateTag"

使用

1
npm run tag
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2022-2025 耀耀切克闹

        我是穷比,在线乞讨!

        支付宝
        微信