long-git-cli
Version:
A CLI tool for Git tag management.
221 lines • 8.85 kB
JavaScript
;
/**
* 完整部署流程编排器
* 整合 Git Tag 创建、Pipeline 监听、Jenkins 部署等功能
*/
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.FullDeployer = exports.DeployStep = void 0;
const pipeline_monitor_1 = require("../monitor/pipeline-monitor");
const jenkins_deployer_1 = require("./jenkins-deployer");
const simpleGit = __importStar(require("simple-git"));
const tag_1 = require("../../utils/tag");
/**
* 部署步骤枚举
*/
var DeployStep;
(function (DeployStep) {
DeployStep["CREATING_TAG"] = "CREATING_TAG";
DeployStep["PUSHING_TAG"] = "PUSHING_TAG";
DeployStep["MONITORING_PIPELINE"] = "MONITORING_PIPELINE";
DeployStep["DEPLOYING_JENKINS"] = "DEPLOYING_JENKINS";
DeployStep["COMPLETED"] = "COMPLETED";
DeployStep["FAILED"] = "FAILED";
})(DeployStep || (exports.DeployStep = DeployStep = {}));
/**
* 完整部署流程编排器类
*/
class FullDeployer {
constructor(bitbucketClient, jenkinsClient, projectConfig, environmentConfig) {
this.bitbucketClient = bitbucketClient;
this.jenkinsClient = jenkinsClient;
this.projectConfig = projectConfig;
this.environmentConfig = environmentConfig;
}
/**
* 执行完整部署流程
*/
async deploy(options = {}) {
const { createNewTag = false, tagName, onProgress } = options;
let currentTag = tagName;
try {
/** 步骤 1: 创建 Tag(如果需要) */
if (createNewTag) {
this.reportProgress(onProgress, {
step: DeployStep.CREATING_TAG,
message: "正在创建 Git Tag...",
timestamp: new Date(),
});
currentTag = await this.createTag();
this.reportProgress(onProgress, {
step: DeployStep.CREATING_TAG,
message: `Tag 创建成功: ${currentTag}`,
timestamp: new Date(),
data: { tagName: currentTag },
});
}
if (!currentTag) {
throw new Error("Tag 名称不能为空");
}
/** 步骤 2: 推送 Tag */
if (createNewTag) {
this.reportProgress(onProgress, {
step: DeployStep.PUSHING_TAG,
message: `正在推送 Tag: ${currentTag}...`,
timestamp: new Date(),
});
await this.pushTag(currentTag);
this.reportProgress(onProgress, {
step: DeployStep.PUSHING_TAG,
message: `Tag 推送成功: ${currentTag}`,
timestamp: new Date(),
});
}
/** 步骤 3: 监听 Bitbucket Pipeline */
this.reportProgress(onProgress, {
step: DeployStep.MONITORING_PIPELINE,
message: `正在监听 Pipeline: ${currentTag}...`,
timestamp: new Date(),
});
const pipelineMonitor = new pipeline_monitor_1.PipelineMonitor(this.bitbucketClient);
const pipelineResult = await pipelineMonitor.monitorPipeline(this.projectConfig.repository.workspace, this.projectConfig.repository.repoSlug, currentTag, {
pollInterval: 15000,
timeout: 30 * 60 * 1000,
onProgress: (status) => {
this.reportProgress(onProgress, {
step: DeployStep.MONITORING_PIPELINE,
message: `Pipeline 状态: ${status.state.name}`,
timestamp: new Date(),
data: { pipelineStatus: status.state.name },
});
},
});
this.reportProgress(onProgress, {
step: DeployStep.MONITORING_PIPELINE,
message: `Pipeline 执行成功: ${pipelineResult.state.name}`,
timestamp: new Date(),
data: { pipelineStatus: pipelineResult.state.name },
});
/** 步骤 4: 触发 Jenkins 部署 */
this.reportProgress(onProgress, {
step: DeployStep.DEPLOYING_JENKINS,
message: `正在触发 Jenkins 部署: ${this.environmentConfig.jenkinsJobName}...`,
timestamp: new Date(),
});
const jenkinsDeployer = new jenkins_deployer_1.JenkinsDeployer(this.jenkinsClient);
const jenkinsResult = await jenkinsDeployer.deploy(this.environmentConfig.jenkinsJobName, {
[this.environmentConfig.jenkinsTagParameter]: currentTag,
}, {
pollInterval: 10000,
timeout: 30 * 60 * 1000,
onProgress: (build) => {
this.reportProgress(onProgress, {
step: DeployStep.DEPLOYING_JENKINS,
message: `Jenkins 构建 #${build.number}: ${build.result || "BUILDING"}`,
timestamp: new Date(),
data: {
buildNumber: build.number,
buildResult: build.result,
},
});
},
});
this.reportProgress(onProgress, {
step: DeployStep.COMPLETED,
message: `部署完成!`,
timestamp: new Date(),
data: {
buildNumber: jenkinsResult.number,
buildResult: jenkinsResult.result,
buildUrl: jenkinsResult.url,
},
});
return {
success: true,
tagName: currentTag,
pipelineStatus: pipelineResult.state.name,
jenkinsResult: jenkinsResult.result || "SUCCESS",
jenkinsBuildNumber: jenkinsResult.number,
jenkinsBuildUrl: jenkinsResult.url,
};
}
catch (error) {
this.reportProgress(onProgress, {
step: DeployStep.FAILED,
message: `部署失败: ${error.message}`,
timestamp: new Date(),
data: { error: error.message },
});
return {
success: false,
tagName: currentTag || "",
error: error.message,
};
}
}
/**
* 创建 Git Tag
*/
async createTag() {
const git = simpleGit.simpleGit(this.projectConfig.path);
/** 获取所有 tags */
const tags = await git.tags();
/** 解析 tag 格式 */
const tagFormat = (0, tag_1.parseTagFormat)(this.environmentConfig.tagFormat);
/** 生成新的 tag */
const newTag = (0, tag_1.generateNextTag)(tags.all, tagFormat);
/** 创建 tag */
await git.addTag(newTag);
return newTag;
}
/**
* 推送 Tag 到远程仓库
*/
async pushTag(tagName) {
const git = simpleGit.simpleGit(this.projectConfig.path);
await git.pushTags("origin");
}
/**
* 报告进度
*/
reportProgress(callback, progress) {
console.log(`[${progress.step}] ${progress.message}`);
if (callback) {
callback(progress);
}
}
}
exports.FullDeployer = FullDeployer;
//# sourceMappingURL=full-deployer.js.map