jenkins Get Build Cause

 
  1. def pipeId = 1130561944231279390

  2. def pipeLogId

  3. def isTagOrBranch

  4. def tagOrBranch

  5. def imageId

  6. def addPipelineLog() {

  7. def isUserTriggered = currentBuild.rawBuild.getCause(hudson.model.Cause$UserIdCause) != null

  8. def isGitLabWebHookTriggered = currentBuild.rawBuild.getCause(com.dabsquared.gitlabjenkins.cause.GitLabWebHookCause) != null

  9. def isTimerTriggered = currentBuild.rawBuild.getCause(hudson.triggers.TimerTrigger$TimerTriggerCause) != null

  10. def isUpstreamTriggered = currentBuild.rawBuild.getCause(hudson.model.Cause.UpstreamCause) != null

  11. println "trigger type:"+currentBuild.rawBuild.getCauses().dump()

  12. println "params.logId: "+params.logId

  13. println "params.projectVersionConfigId: "+params.projectVersionConfigId

  14. println "params.publishLogId: "+params.publishLogId

  15. println "params.preExecuteEndPipeStageId: "+params.preExecuteEndPipeStageId

  16.  
  17. def reqPipeLogId = params.logId

  18. def command = $/curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc' -d '{"pipeId":1130561944231279390,"pipeLogId":"$reqPipeLogId","isUserTriggered":$isUserTriggered,"isGitLabWebHookTriggered":$isGitLabWebHookTriggered,"isTimerTriggered":$isTimerTriggered,"isUpstreamTriggered":$isUpstreamTriggered}' "http://10.145.196.76:12000/pipeline/pipelines/pipelineLog" /$

  19. res = sh(returnStdout: true, script: command).trim()

  20. println "add pipeline log result:\n" + res

  21.  
  22. return parseJsonText(res).body.content

  23. }

  24.  
  25. def getStageLog(log) {

  26. def stageLog = log.substring(log.lastIndexOf("[Pipeline] stage"))

  27. echo "log size:" + stageLog.length()

  28.  
  29. return URLEncoder.encode(stageLog, "UTF-8")

  30. }

  31.  
  32. def toUpdatePipelineLogCurlSh(result, duration, log, pipeLogId, pipeStageId) {

  33. def tmpDir = 'deploy_tmp'

  34. def tmpFile = 'updatePipelineLog_params_tmp'

  35.  
  36. def data = '{"flag":"'+result+'","duration":"'+duration+'","message":"'+log+'","pipeLogId":"'+pipeLogId+'","pipeStageId":"'+pipeStageId+'","preExecuteEndPipeStageId":"'+preExecuteEndPipeStageId+'"}';

  37. writeToFile(env.WORKSPACE, tmpDir, tmpFile, data)

  38.  
  39. def tmpFilePath = tmpDir + '/' + tmpFile

  40. def curl = 'curl -X PUT --header \'Content-Type: application/json\' --header \'Accept: application/json\' --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' --data-binary "@'+ tmpFilePath +'" "http://10.145.196.76:12000/pipeline/pipelines/pipelineStageLog" '

  41. return curl

  42. }

  43.  
  44. def toEndPipelineLogCurlSh(result, pipeLogId, projectVersionConfigId, publishLogId) {

  45. def curl = 'curl -X GET --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' "http://10.145.196.76:12000/pipeline/pipelines/pipeLineLog/'+pipeLogId+'?flag='+result+'&projectVersionConfigId='+projectVersionConfigId+'&publishLogId='+publishLogId+'" '

  46. return curl

  47. }

  48.  
  49. //预执行结束阶段校验

  50. def preExecuteEndPipelineStageCheck(pipeStageId) {

  51. def pipeStageIdStr = '' + pipeStageId

  52. def isEndPoint = (params.preExecuteEndPipeStageId != null && pipeStageIdStr == params.preExecuteEndPipeStageId)

  53. if (isEndPoint) {

  54. println "this stage is pre-execute end stage, pre-execute finished "

  55. error "preExecuteFinished"

  56. }

  57. }

  58.  
  59. //若异常是预执行结束,将result置为SUCCESS

  60. def setPreExecuteResult(result, msg) {

  61. if (msg == 'preExecuteFinished') {

  62. result = 'SUCCESS'

  63. println "this exception message is pre-execute finished , set result to SUCCESS "

  64. println "本阶段是预执行结束阶段,预执行完成"

  65. cleanWs()

  66. println "clean workspace"

  67. }

  68. return result

  69. }

  70.  
  71. //sonar check analysis

  72. def toSonarCheckCurlSh(appId, appKey) {

  73. def curl = 'curl -X POST --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' -d \'appId='+appId+'&appKey='+appKey+'\' "http://10.145.196.76:12000/quality/analysis" '

  74. return curl

  75. }

  76.  
  77. def analysisSonarCheck(appId, appKey) {

  78. def data = '?appId='+appId+'&appKey='+appKey

  79. def url = 'http://10.145.196.76:12000/quality/analysis'+data

  80. println "request " + url + " to analysis sonar check result \nparams: " + data

  81. return paasPost(url, null)

  82. }

  83.  
  84. def getSonarAppProfile(appId) {

  85. /*

  86. // GET

  87. def get = new URL("http://10.145.196.76:12000/quality/app/profile/"+appId).openConnection();

  88. get.setRequestProperty("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc")

  89.  
  90. def getRC = get.getResponseCode();

  91. def res;

  92. if(getRC.equals(200)) {

  93. res=get.getInputStream().getText()

  94. println(res);

  95. } else {

  96. error "Sonar Check Stage - getSonarAppProfile(" + appId + ") get sonar app profile error :" + getRC

  97. }

  98.  
  99. return parseJsonText(res).body.content

  100. */

  101.  
  102. def url = 'http://10.145.196.76:12000/quality/app/profile/'+appId

  103. println "request " + url + " to get sonar app profile"

  104. return paasGet(url)

  105. }

  106.  
  107. def toApproveCurlSh(pipeId, pipeLogId, pipeStageId, buildId, jobName) {

  108. def curl = 'curl -X POST --header \'Content-Type: application/json\' --header \'Accept: application/json\' --header \'token: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc\' -d \'{"pipeId":"'+pipeId+'","pipeLogId":"'+pipeLogId+'","pipeStageId":"'+pipeStageId+'","inputId":"'+pipeStageId+'","buildId":"'+buildId+'","jobName":"'+jobName+'"}\' "http://10.145.196.76:12000/pipeline/pipelines/addPersonalTask" '

  109. return curl

  110. }

  111.  
  112. //sync harbor image by our api

  113. def syncPaasImage(imageId, targetCeEnv, pipeLogId, pipeStageId) {

  114. def url = 'http://10.145.196.76:12000/pipeline/image/sync'

  115. def data = '{"imageId":"'+imageId+'","targetCeEnv":"'+targetCeEnv+'","pipeLogId":"'+pipeLogId+'","pipeStageId":"'+pipeStageId+'"}'

  116. println "request " + url + " to sync harbor image by pipeline's api\nparams: " + data

  117. return paasPost(url, data)

  118. }

  119.  
  120. //sync harbor image by ce-api directly

  121. def syncPaasImage(url, sourceAddress, destAddress, codePackagePath, imageName, pipeLogId) {

  122. if (imageName == null || imageName == '' || imageName == 'null') {

  123. //适用于构建镜像阶段(选择镜像阶段不需要,yaml中已有镜像名)

  124. imageName = getImageName(codePackagePath, pipeLogId)

  125. }

  126. sourceAddress = sourceAddress+':'+imageName

  127. destAddress = destAddress+':'+imageName

  128. def data = '{"pullpoint":"'+sourceAddress+'","pushpoint":"'+destAddress+'"}'

  129. println "request " + url + " to sync harbor image by ce-api directly\nparams: " + data

  130. return paasPost(url, data)

  131. }

  132.  
  133. //record sync-image info

  134. def recordSyncImageInfo(imageId, ceEnvKey, pipeLogId, pipeStageId) {

  135. def url = 'http://10.145.196.76:12000/pipeline/image/syncImage'

  136. def data = '{"imageId":"'+imageId+'", "imageCeEnv":"'+ceEnvKey+'",' +

  137. '"pipeStageId":"'+pipeStageId+'", "pipeLogId":"'+pipeLogId+'"}'

  138. println "request " + url + " to save sync-image info\nparams: " + data

  139. return paasPost(url, data)

  140. }

  141.  
  142. //build harbor image

  143. def buildPaasImage(ceRemoteAddress, imgNamespaceName, repoName,

  144. tagName, codeFilePath, hasAdditionalFile, curlUploadFilePara,

  145. dockerFileType, dockerfilePath, dockerFileContent) {

  146.  
  147. //upload files

  148. uploadImageFiles(codeFilePath, hasAdditionalFile, curlUploadFilePara,

  149. ceRemoteAddress, imgNamespaceName, repoName, tagName);

  150.  
  151. //dockerfile

  152. dockerfile = getDockerfileContent(dockerFileType, dockerfilePath, dockerFileContent)

  153.  
  154. //build image

  155. buildPaasImage(dockerfile,

  156. ceRemoteAddress, imgNamespaceName, repoName, tagName);

  157.  
  158. //wait image build finished

  159. waitImageBuildFinished(ceRemoteAddress, imgNamespaceName, repoName, tagName);

  160. }

  161.  
  162. //record image info

  163. def recordImageInfo(ceEnvKey, imgNamespaceName, repoName, tagName,

  164. isTagOrBranch, tagOrBranch, pipeLogId, pipeStageId) {

  165. def url = 'http://10.145.196.76:12000/pipeline/image'

  166. def data = '{"imageCeEnv":"'+ceEnvKey+'", "imageNamespace":"'+imgNamespaceName+'", "imageRepo":"'+repoName+'", "imageName":"'+tagName+'",' +

  167. '"sourceCodeType":"'+isTagOrBranch+'", "sourceCode":"'+tagOrBranch+'",' +

  168. '"appId":"123", "baseId":"1107175323881272818", "pipeId":"1130561944231279390", "pipeStageId":"'+pipeStageId+'",' +

  169. '"pipeLogId":"'+pipeLogId+'"}'

  170. println "request " + url + " to save image info\nparams: " + data

  171. return paasPost(url, data)

  172.  
  173. }

  174.  
  175. def uploadImageFiles(codeFilePath, hasAdditionalFile, curlUploadFilePara,

  176. ceRemoteAddress, imgNamespaceName, repoName, tagName) {

  177.  
  178. def otherFilePara = ""

  179. if (hasAdditionalFile) {

  180. otherFilePara = curlUploadFilePara

  181. }

  182. def uploadCommand = '''

  183. #上传构建镜像所需文件

  184. curl -F "files=@'''+codeFilePath+'''" '''+otherFilePara+''' --header "token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc" "'''+ceRemoteAddress+'''/zuul/ceimage/uploadfiles?namespace-name='''+imgNamespaceName+'''&repo-name='''+repoName+'''&tag-name='''+tagName+'''&username=temscmt"

  185. '''

  186. def uploadRes = sh(returnStdout: true, script: uploadCommand).trim()

  187. println "upload image related files to paas container result:\n " + uploadRes

  188. handleException(uploadRes, 'upload image related files to paas container')

  189. }

  190.  
  191. def getDockerfileContent(dockerFileType, dockerfilePath, dockerFileContent) {

  192. def tmpDir = 'deploy_tmp'

  193.  
  194. //dockerfile内容输出到文件

  195. if (dockerFileType == 'INPUT') {

  196. def tmpFile = 'Dockerfile_devops_tmp'

  197. println "dockerfile content is by user input online, so need to write to a tmp file"

  198. writeToFile(env.WORKSPACE, tmpDir, tmpFile, dockerFileContent)

  199. dockerfilePath = tmpDir + '/' + tmpFile

  200. }

  201.  
  202. dockerfile = readFile encoding: 'UTF-8', file: dockerfilePath

  203. dockerfile = dockerfile.replaceAll("\n", "\\\\n")

  204.  
  205. return dockerfile

  206. }

  207.  
  208. def writeToFile(currentPath, relativeDirPath, filename, content) {

  209. println "write to file " + currentPath + " " + relativeDirPath + " " +filename

  210. writeFile encoding: 'UTF-8', file: relativeDirPath+'/'+filename, text: content

  211. println "write to file finish"

  212. }

  213.  
  214. def buildPaasImage(dockerfile,

  215. ceRemoteAddress, imgNamespaceName, repoName, tagName) {

  216. def baseImageName='centos-jdk8'

  217. def baseImageTag='latest'

  218.  
  219. def url = ceRemoteAddress + '/ceimage/build'

  220. def data = '{"baseImageName": "'+baseImageName+'","baseImageTag": "'+baseImageTag+'", "dockerfile": "'+dockerfile+'", "namespaceName": "'+imgNamespaceName+'", "repoName": "'+repoName+'", "tag": "'+tagName+'", "username":"temscmt"}'

  221. println "request " + url + " to build image\nparams: " + data

  222. paasPost(url, data)

  223.  
  224. /*

  225. def buildCommand = '''

  226. #构建镜像

  227. curl -X POST --header 'Content-Type: application/json' \

  228. --header 'Accept: application/json' \

  229. --header "token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc" \

  230. -d '{"baseImageName": "'''+baseImageName+'''","baseImageTag": "'''+baseImageTag+'''", "dockerfile": "'''+dockerfile+'''", "namespaceName": "'''+imgNamespaceName+'''", "repoName": "'''+repoName+'''", "tag": "'''+tagName+'''", "username":"temscmt"}' \

  231. "'''+ceRemoteAddress+'''/ceimage/build"

  232. '''

  233. def buildRes = sh(returnStdout: true, script: buildCommand).trim()

  234. handleException(buildRes, 'build image to paas container error')

  235. */

  236. }

  237.  
  238. def waitImageBuildFinished(ceRemoteAddress, imgNamespaceName, repoName, tagName) {

  239. timeout(10) {

  240. waitUntil {

  241. def res = getCeImageBuildDetail(ceRemoteAddress, imgNamespaceName, repoName, tagName)

  242. if (res == null) {

  243. println " image build detail body null 镜像还未找到... "

  244. return false

  245. } else if (res.status == '1') {

  246. println " image build detail status 1 镜像构建中 ... "

  247. return false

  248. } else if (res.status == '2') {

  249. println " image build detail status 2 镜像构建成功 ... "

  250. return true

  251. } else if (res.status == '3') {

  252. println " image build detail status 3 镜像构建失败 ... "

  253. error "镜像构建失败"

  254. } else {

  255. println " image build detail status other ... " + res.status

  256. return false

  257. }

  258. }

  259. }

  260. }

  261.  
  262. def getCeImageBuildDetail(ceRemoteAddress, imgNamespaceName, repoName, tagName) {

  263. def url = ceRemoteAddress + '/ceimage/buildrecords/namespaces/' + imgNamespaceName + '/repos/' + repoName + '/tags/' + tagName

  264. println "request " + url + " to get image build detail"

  265. return paasGet(url)

  266. }

  267.  
  268. //select harbor image

  269. def selectPaasImage(ceRemoteAddress, imgNamespaceName, repoName, tagName) {

  270. def res = getCeImageBuildDetail(ceRemoteAddress, imgNamespaceName, repoName, tagName)

  271. if (res != null && res.status == '2') {

  272. println " 镜像存在"

  273. } else {

  274. println " image status other ... " + res.status

  275. error "镜像状态异常"

  276. }

  277. }

  278.  
  279. //ansible deploy

  280. def ansibleDeploy() {

  281. def url = 'http://10.145.196.76:12000/pipeline/pipelines/get/stage/config?pipeId=1130561944231279390'

  282. println "request " + url + " to get ansible inventory and playbook"

  283. def res = paasGet(url)

  284.  
  285. def inventory = res.inventory

  286. //println "res.inventory:\n" + inventory

  287. def playbook = res.playbook

  288. println "res.playbook:\n" + playbook

  289.  
  290. def tmpDir = 'deploy_tmp';

  291. println "inventory content is by user input online, so need to write to a tmp file"

  292. writeToFile(env.WORKSPACE, tmpDir, 'inventory_tmp', inventory)

  293. println "playbook content is by user input online, so need to write to a tmp file"

  294. writeToFile(env.WORKSPACE, tmpDir, 'playbook_tmp.yml', playbook)

  295. }

  296.  
  297. //deploy paas app

  298. def deployPaasApp(pipeStageId, codePackagePath, pipeLogId) {

  299. def codeFileNoExt = ''

  300. if (codePackagePath != null && codePackagePath != '' && codePackagePath != 'null') {

  301. //适用于构建镜像阶段(选择镜像阶段不需要,yaml中已有镜像名)

  302. codeFileNoExt = getCodeFileNameNoExt(codePackagePath)

  303. }

  304. def url = 'http://10.145.196.76:12000/pipeline/pipelines/paasdeploy'

  305. def data = '{"pipeId":"1130561944231279390","pipeStageId":"'+pipeStageId+'","parameters":{"codeFileNoExt":"'+codeFileNoExt+'","buildId":"'+env.BUILD_ID+'","pipeLogId":"'+pipeLogId+'"}}'

  306. println "request " + url + " to deploy paas app\nparams: " + data

  307. return paasPost(url, data)

  308.  
  309. /*

  310. def command = '''

  311. #容器应用发布

  312. curl -X POST --header 'Content-Type: application/json' \

  313. --header 'Accept: application/json' \

  314. --header "token:eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc" \

  315. -d '{"pipeId": "1130561944231279390","pipeStageId": "'''+pipeStageId+'''", "parameters": {"codeFileNoExt":"'''+codeFileNoExt+'''", "buildId": "'''+env.BUILD_ID+'''", "pipeLogId": "'''+pipeLogId+'''"}}' \

  316. "http://10.145.196.76:12000/pipeline/pipelines/paasdeploy"

  317. '''

  318. def res = sh(returnStdout: true, script: command).trim()

  319. handleException(res, 'deploy app to paas container error')

  320. */

  321. }

  322.  
  323. //get image name by code package's fuzzy path user input

  324. def getImageName(codePackagePath, pipeLogId) {

  325. codeFileNoExt = getCodeFileNameNoExt(codePackagePath)

  326. return formatImageName(codeFileNoExt, pipeLogId)

  327. }

  328.  
  329. //get image name by code package's exact relative path

  330. def getImageNameByExactPath(codeFilePath, pipeLogId) {

  331. codeFileNoExt = getCodeFileNameNoExtByExactPath(codeFilePath)

  332. return formatImageName(codeFileNoExt, pipeLogId)

  333. }

  334.  
  335. //format image name

  336. def formatImageName(codeFileNoExt, pipeLogId) {

  337. return codeFileNoExt + '-te-msc-paas-gateway-' + pipeLogId + '-' + env.BUILD_ID

  338. }

  339.  
  340. //get code package filename without extension by fuzzy path user input

  341. def getCodeFileNameNoExt(codePackagePath) {

  342. def codeFilePath = getCodeFilePath(codePackagePath)

  343. return getCodeFileNameNoExtByExactPath(codeFilePath)

  344. }

  345.  
  346. //get code package filename without extension by its exact relative path

  347. def getCodeFileNameNoExtByExactPath(codeFilePath) {

  348. def command = '''

  349. #获取包名

  350. codeFile=$(basename '''+codeFilePath+''')

  351.  
  352. #获取不含后缀的包名

  353. echo ${codeFile%.*}

  354. '''

  355. return sh(returnStdout: true, script: command).trim()

  356. }

  357.  
  358. //find code package and return relative path by fuzzy path user input

  359. def getCodeFilePath(codePackagePath) {

  360. def command = '''

  361. #获取包文件

  362. codePackageSearchName=$(basename "'''+codePackagePath+'''")

  363. codePackageDirname=$(dirname "'''+codePackagePath+'''")

  364. codeFile=$(find $codePackageDirname -name $codePackageSearchName)

  365.  
  366. #若模糊匹配的文件有多个,只取第一个

  367. echo $codeFile | awk '{ print $1 }'

  368. '''

  369. return sh(returnStdout: true, script: command).trim()

  370. }

  371.  
  372. // PAAS GET

  373. def paasGet(url) {

  374. def req = new URL(url).openConnection();

  375. req.setRequestProperty("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc")

  376. def getRC = req.getResponseCode()

  377. println "response code:"+getRC

  378. def res;

  379. if(getRC.equals(200)) {

  380. res=req.getInputStream().getText()

  381. def code = parseJsonText(res).code

  382. println "response paas json code:"+code

  383. if (code != '000000') {

  384. error "ERROR: request " + url + " " + code + " error.\n" + res

  385. }

  386. println("result:\n" + res)

  387. } else {

  388. println "ERROR: request " + url + " " + getRC + " error."

  389. error req.getInputStream().getText()

  390. }

  391. return parseJsonText(res).body.content

  392. }

  393.  
  394. // PAAS POST

  395. def paasPost(url, data) {

  396. def req = new URL(url).openConnection()

  397. req.setRequestMethod("POST")

  398. req.setDoOutput(true)

  399. req.setRequestProperty("token","eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ0ZW1zY210In0.7Kno6QoIFmT8nQv-C2edlP514ekqy9pPNID_Vf__DPc")

  400. req.setRequestProperty("Content-Type", "application/json")

  401. if (data != null) {

  402. req.getOutputStream().write(data.getBytes("UTF-8"))

  403. }

  404. def postRC = req.getResponseCode()

  405. println "response code:"+postRC

  406. def body;

  407. if(postRC.equals(200)) {

  408. body = req.getInputStream().getText()

  409. def code = parseJsonText(body).code

  410. println "response paas json code:"+code

  411. if (code != '000000') {

  412. error "ERROR: request " + url + " " + code + " error.\n" + body

  413. }

  414. println("result:\n" + body)

  415. }else{

  416. println "ERROR: request " + url+ " " + postRC + " error."

  417. error req.getInputStream().getText()

  418. }

  419. return parseJsonText(body).body.content

  420. }

  421.  
  422. def parseJsonText(String json) {

  423. return new groovy.json.JsonSlurperClassic().parseText(json)

  424. }

  425.  
  426. def handleException(res, msg) {

  427. def code = parseJsonText(res).code

  428. if (code != '000000') {

  429. error "ERROR: " + msg + " " + code + " \n" + res

  430. }

  431. }

  432.  
  433. node{

  434. stage('GIT拉取代码') {

  435. pipeLogId = addPipelineLog()

  436. def pipeStageId = 1130561945160317727

  437. try{

  438. isTagOrBranch = 'TAG'

  439. tagOrBranch = params.tagOrBranch

  440. checkout([

  441. $class: 'GitSCM',branches: [[name: "${params.tagOrBranch}"]],

  442. userRemoteConfigs: [[url: 'http://10.145.196.76:9080/Shtel-PaaS-Platform/shtel-paas-service-gateway.git', credentialsId: 'c7887cee85a24fdfa8e372d317fa1d83']]

  443. ])

  444. preExecuteEndPipelineStageCheck(pipeStageId)

  445. sleep(unit: 'MILLISECONDS', time: 500)

  446. sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, currentBuild.duration, getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  447. }catch(e){

  448. result = 'FAILURE'

  449. echo e.getMessage()

  450. result = setPreExecuteResult(result, e.getMessage())

  451. sleep(unit: 'MILLISECONDS', time: 500)

  452. sh(script: toUpdatePipelineLogCurlSh(result, currentBuild.duration, getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  453. sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))

  454. throw e

  455. }

  456. }

  457. }

  458. node{

  459. stage('Maven构建') {

  460. def mvnHome = tool 'maventool'

  461. def pipeStageId = 1130561945160317728

  462. try{

  463. sh "${mvnHome}/bin/mvn -f pom.xml clean package -X -U -Dmaven.test.skip=true"

  464. preExecuteEndPipelineStageCheck(pipeStageId)

  465. sleep(unit: 'MILLISECONDS', time: 500)

  466. sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  467. }catch(e){

  468. result = 'FAILURE'

  469. echo e.getMessage()

  470. result = setPreExecuteResult(result, e.getMessage())

  471. sleep(unit: 'MILLISECONDS', time: 500)

  472. sh(script: toUpdatePipelineLogCurlSh(result, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  473. sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))

  474. throw e

  475. }

  476. }

  477. }

  478. node{

  479. stage('Paas构建镜像') {

  480. def pipeStageId = 1130561945160317729

  481. try{

  482. ceEnvKey = 'test'

  483. ceRemoteAddress = 'http://10.145.208.200:31601'

  484. imgNamespaceName = 'te-msc-paas'

  485. repoName = 'te-msc-paas-gateway'

  486. codePackagePath = '*.tar.gz'

  487. hasAdditionalFile = false

  488. curlUploadFilePara = ''

  489. dockerFileType = 'INPUT'

  490. dockerfilePath = ''

  491. dockerFileContent = '''FROM hub.paas/base/centos-jdk8-cn:latest

  492. ADD *.tar.gz /usr/local/gateway-service/

  493. RUN chmod u+x /usr/local/gateway-service/bin/run.sh

  494. EXPOSE 8888

  495. CMD '/usr/local/gateway-service/bin/run.sh' '''

  496. def codeFilePath = getCodeFilePath(codePackagePath)

  497. def tagName = getImageNameByExactPath(codeFilePath, pipeLogId)

  498. buildPaasImage(ceRemoteAddress, imgNamespaceName, repoName, tagName, codeFilePath, hasAdditionalFile, curlUploadFilePara, dockerFileType, dockerfilePath, dockerFileContent)

  499. imageId = recordImageInfo(ceEnvKey, imgNamespaceName, repoName, tagName, isTagOrBranch, tagOrBranch, pipeLogId, pipeStageId)

  500. preExecuteEndPipelineStageCheck(pipeStageId)

  501. sleep(unit: 'MILLISECONDS', time: 500)

  502. sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  503. }catch(e){

  504. result = 'FAILURE'

  505. echo e.getMessage()

  506. result = setPreExecuteResult(result, e.getMessage())

  507. sleep(unit: 'MILLISECONDS', time: 500)

  508. sh(script: toUpdatePipelineLogCurlSh(result, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  509. sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))

  510. throw e

  511. }

  512. }

  513. }

  514. node{

  515. stage('paas发布部署') {

  516. def pipeStageId = 1130561945161366306

  517. try{

  518. deployPaasApp(pipeStageId, '*.tar.gz', pipeLogId)

  519. preExecuteEndPipelineStageCheck(pipeStageId)

  520. sleep(unit: 'MILLISECONDS', time: 500)

  521. sh(script: toUpdatePipelineLogCurlSh(currentBuild.currentResult, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  522. sh(script: toEndPipelineLogCurlSh(currentBuild.currentResult, pipeLogId, params.projectVersionConfigId, params.publishLogId))

  523. }catch(e){

  524. result = 'FAILURE'

  525. echo e.getMessage()

  526. result = setPreExecuteResult(result, e.getMessage())

  527. sleep(unit: 'MILLISECONDS', time: 500)

  528. sh(script: toUpdatePipelineLogCurlSh(result, '', getStageLog(currentBuild.rawBuild.log), pipeLogId, pipeStageId))

  529. sh(script: toEndPipelineLogCurlSh(result, pipeLogId, params.projectVersionConfigId, params.publishLogId))

  530. throw e

  531. }

  532. }

  533. }

  534.  
  535. node{

  536. stage('clean workspace') {

  537. cleanWs()

  538. }

  539. }

转载于:https://www.cnblogs.com/fan-gx/p/11220221.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值