UNPKG

@notadd/cli

Version:

notadd core none dependence

113 lines (112 loc) 7.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JenkinsCommand = void 0; const tslib_1 = require("tslib"); const cli_core_1 = require("@notadd/cli-core"); const shelljs_1 = tslib_1.__importDefault(require("shelljs")); const axios_1 = tslib_1.__importDefault(require("axios")); const core_1 = require("@notadd/core"); let JenkinsCommand = class JenkinsCommand { async build(serviceName, gitBranchName, gitTag, dockerUsername, dockerPassword, prefix, reqAddr, dingdingUrl, dingdingAtPeoples, qiyeWeChatUrl, weChatAtPeoples, dockerHost) { this.serviceName = serviceName; this.gitBranchName = gitBranchName; this.gitTag = gitTag; this.dockerUsername = dockerUsername; this.dockerPassword = dockerPassword; this.prefix = prefix; this.reqAddr = reqAddr; this.dingdingUrl = dingdingUrl; this.dingdingAtPeoples = dingdingAtPeoples; this.qiyeWeChatUrl = qiyeWeChatUrl; this.weChatAtPeoples = weChatAtPeoples; this.dockerHost = dockerHost; const SERVICE_NAME = this.serviceName; const GIT_BRANCH_NAME = this.gitBranchName; const GIT_TAG = this.gitTag; const DAOCLOUD_HOST = this.dockerHost; console.log(SERVICE_NAME, GIT_BRANCH_NAME, GIT_TAG); if (this.dockerBuild(SERVICE_NAME, GIT_BRANCH_NAME, GIT_TAG, DAOCLOUD_HOST) === false) { console.log("构建镜像失败"); await this.dingding(`[jenkins] 项目: ${SERVICE_NAME}${GIT_BRANCH_NAME} 构建镜像失败`, GIT_BRANCH_NAME); // await this.enterpriseWeChat(`[DEPLOY] ${SERVICE_NAME} JEEKINS ${GIT_BRANCH_NAME} 构建镜像失败`, GIT_BRANCH_NAME) return; } if (await this.reBoot(SERVICE_NAME, GIT_BRANCH_NAME) === false) { console.log("请求重启docker服务失败"); await this.dingding(`[jenkins] ${SERVICE_NAME} JEEKINS ${GIT_BRANCH_NAME} 远程重启服务失败`, GIT_BRANCH_NAME); // await this.enterpriseWeChat(`[DEPLOY] ${SERVICE_NAME} JEEKINS ${GIT_BRANCH_NAME} 远程重启服务失败`, GIT_BRANCH_NAME) return; } await this.dingding(`[jenkins]${SERVICE_NAME} JEEKINS ${GIT_BRANCH_NAME} 部署服务成功`, GIT_BRANCH_NAME); // await this.enterpriseWeChat(`[DEPLOY]${SERVICE_NAME} JEEKINS ${GIT_BRANCH_NAME} 部署服务成功`, GIT_BRANCH_NAME) console.log("服务部署成功"); } //构建镜像 dockerBuild(SERVICE_NAME, GIT_BRANCH_NAME, GIT_TAG, DAOCLOUD_HOST) { const DOCKER_IMAGE = ((this.prefix == null || this.prefix == undefined) ? `guanjie/` : (this.prefix + '/')) + SERVICE_NAME; const DOCKER_USERNAME = (this.dockerUsername == null || this.dockerUsername == undefined) ? 'sun_y' : this.dockerUsername; const DOCKER_PASSWORD = (this.dockerPassword == null || this.dockerPassword == undefined) ? '123456sy' : this.dockerPassword; let codeArr = []; shelljs_1.default.echo(` ###################### BUILD IMAGE NAME:${SERVICE_NAME} GIT_BRANCH: ${GIT_BRANCH_NAME} ######################`); shelljs_1.default.echo(` ###################### BUILD IMAGE NAME:${SERVICE_NAME} GIT_BRANCH: ${GIT_BRANCH_NAME} ######################`); console.log(`镜像名: ${DAOCLOUD_HOST}/${DOCKER_IMAGE}`); codeArr.push(shelljs_1.default.exec(` docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${DAOCLOUD_HOST}`).code); codeArr.push(shelljs_1.default.exec(` docker build --pull --no-cache -t ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_${GIT_TAG} --build-arg BASE_IMAGE=${GIT_BRANCH_NAME} .`).code); codeArr.push(shelljs_1.default.exec(` docker tag ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_${GIT_TAG} ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_latest`).code); codeArr.push(shelljs_1.default.exec(` docker push ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_${GIT_TAG}`).code); codeArr.push(shelljs_1.default.exec(` docker push ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_latest`).code); codeArr.push(shelljs_1.default.exec(` docker rmi ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_${GIT_TAG}`).code); codeArr.push(shelljs_1.default.exec(` docker rmi ${DAOCLOUD_HOST}/${DOCKER_IMAGE}:${GIT_BRANCH_NAME}_latest`).code); let code = codeArr.reduce((pre, next) => { return pre + next; }); console.log(`构建结果 ${code}`); return code === 0 ? true : false; } async reBoot(SERVICE, GIT_BRANCH_NAME) { const HOOK_PORT = GIT_BRANCH_NAME === 'dev' ? 50081 : 50082; const REQ_ADDR = GIT_BRANCH_NAME === 'dev' ? '10.0.1.24' : '10.0.1.25'; console.log(`reboot address: http://${REQ_ADDR}:${HOOK_PORT}`); console.log(`reboot service: ${SERVICE}`); const res = await axios_1.default.get(`http://${REQ_ADDR}:${HOOK_PORT}/docker/build-image/${SERVICE}`); console.log(`reboot response code: ${res.status}`); return res.status == 200 ? true : false; } async dingding(msg, GIT_BRANCH_NAME) { const URL = (this.dingdingUrl == null || this.dingdingUrl == undefined) ? 'https://oapi.dingtalk.com/robot/send?access_token=dac1c21968a2535816d71f646eb4c84aaded4115fe3390529bdaeefa29468124' : this.dingdingUrl; const ATPEOPLES = (this.dingdingAtPeoples == null || this.dingdingAtPeoples == undefined) ? `["18336315834"]` : this.dingdingAtPeoples; console.log("URL:" + URL); console.log("ATPEOPLES:" + ATPEOPLES); if (GIT_BRANCH_NAME == 'master') { const res = await axios_1.default.post(`${URL}`, `{"msgtype": "text", "text": {"content": "${msg}"},"at":{"atMobiles":${ATPEOPLES},"isAtAll":false}}`, { headers: { 'Content-Type': 'application/json' } }); console.log(res.data); return; } const res = await axios_1.default.post(`${URL}`, `{ "msgtype": "text", "text": { "content": "${msg}"}}`, { headers: { 'Content-Type': 'application/json' } }); console.log(res.data); return; } }; tslib_1.__decorate([ cli_core_1.Command('jenkins'), tslib_1.__param(0, core_1.Args(`sn`)), tslib_1.__param(1, core_1.Args(`gb`)), tslib_1.__param(2, core_1.Args(`gt`)), tslib_1.__param(3, core_1.Args(`du`)), tslib_1.__param(4, core_1.Args(`dp`)), tslib_1.__param(5, core_1.Args(`pf`)), tslib_1.__param(6, core_1.Args(`dd`)), tslib_1.__param(7, core_1.Args(`d1du`)), tslib_1.__param(8, core_1.Args(`ddp`)), tslib_1.__param(9, core_1.Args(`qyu`)), tslib_1.__param(10, core_1.Args(`qyp`)), tslib_1.__param(11, core_1.Args(`host`)), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", [String, String, String, String, String, String, String, String, String, String, String, String]), tslib_1.__metadata("design:returntype", Promise) ], JenkinsCommand.prototype, "build", null); JenkinsCommand = tslib_1.__decorate([ core_1.Controller() ], JenkinsCommand); exports.JenkinsCommand = JenkinsCommand; // new JenkinsCommand().dockerBuild('cdn-web', 'dev', '222', '10.0.1.32')