Pipeline.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. <template>
  2. <ElTabs class="tabs" v-model="activeTab" v-loading.lock="loading">
  3. <ElTabPane label="流水线" name="pipeline">
  4. <ElTable :data="pipelines" width="100%" :default-sort="{prop: 'id', order: 'descending'}">
  5. <ElTableColumn
  6. prop="id"
  7. label="ID"
  8. align="center"
  9. width="60"
  10. ></ElTableColumn>
  11. <ElTableColumn
  12. prop="name"
  13. label="名称"
  14. align="center"
  15. width="180"
  16. ></ElTableColumn>
  17. <ElTableColumn
  18. prop="username"
  19. label="最近执行人"
  20. align="center"
  21. width="180"
  22. ></ElTableColumn>
  23. <ElTableColumn
  24. label="最近执行时间"
  25. align="center"
  26. :formatter="(row) => formatter(row.startTime)"
  27. ></ElTableColumn>
  28. <ElTableColumn
  29. prop="result"
  30. label="执行状态"
  31. align="center"
  32. width="110"
  33. :filters="[{ text: '成功', value: '成功' }, { text: '失败', value: '失败' }]"
  34. :filter-method="(value, row) => {
  35. return row.result === value;
  36. }"
  37. filter-placement="bottom-end"
  38. >
  39. <template #default="scope">
  40. <template v-if="scope.row.result === '成功'">
  41. <ElTag type="success">{{scope.row.result}}</ElTag>
  42. </template>
  43. <template v-else-if="scope.row.result === '失败'">
  44. <ElTag type="danger">{{scope.row.result}}</ElTag>
  45. </template>
  46. <template v-else-if="scope.row.result === '执行中'">
  47. <ElTag>{{scope.row.result}}<i class="el-icon-loading"></i></ElTag>
  48. </template>
  49. <template v-else-if="scope.row.result === '无执行记录'">
  50. <ElTag>{{scope.row.result}}</ElTag>
  51. </template>
  52. <template v-else>
  53. <ElTag type="warning">未知状态</ElTag>
  54. </template>
  55. </template>
  56. </ElTableColumn>
  57. <ElTableColumn
  58. label="详情"
  59. align="center"
  60. >
  61. <template #default="scope">
  62. <ElButton
  63. @click="handleClick('detail', scope.row.recordId)"
  64. type="text"
  65. size="small"
  66. :disabled="scope.row.result==='执行中'"
  67. >最近执行详情</ElButton>
  68. <ElButton @click="handleClick('history', scope.row.id)" type="text" size="small">历史</ElButton>
  69. </template>
  70. </ElTableColumn>
  71. <ElTableColumn
  72. align="center"
  73. >
  74. <template #header>
  75. <span>操作</span>
  76. <ElButton @click="handleClick('create')" type="text" class="add-button">新建</ElButton>
  77. </template>
  78. <template #default="scope">
  79. <ElButton @click="handleClick('trigger', scope.row.id)" type="text" size="small">触发</ElButton>
  80. <ElButton @click="handleClick('edit', scope.row.id)" type="text" size="small">编辑</ElButton>
  81. <ElButton @click="handleClick('delete', scope.row.id)" type="text" size="small">删除</ElButton>
  82. </template>
  83. </ElTableColumn>
  84. </ElTable>
  85. <ElDialog v-model="showPipelineDetailModal" destroy-on-close>
  86. <template #title>历史执行结果详情</template>
  87. <pre class="log" v-loading="dialogLoading" >{{detail}}</pre>
  88. </ElDialog>
  89. <ElDialog v-model="showPipelineHistoryModal" destroy-on-close>
  90. <template #title>流水线构建历史</template>
  91. <ElTable
  92. :data="pipelineHistory"
  93. v-loading="dialogLoading"
  94. height="400"
  95. >
  96. <ElTableColumn
  97. prop="id"
  98. label="构建ID"
  99. align="center"
  100. width="100"
  101. ></ElTableColumn>
  102. <ElTableColumn
  103. prop="pipelineId"
  104. label="流水线ID"
  105. align="center"
  106. width="100"
  107. ></ElTableColumn>
  108. <ElTableColumn
  109. prop="username"
  110. label="触发人"
  111. align="center"
  112. width="180"
  113. ></ElTableColumn>
  114. <ElTableColumn
  115. label="执行时间"
  116. align="center"
  117. :formatter="(row) => formatter(row.startTime)"
  118. ></ElTableColumn>
  119. <ElTableColumn
  120. prop="result"
  121. label="执行状态"
  122. align="center"
  123. width="100"
  124. :filters="[{ text: '成功', value: '成功' }, { text: '失败', value: '失败' }]"
  125. :filter-method="(value, row) => {
  126. return row.result === value;
  127. }"
  128. filter-placement="bottom-end"
  129. >
  130. <template #default="scope">
  131. <template v-if="scope.row.result === '成功'">
  132. <ElTag type="success">{{scope.row.result}}</ElTag>
  133. </template>
  134. <template v-else-if="scope.row.result === '失败'">
  135. <ElTag type="danger">{{scope.row.result}}</ElTag>
  136. </template>
  137. <template v-else-if="scope.row.result === '执行中'">
  138. <ElTag>{{scope.row.result}}<i class="el-icon-loading"></i></ElTag>
  139. </template>
  140. <template v-else-if="scope.row.result === '无执行记录'">
  141. <ElTag>{{scope.row.result}}</ElTag>
  142. </template>
  143. <template v-else>
  144. <ElTag type="warning">未知状态</ElTag>
  145. </template>
  146. </template>
  147. </ElTableColumn>
  148. <ElTableColumn>
  149. <template #default="scope">
  150. <ElButton @click="handleClick('detail', scope.row.id)" type="text" size="small">查看详情</ElButton>
  151. </template>
  152. </ElTableColumn>
  153. </ElTable>
  154. </ElDialog>
  155. <ElDialog
  156. v-model="showPipelineEditModal"
  157. width="80%"
  158. destroy-on-close
  159. >
  160. <template #title>
  161. <span>编辑流水线配置</span>
  162. </template>
  163. <ElSteps :active="activeStep" v-loading="dialogLoading">
  164. <ElStep
  165. v-for="(config, index) in pipelineConfig"
  166. :key="index"
  167. class="step"
  168. @click="() => activeStep = index"
  169. >
  170. <template #title>
  171. <span>{{getStage(config.name).title}}</span>
  172. <ElTooltip
  173. class="tooltip"
  174. effect="dark"
  175. :content="getStage(config.name).description"
  176. placement="right-end"
  177. >
  178. <i class="el-icon-question"></i>
  179. </ElTooltip>
  180. </template>
  181. </ElStep>
  182. </ElSteps>
  183. <div class="edit-pipeline-form" v-for="(config, index) in pipelineConfig" :key="index" v-show="activeStep === index">
  184. <ElForm label-width="160px">
  185. <div style="margin: 20px 0 20px 160px;">
  186. <ElCheckbox
  187. :disabled="!getStage(config.name).skippable"
  188. v-model="config.active"
  189. >启用该阶段</ElCheckbox>
  190. </div>
  191. <template v-for="(val, key) in config.configs" :key="key" placeholder="请选择">
  192. <ElFormItem :label="key">
  193. <ElSelect v-if="key === 'apiTestIds'" v-model="config.configs[key]" multiple >
  194. <el-option
  195. v-for="item in apiTestOptions"
  196. :key="item.testId"
  197. :label="item.testName"
  198. :value="item.testId">
  199. </el-option>
  200. </ElSelect>
  201. <ElInput v-else
  202. :disabled="!getStage(config.name).configs.find((config) => config.name === key).editable"
  203. v-model="config.configs[key]"
  204. ></ElInput>
  205. </ElFormItem>
  206. </template>
  207. </ElForm>
  208. </div>
  209. <template #footer>
  210. <ElButton :disabled="activeStep == 0" @click="() => activeStep -= 1">上一步</ElButton>
  211. <ElButton :disabled="isLastStep" @click="() => activeStep += 1">下一步</ElButton>
  212. <ElButton type="primary" @click="handlePipelineUpdateConfirm">确定</ElButton>
  213. </template>
  214. </ElDialog>
  215. <ElDialog
  216. v-model="showPipelineCreateModal"
  217. :before-close="(done) => {
  218. $refs['createPipelineForm'].resetFields();
  219. done();
  220. }"
  221. destroy-on-close
  222. >
  223. <template #title>
  224. <span>创建流水线</span>
  225. <span class="button-group">
  226. <ElButton type="text">模板详解</ElButton>
  227. </span>
  228. </template>
  229. <ElForm
  230. :model="createPipelineForm"
  231. ref="createPipelineForm"
  232. label-position="top"
  233. label-suffix=":"
  234. class="create-pipeline-form"
  235. v-loading="dialogLoading"
  236. >
  237. <ElFormItem label="名称" prop="name" required>
  238. <ElInput v-model="createPipelineForm.name" placeholder="请输入流水线名称"></ElInput>
  239. </ElFormItem>
  240. <ElFormItem label="模板" prop="template" required>
  241. <ElSelect v-model="createPipelineForm.template" placeholder="请选择模板">
  242. <ElOption
  243. v-for="(template, index) in templates"
  244. :key="index" :label="template"
  245. :value="template"
  246. ></ElOption>
  247. </ElSelect>
  248. </ElFormItem>
  249. </ElForm>
  250. <template #footer>
  251. <span class="dialog-footer">
  252. <el-button type="primary" v-loading="confirmLoading" @click="handlePipelineCreateConfirm">确 定</el-button>
  253. </span>
  254. </template>
  255. </ElDialog>
  256. </ElTabPane>
  257. <ElTabPane label="部署产物" name="production">
  258. <ElTable :data="productions" width="100%" :default-sort="{prop: 'id', order: 'descending'}">
  259. <ElTableColumn
  260. prop="id"
  261. label="ID"
  262. align="center"
  263. width="60"
  264. ></ElTableColumn>
  265. <ElTableColumn
  266. prop="name"
  267. label="名称"
  268. align="center"
  269. width="100"
  270. ></ElTableColumn>
  271. <ElTableColumn
  272. prop="pipelineId"
  273. label="流水线ID"
  274. align="center"
  275. width="60"
  276. ></ElTableColumn>
  277. <ElTableColumn
  278. label="部署时间"
  279. align="center"
  280. width="320"
  281. :formatter="(row) => formatter(row.deployedTime)"
  282. ></ElTableColumn>
  283. <ElTableColumn
  284. label="URL"
  285. align="center"
  286. >
  287. <template #default="scope">
  288. <ElTooltip effect="dark" :content="scope.row.accessUrl" placement="top-start">
  289. <ElButton type="text" @click="open(scope.row.accessUrl, '_blank')">
  290. {{scope.row.accessUrl}}
  291. </ElButton>
  292. </ElTooltip>
  293. </template>
  294. </ElTableColumn>
  295. <ElTableColumn
  296. label="运行状态"
  297. align="center"
  298. width="100"
  299. >
  300. <template #default="scope">
  301. <ElTag :type="scope.row.status.indexOf('正常') > -1 ? 'success' : 'danger'">{{scope.row.status}}</ElTag>
  302. </template>
  303. </ElTableColumn>
  304. <ElTableColumn
  305. label="运行状态"
  306. align="center"
  307. width="100"
  308. >
  309. <template #default="scope">
  310. <ElButton type="text" @click="showLog(scope.row.pipelineId)">查看日志</ElButton>
  311. </template>
  312. </ElTableColumn>
  313. <ElTableColumn width="60">
  314. <template #default="scope">
  315. <ElButton type="text" @click="deleteProduction(scope.row.pipelineId)">
  316. <i class="el-icon-delete"></i>
  317. </ElButton>
  318. </template>
  319. </ElTableColumn>
  320. </ElTable>
  321. <ElDialog v-model="showProductionLogModal" destroy-on-close>
  322. <template #title>
  323. 产物日志
  324. </template>
  325. <pre class="log" v-loading="dialogLoading" >{{currentLog}}</pre>
  326. <template #footer>
  327. <ElButton @click="refreshLog">刷新</ElButton>
  328. </template>
  329. </ElDialog>
  330. </ElTabPane>
  331. </ElTabs>
  332. </template>
  333. <script>
  334. import { APITestAPI, PipelineAPI } from '@/api';
  335. import { formatter } from '@/utils/time';
  336. export default {
  337. data() {
  338. return {
  339. loading: false,
  340. dialogLoading: false,
  341. confirmLoading: false,
  342. activeTab: 'pipeline',
  343. pipelines: [],
  344. productions: [],
  345. showPipelineCreateModal: false,
  346. showPipelineEditModal: false,
  347. showPipelineHistoryModal: false,
  348. showPipelineDetailModal: false,
  349. detail: '',
  350. pipelineHistory: [],
  351. pipelineConfig: '',
  352. jsonValid: true,
  353. editingPipelineId: -1,
  354. templates: [],
  355. createPipelineForm: {
  356. name: '',
  357. template: '',
  358. },
  359. stages: [],
  360. activeStep: 0,
  361. showProductionLogModal: false,
  362. currentLogId: -1,
  363. currentLog: '',
  364. apiTestOptions: [],
  365. };
  366. },
  367. computed: {
  368. isLastStep() {
  369. return this.activeStep === this.stages.length - 1;
  370. },
  371. stageConfigsBase() {
  372. return this.stages.map((stage) => ({
  373. name: stage.name,
  374. active: true,
  375. configs: stage.configs.map(({ name, value, editable }) => ({
  376. [name]: value,
  377. editable,
  378. })).reduce((prev, curr) => Object.assign(prev, curr), {}),
  379. }));
  380. },
  381. stageConfigs() {
  382. return this.stageConfigsBase;
  383. },
  384. },
  385. async mounted() {
  386. this.loading = true;
  387. this.pipelines = await PipelineAPI.getPipelinesByProjectId(this.$store.state.workspace.currentProjectId);
  388. this.productions = await PipelineAPI.getDeploymentsByProjectId(this.$store.state.workspace.currentProjectId);
  389. this.templates = await PipelineAPI.getPipelineTemplates();
  390. this.stages = await PipelineAPI.getStages();
  391. this.apiTestOptions = await APITestAPI.getAPITestInfosByProjectId(this.$store.state.workspace.currentProjectId);
  392. this.loading = false;
  393. },
  394. methods: {
  395. formatter,
  396. async handleClick(action, id) {
  397. switch (action) {
  398. case 'create': {
  399. this.showPipelineCreateModal = true;
  400. break;
  401. }
  402. case 'trigger': {
  403. this.loading = true;
  404. await PipelineAPI.triggerDeployment({
  405. projectId: this.$store.state.workspace.currentProjectId,
  406. pipelineId: id,
  407. });
  408. PipelineAPI.getDeploymentsByProjectId(this.$store.state.workspace.currentProjectId)
  409. .then(
  410. (res) => {
  411. this.productions = res;
  412. },
  413. );
  414. this.loading = true;
  415. this.pipelines = await PipelineAPI.getPipelinesByProjectId(this.$store.state.workspace.currentProjectId);
  416. this.loading = false;
  417. break;
  418. }
  419. case 'detail': {
  420. this.showPipelineDetailModal = true;
  421. this.dialogLoading = true;
  422. this.detail = await PipelineAPI.getPipelineRecordDetail(id);
  423. this.dialogLoading = false;
  424. break;
  425. }
  426. case 'history': {
  427. this.showPipelineHistoryModal = true;
  428. this.dialogLoading = true;
  429. this.pipelineHistory = await PipelineAPI.getPipelineRecordList({
  430. projectId: this.$store.state.workspace.currentProjectId,
  431. pipelineId: id,
  432. });
  433. this.dialogLoading = false;
  434. break;
  435. }
  436. case 'edit': {
  437. this.showPipelineEditModal = true;
  438. this.dialogLoading = true;
  439. this.editingPipelineId = id;
  440. const config = (await PipelineAPI.getPipeline({
  441. projectId: this.$store.state.workspace.currentProjectId,
  442. pipelineId: id,
  443. })).configJson;
  444. this.pipelineConfig = JSON.parse(config || '{}');
  445. this.pipelineConfig.forEach((item) => {
  446. const targetStage = this.stages.find((stage) => stage.name === item.name);
  447. if (targetStage) {
  448. targetStage.configs.forEach((configTemplate) => {
  449. if (!item.configs[configTemplate.name]) {
  450. item.configs[configTemplate.name] = configTemplate.value;
  451. }
  452. });
  453. }
  454. });
  455. this.dialogLoading = false;
  456. break;
  457. }
  458. case 'delete': {
  459. this.loading = true;
  460. PipelineAPI.deletePipeline({
  461. projectId: this.$store.state.workspace.currentProjectId,
  462. pipelineId: id,
  463. });
  464. this.loading = false;
  465. break;
  466. }
  467. default: {
  468. console.warn('Unknown type of action.');
  469. }
  470. }
  471. },
  472. async handlePipelineUpdateConfirm() {
  473. this.confirmLoading = true;
  474. await PipelineAPI.updatePipeline({
  475. projectId: this.$store.state.workspace.currentProjectId,
  476. pipelineId: this.editingPipelineId,
  477. configJson: JSON.stringify(this.pipelineConfig),
  478. });
  479. this.confirmLoading = false;
  480. this.showPipelineEditModal = false;
  481. this.loading = true;
  482. this.pipelines = await PipelineAPI.getPipelinesByProjectId(this.$store.state.workspace.currentProjectId);
  483. this.loading = false;
  484. },
  485. async handlePipelineCreateConfirm() {
  486. this.confirmLoading = true;
  487. this.$refs.createPipelineForm.validate(async (valid) => {
  488. if (valid) {
  489. await PipelineAPI.createPipeline({
  490. projectId: this.$store.state.workspace.currentProjectId,
  491. name: this.createPipelineForm.name,
  492. templateName: this.createPipelineForm.template,
  493. });
  494. this.showPipelineCreateModal = false;
  495. }
  496. });
  497. this.confirmLoading = false;
  498. this.loading = true;
  499. this.pipelines = await PipelineAPI.getPipelinesByProjectId(this.$store.state.workspace.currentProjectId);
  500. this.loading = false;
  501. },
  502. open(url, target) {
  503. window.open(url, target);
  504. },
  505. async deleteProduction(pipelineId) {
  506. this.loading = true;
  507. await PipelineAPI.deleteInstance({
  508. projectId: this.$store.state.workspace.currentProjectId,
  509. pipelineId,
  510. });
  511. this.loading = false;
  512. },
  513. getStage(stageName) {
  514. return this.stages.find((stage) => stage.name === stageName);
  515. },
  516. async showLog(pipelineId) {
  517. this.currentLogId = pipelineId;
  518. this.showProductionLogModal = true;
  519. this.dialogLoading = true;
  520. this.currentLog = await PipelineAPI.getDeploymentLog({
  521. projectId: this.$store.state.workspace.currentProjectId,
  522. pipelineId,
  523. });
  524. this.dialogLoading = false;
  525. },
  526. async refreshLog() {
  527. this.dialogLoading = true;
  528. this.currentLog = await PipelineAPI.getDeploymentLog({
  529. projectId: this.$store.state.workspace.currentProjectId,
  530. pipelineId: this.currentLogId,
  531. });
  532. this.dialogLoading = false;
  533. },
  534. },
  535. };
  536. </script>
  537. <style scoped>
  538. .tabs {
  539. width: 80%;
  540. margin: auto auto;
  541. }
  542. .add-button {
  543. margin-left: 14px;
  544. }
  545. .log {
  546. width: 100%;
  547. word-break: break-word;
  548. white-space: pre-wrap;
  549. }
  550. .create-pipeline-form {
  551. width: 60%;
  552. margin: 0 auto;
  553. }
  554. .edit-pipeline-form {
  555. margin-top: 20px;
  556. }
  557. .button-group {
  558. margin-left: 40px;
  559. }
  560. .step {
  561. cursor: pointer;
  562. }
  563. </style>