Pipeline.vue 21 KB

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