You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ghost/.github/scripts/bump-version.js

62 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 引入所需模块文件系统Promise版、子进程execPromise化、路径处理
const fs = require('fs/promises');
const exec = require('util').promisify(require('child_process').exec);
const path = require('path');
// 引入GitHub Actions核心模块用于设置输出和语义化版本处理工具
const core = require('@actions/core');
const semver = require('semver');
// 异步自执行函数:处理版本号更新逻辑
(async () => {
// 1. 定位并读取core模块的package.json文件
const corePackageJsonPath = path.join(__dirname, '../../ghost/core/package.json');
const corePackageJson = require(corePackageJsonPath);
// 2. 获取当前版本号并打印
const current_version = corePackageJson.version;
console.log(`Current version: ${current_version}`);
// 3. 获取命令行传入的第一个参数(用于判断版本类型)
const firstArg = process.argv[2];
console.log('firstArg', firstArg);
// 4. 获取当前Git提交的短哈希值用于构建版本号
const buildString = await exec('git rev-parse --short HEAD')
.then(({stdout}) => stdout.trim()); // 去除输出中的换行符
let newVersion; // 声明新版本号变量
// 5. 根据命令行参数生成不同类型的新版本号
if (firstArg === 'canary' || firstArg === 'six') {
// 对于canary或six类型升级minor版本并添加预发布标签包含Git哈希
const bumpedVersion = semver.inc(current_version, 'minor'); // 升级次要版本如1.2.3 → 1.3.0
newVersion = `${bumpedVersion}-pre-g${buildString}`; // 格式1.3.0-pre-gabc123
} else {
// 其他情况使用git describe生成版本号基于最近的标签
// git describe --long会输出类似"v1.2.3-4-gabc123"这里去除前缀v
const gitVersion = await exec('git describe --long HEAD')
.then(({stdout}) => stdout.trim().replace(/^v/, ''));
newVersion = gitVersion;
}
// 6. 统一添加+moya后缀可能用于标识自定义构建
newVersion += '+moya';
console.log('newVersion', newVersion);
// 7. 更新core模块的package.json版本号并写入文件
corePackageJson.version = newVersion;
await fs.writeFile(corePackageJsonPath, JSON.stringify(corePackageJson, null, 2)); // 保留2空格缩进
// 8. 同步更新admin模块的package.json版本号
const adminPackageJsonPath = path.join(__dirname, '../../ghost/admin/package.json');
const adminPackageJson = require(adminPackageJsonPath);
adminPackageJson.version = newVersion;
await fs.writeFile(adminPackageJsonPath, JSON.stringify(adminPackageJson, null, 2));
console.log('Version bumped to', newVersion);
// 9. 设置GitHub Actions输出变量供后续步骤使用
core.setOutput('BUILD_VERSION', newVersion); // 输出构建版本号
core.setOutput('GIT_COMMIT_HASH', buildString) // 输出Git提交哈希
})();