@taole/deploy-helper 0.3.3 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.mjs CHANGED
@@ -5,7 +5,7 @@ import fs from 'fs';
5
5
  import { join, basename, dirname } from "path";
6
6
  import { simpleGit } from 'simple-git';
7
7
  import { homedir } from 'os'
8
- import { runPipeline, checkYunxiaoToken } from './lib/pipelineApi.mjs';
8
+ import { runPipeline, checkYunxiaoToken, triggerPipeline } from './lib/pipelineApi.mjs';
9
9
 
10
10
  const TEST_SERVER_HOST = "192.168.0.35";
11
11
  /**
@@ -89,7 +89,7 @@ async function main() {
89
89
  // return;
90
90
  // other commands
91
91
  let command = process.argv[2];
92
- if (!["init", "prod", "test", "scp", "scpevt"].includes(command)) {
92
+ if (!["init", "prod", "test", "scp", "scpevt", "pipeline"].includes(command)) {
93
93
  command = "help";
94
94
  }
95
95
 
@@ -103,6 +103,7 @@ async function main() {
103
103
  console.log(`command: scp {file} {dest} 复制文件{file}到OfficialSite测试服务器{dest}`);
104
104
  console.log(`command: scpevt {file} 复制文件{file}到events测试服务器{file}`);
105
105
  console.log(`command: scpevt {file} {dest} 复制文件{file}到events测试服务器{dest}`);
106
+ console.log(`command: pipeline {pipelineName|pipelineId} 触发流水线{pipelineName|pipelineId}`);
106
107
  process.exit(0);
107
108
  } else if (command === "init") {
108
109
  await initConfigJson();
@@ -145,6 +146,20 @@ async function main() {
145
146
  await scpClient.uploadFile(srcFilePath, destPath);
146
147
  console.log(`scp done.`);
147
148
  process.exit(0);
149
+ } else if (command === "pipeline") {
150
+ const pipelineName = process.argv[3];
151
+ if (!pipelineName) {
152
+ console.log(`pipeline参数不能为空`);
153
+ process.exit(1);
154
+ }
155
+ try {
156
+ await triggerPipeline(pipelineName);
157
+ process.exit(0);
158
+ } catch (error) {
159
+ console.log(`触发流水线失败: ${error}`);
160
+ process.exit(1);
161
+ }
162
+
148
163
  }
149
164
  try {
150
165
  const workDir = process.cwd(); // 当前项目根目录
@@ -3,7 +3,7 @@ import { join } from 'node:path';
3
3
  import fs from 'node:fs';
4
4
  import simpleGit from 'simple-git';
5
5
 
6
- import { createPipelineRunFunc, getLatestPipelineRunFunc, listPipelinesFunc } from '../modules/alibabacloud-devops-mcp-server/dist/operations/flow/pipeline.js'
6
+ import { createPipelineRunFunc, listPipelinesFunc, getPipelineFunc } from '../modules/alibabacloud-devops-mcp-server/dist/operations/flow/pipeline.js'
7
7
 
8
8
  export const organizationId = "5ec8bb7bd1d1abe63b55cd33";
9
9
 
@@ -78,19 +78,39 @@ export function setDevToken(token) {
78
78
  async function getPipelineInfoByName(name) {
79
79
  let pipeline = null;
80
80
  try {
81
- const pipelines = await listPipelinesFunc(organizationId, { pipelineName: name, perPage: 1, page: 1 });
81
+ const pipelines = await listPipelinesFunc(organizationId, { pipelineName: name, perPage: 50, page: 1 });
82
82
  if (pipelines && Array.isArray(pipelines.items) && pipelines.items.length > 0) {
83
- pipeline = pipelines.items[0];
83
+ pipeline = pipelines.items.find(item => item.name === name);
84
84
  }
85
85
  } catch (error) {
86
- throw new Error(`获取流水线信息失败, 请确认流水线名称: ${name} 是否正确或检查云效token的权限是否足够: ${error}`);
86
+ throw new Error(`获取流水线信息失败, 请确认流水线名称:【${name}】是否正确或检查云效token的权限是否足够: ${error}`);
87
87
  }
88
88
  if (!pipeline) {
89
- throw new Error(`未找到对应的流水线, 请确认流水线名称: ${name} 是否正确或检查云效token的权限是否足够`);
89
+ throw new Error(`未找到对应的流水线, 请确认流水线名称:【${name}】是否正确或检查云效token的权限是否足够`);
90
90
  }
91
91
  return pipeline;
92
92
  }
93
93
 
94
+ export async function triggerPipeline(pipelineName) {
95
+ const yunxiaoToken = getYunxiaoToken({});
96
+ if(!yunxiaoToken) {
97
+ throw new Error(`未设置云效token, 请检查云效token是否正确`);
98
+ }
99
+ let pipelineID = "";
100
+ let name = pipelineName;
101
+ if(/^\d+$/.test(pipelineName)) {
102
+ pipelineID = pipelineName;
103
+ const pipelineInfo = await getPipelineFunc(organizationId, pipelineID);
104
+ name = pipelineInfo.name;
105
+ } else {
106
+ const pipelineInfo = await getPipelineInfoByName(pipelineName);
107
+ pipelineID = pipelineInfo.id;
108
+ name = pipelineInfo.name;
109
+ }
110
+ const runId = await createPipelineRunFunc(organizationId, pipelineID, {});
111
+ const pipelineRunDetailUrl = `https://flow.aliyun.com/pipelines/${pipelineID}/builds/${runId}`;
112
+ console.log(`流水线${name}[${pipelineID}]触发成功,流水线执行id: ${runId}, 流水线执行详情: ${pipelineRunDetailUrl}`);
113
+ }
94
114
 
95
115
 
96
116
  /**
@@ -126,9 +146,13 @@ export function checkYunxiaoToken(config, mode) {
126
146
 
127
147
  async function runSinglePipeline(pipeline, index, total) {
128
148
  console.log(`开始处理流水线: ${pipeline.name}, 当前是第${index + 1}个, 总共${total}个`);
129
- // 获取流水线信息
130
- const pipelineInfo = await getPipelineInfoByName(pipeline.name);
131
- console.log(`流水线${pipeline.name}的id: ${pipelineInfo.id}`);
149
+ let pipelineId = pipeline.id || "";
150
+ if (!pipelineId) {
151
+ // 获取流水线信息
152
+ const pipelineInfo = await getPipelineInfoByName(pipeline.name);
153
+ pipelineId = pipelineInfo.id;
154
+ }
155
+ console.log(`流水线${pipeline.name}的id: ${pipelineId}`);
132
156
 
133
157
  // 处理分支强推的问题
134
158
  const force2Branch = pipeline.force2Branch;
@@ -159,9 +183,9 @@ async function runSinglePipeline(pipeline, index, total) {
159
183
  }
160
184
  // TODO: PERF: 获取流水线信息后, 可以缓存下来, 避免每次都重新获取
161
185
  // TODO: 触发流水线时, 可以传入参数, 比如分支, 比如环境, 但是不知道为什么覆盖不了。这里先不传了
162
- const runId = await createPipelineRunFunc(organizationId, pipelineInfo.id, {});
163
- const piplineRunDetailUrl = `https://flow.aliyun.com/pipelines/${pipelineInfo.id}/builds/${runId}`;
164
- console.log(`流水线${pipelineInfo.name}触发成功,流水线执行id: ${runId}, 流水线执行详情: ${piplineRunDetailUrl}`);
186
+ const runId = await createPipelineRunFunc(organizationId, pipelineId, {});
187
+ const piplineRunDetailUrl = `https://flow.aliyun.com/pipelines/${pipelineId}/builds/${runId}`;
188
+ console.log(`流水线${pipeline.name}触发成功,流水线执行id: ${runId}, 流水线执行详情: ${piplineRunDetailUrl}`);
165
189
  }
166
190
 
167
191
  export async function runPipeline(config, mode) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/deploy-helper",
3
- "version": "0.3.3",
3
+ "version": "0.3.5",
4
4
  "description": "脚本部署工具,用于将项目部署到测试环境或生产环境",
5
5
  "main": "index.mjs",
6
6
  "type": "module",