Pipeline.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <template>
  2. <ElTabs class="tabs" v-model="activeTab">
  3. <ElTabPane label="流水线" name="pipeline">
  4. <ElTable :data="pipelines" width="100%">
  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) => timeFormatter(row, 'startTime')"
  27. ></ElTableColumn>
  28. <ElTableColumn
  29. prop="result"
  30. label="执行状态"
  31. align="center"
  32. width="100"
  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>
  50. <ElTag type="warning">未知状态</ElTag>
  51. </template>
  52. </template>
  53. </ElTableColumn>
  54. <ElTableColumn
  55. label="详情"
  56. align="center"
  57. >
  58. <template #default="scope">
  59. <ElButton
  60. @click="handleClick('detail', scope.row.id)"
  61. type="text"
  62. size="small"
  63. :disabled="scope.row.result==='执行中'"
  64. >最近执行详情</ElButton>
  65. <ElButton @click="handleClick('history', scope.row.id)" type="text" size="small">历史</ElButton>
  66. </template>
  67. </ElTableColumn>
  68. <ElTableColumn
  69. align="center"
  70. >
  71. <template #header>
  72. <span>操作</span>
  73. <ElButton @click="handleClick('create')" type="text" class="add-button">新建</ElButton>
  74. </template>
  75. <template #default="scope">
  76. <ElButton @click="handleClick('trigger', scope.row.id)" type="text" size="small">触发</ElButton>
  77. <ElButton @click="handleClick('edit', scope.row.id)" type="text" size="small">编辑</ElButton>
  78. <ElButton @click="handleClick('delete', scope.row.id)" type="text" size="small">删除</ElButton>
  79. </template>
  80. </ElTableColumn>
  81. </ElTable>
  82. <ElDialog v-model="showPipelineDetailModal" destroy-on-close>
  83. <template #title>最近一次流水线构建的执行结果详情</template>
  84. <pre class="pipeline-detail">{{detail}}</pre>
  85. </ElDialog>
  86. <ElDialog v-model="showPipelineHistoryModal" destroy-on-close>
  87. <template #title>流水线构建历史</template>
  88. <ElTable
  89. :data="pipelineHistory"
  90. height="400"
  91. >
  92. <ElTableColumn type="expand">
  93. <template #default="scope">
  94. <pre class="pipeline-detail">{{scope.row.details}}</pre>
  95. </template>
  96. </ElTableColumn>
  97. <ElTableColumn
  98. prop="id"
  99. label="构建ID"
  100. align="center"
  101. width="100"
  102. ></ElTableColumn>
  103. <ElTableColumn
  104. prop="pipelineId"
  105. label="流水线ID"
  106. align="center"
  107. width="100"
  108. ></ElTableColumn>
  109. <ElTableColumn
  110. prop="username"
  111. label="触发人"
  112. align="center"
  113. width="180"
  114. ></ElTableColumn>
  115. <ElTableColumn
  116. label="执行时间"
  117. align="center"
  118. :formatter="(row) => timeFormatter(row, 'startTime')"
  119. ></ElTableColumn>
  120. </ElTable>
  121. </ElDialog>
  122. <ElDialog
  123. v-model="showPipelineEditModal"
  124. :before-close="(done) => {
  125. jsonValid=true;
  126. done();
  127. }"
  128. destroy-on-close
  129. >
  130. <template #title>
  131. <span>编辑流水线配置</span>
  132. <span class="button-group">
  133. <ElButton type="text">脚本教程</ElButton>
  134. </span>
  135. </template>
  136. <ElInput
  137. type="textarea"
  138. :rows="16"
  139. placeholder="请输入流水线配置"
  140. @input="lintJson"
  141. v-model="pipelineConfig">
  142. ></ElInput>
  143. <pre v-if="!jsonValid" :innerText="hint"></pre>
  144. <template #footer>
  145. <span class="dialog-footer">
  146. <el-button type="primary" @click="handlePipelineUpdateConfirm" :disabled="!jsonValid">确 定</el-button>
  147. </span>
  148. </template>
  149. </ElDialog>
  150. <ElDialog
  151. v-model="showPipelineCreateModal"
  152. :before-close="(done) => {
  153. $refs['createPipelineForm'].resetFields();
  154. done();
  155. }"
  156. destroy-on-close
  157. >
  158. <template #title>
  159. <span>创建流水线</span>
  160. <span class="button-group">
  161. <ElButton type="text">模板详解</ElButton>
  162. </span>
  163. </template>
  164. <ElForm
  165. :model="createPipelineForm"
  166. ref="createPipelineForm"
  167. label-position="top"
  168. label-suffix=":"
  169. class="create-pipeline-form"
  170. >
  171. <ElFormItem label="名称" prop="name" required>
  172. <ElInput v-model="createPipelineForm.name" placeholder="请输入流水线名称"></ElInput>
  173. </ElFormItem>
  174. <ElFormItem label="模板" prop="template" required>
  175. <ElSelect v-model="createPipelineForm.template" placeholder="请选择模板">
  176. <ElOption
  177. v-for="(template, index) in templates"
  178. :key="index" :label="template"
  179. :value="template"
  180. ></ElOption>
  181. </ElSelect>
  182. </ElFormItem>
  183. </ElForm>
  184. <template #footer>
  185. <span class="dialog-footer">
  186. <el-button type="primary" @click="handlePipelineCreateConfirm">确 定</el-button>
  187. </span>
  188. </template>
  189. </ElDialog>
  190. </ElTabPane>
  191. <ElTabPane label="部署产物" name="production">
  192. <ElTable :data="productions" width="100%">
  193. <ElTableColumn
  194. prop="id"
  195. label="ID"
  196. align="center"
  197. width="60"
  198. ></ElTableColumn>
  199. <ElTableColumn
  200. prop="name"
  201. label="名称"
  202. align="center"
  203. ></ElTableColumn>
  204. <ElTableColumn
  205. prop="pipelineId"
  206. label="流水线ID"
  207. align="center"
  208. width="100"
  209. ></ElTableColumn>
  210. <ElTableColumn
  211. label="部署时间"
  212. align="center"
  213. :formatter="(row) => timeFormatter(row, 'deployedTime')"
  214. ></ElTableColumn>
  215. <ElTableColumn
  216. label="URL"
  217. align="center"
  218. >
  219. <template #default="scope">
  220. <ElButton type="text" @click="open(scope.row.accessUrl, '_blank')">
  221. {{scope.row.accessUrl}}
  222. </ElButton>
  223. </template>
  224. </ElTableColumn>
  225. <ElTableColumn
  226. label="运行状态"
  227. align="center"
  228. >
  229. <template #default="scope">
  230. <template v-if="scope.row.status === '正常'">
  231. <ElTag type="success">{{scope.row.status}}</ElTag>
  232. </template>
  233. <template v-else>
  234. <ElTag type="danger">{{scope.row.status}}</ElTag>
  235. </template>
  236. </template>
  237. </ElTableColumn>
  238. <ElTableColumn>
  239. <template #default="scope">
  240. <ElButton type="text" @click="deleteProduction(scope.row.id)">
  241. <i class="el-icon-delete"></i>
  242. </ElButton>
  243. </template>
  244. </ElTableColumn>
  245. </ElTable>
  246. </ElTabPane>
  247. </ElTabs>
  248. </template>
  249. <script>
  250. import jsonlint from '@/utils/jsonlint';
  251. import { PipelineAPI } from '@/api';
  252. import debounce from '@/utils/debounce';
  253. export default {
  254. data() {
  255. return {
  256. activeTab: 'pipeline',
  257. pipelines: [],
  258. productions: [],
  259. showPipelineCreateModal: false,
  260. showPipelineEditModal: false,
  261. showPipelineHistoryModal: false,
  262. showPipelineDetailModal: false,
  263. detail: '',
  264. pipelineHistory: [],
  265. pipelineConfig: '',
  266. jsonValid: true,
  267. editingPipelineId: -1,
  268. templates: [],
  269. createPipelineForm: {
  270. name: '',
  271. template: '',
  272. },
  273. };
  274. },
  275. async mounted() {
  276. this.pipelines = await PipelineAPI.getPipelinesByProjectId(this.$store.state.workspace.currentProjectId);
  277. this.productions = await PipelineAPI.getDeploymentsByProjectId(this.$store.state.workspace.currentProjectId);
  278. this.templates = await PipelineAPI.getPipelineTemplates();
  279. },
  280. methods: {
  281. timeFormatter: (row, property) => `${row[property].year}/${row[property].month}/${row[property].day} `
  282. + `${row[property].hours}:${row[property].minutes}:${row[property].seconds}`,
  283. async handleClick(action, id) {
  284. switch (action) {
  285. case 'create': {
  286. this.showPipelineCreateModal = true;
  287. break;
  288. }
  289. case 'trigger': {
  290. await PipelineAPI.triggerDeployment({
  291. projectId: this.$store.state.workspace.currentProjectId,
  292. pipelineId: id,
  293. });
  294. this.productions = await PipelineAPI.getDeploymentsByProjectId(this.$store.state.workspace.currentProjectId);
  295. break;
  296. }
  297. case 'detail': {
  298. this.showPipelineDetailModal = true;
  299. this.detail = Array.prototype.find.call(this.pipelines, (pipeline) => pipeline.id === id).details;
  300. break;
  301. }
  302. case 'history': {
  303. this.showPipelineHistoryModal = true;
  304. this.pipelineHistory = await PipelineAPI.getPipelineHistory({
  305. projectId: this.$store.state.workspace.currentProjectId,
  306. pipelineId: id,
  307. });
  308. break;
  309. }
  310. case 'edit': {
  311. this.showPipelineEditModal = true;
  312. this.editingPipelineId = id;
  313. this.pipelineConfig = await PipelineAPI.getPipeline({
  314. projectId: this.$store.state.workspace.currentProjectId,
  315. pipelineId: id,
  316. }).configJson || '{}';
  317. break;
  318. }
  319. case 'delete': {
  320. PipelineAPI.deletePipeline({
  321. projectId: this.$store.state.workspace.currentProjectId,
  322. pipelineId: id,
  323. });
  324. break;
  325. }
  326. default: {
  327. console.warn('Unknown type of action.');
  328. }
  329. }
  330. },
  331. // eslint-disable-next-line func-names
  332. lintJson: debounce(function (value) {
  333. this.jsonValid = true;
  334. try {
  335. const result = jsonlint.parse(value);
  336. this.pipelineConfig = JSON.stringify(result, null, ' ');
  337. } catch (e) {
  338. this.jsonValid = false;
  339. this.hint = `${value}\n${e}`;
  340. }
  341. }, 500),
  342. async handlePipelineUpdateConfirm() {
  343. await PipelineAPI.updatePipeline({
  344. projectId: this.$store.state.workspace.currentProjectId,
  345. pipelineId: this.editingPipelineId,
  346. configJson: JSON.stringify(JSON.parse(this.pipelineConfig)),
  347. });
  348. this.showPipelineEditModal = false;
  349. },
  350. async handlePipelineCreateConfirm() {
  351. this.$refs.createPipelineForm.validate(async (valid) => {
  352. if (valid) {
  353. await PipelineAPI.createPipeline({
  354. projectId: this.$store.state.workspace.currentProjectId,
  355. pipelineId: this.editingPipelineId,
  356. name: this.createPipelineForm.name,
  357. templateName: this.createPipelineForm.template,
  358. });
  359. this.showPipelineCreateModal = false;
  360. }
  361. });
  362. },
  363. open(url, target) {
  364. window.open(url, target);
  365. },
  366. async deleteProduction(id) {
  367. await PipelineAPI.deleteInstance(id);
  368. },
  369. },
  370. };
  371. </script>
  372. <style scoped>
  373. .tabs {
  374. width: 80%;
  375. margin: auto auto;
  376. }
  377. .add-button {
  378. margin-left: 14px;
  379. }
  380. .pipeline-detail {
  381. width: 100%;
  382. word-break: break-word;
  383. white-space: pre-wrap;
  384. }
  385. .create-pipeline-form {
  386. width: 60%;
  387. margin: 0 auto;
  388. }
  389. .button-group {
  390. margin-left: 40px;
  391. }
  392. </style>