1. def POD_LABEL = "jnlp-agent-genoany-service-${UUID.randomUUID().toString()}"
    2. def BUILD_VERSION = ''
    3. def CAN_DEPLOY = "YES"
    4. def IS_SUCCESS = true
    5. podTemplate (
    6. label: POD_LABEL, cloud: 'kubernetes',
    7. containers: [
    8. containerTemplate(name: 'jnlp', image: 'jenkins/jnlp-agent-docker', args: '${computer.jnlpmac} ${computer.name}'),
    9. //containerTemplate(name: 'sonarscanner', image: 'sonarsource/sonar-scanner-cli:latest', command: 'cat', ttyEnabled: true),
    10. containerTemplate(name: 'maven', image: 'maven:3.6.0-jdk-8-alpine', ttyEnabled: true, command: 'cat')
    11. ],
    12. volumes: [
    13. hostPathVolume(hostPath: '/var/run/docker.sock', mountPath:'/var/run/docker.sock'),
    14. persistentVolumeClaim(mountPath: '/root/.m2/repository', claimName: 'maven-repo-pvc', readOnly: false),
    15. configMapVolume(mountPath: '/root/.m2', configMapName: 'settings.xml')
    16. ]
    17. ) {
    18. node(POD_LABEL) {
    19. try{
    20. stage('code-checkout') {
    21. container('maven') {
    22. checkout scm
    23. }
    24. build_tag = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
    25. }
    26. stage('settings'){
    27. moduleName = "genoany-crud-service"
    28. // 读取 Pom.xml 参数
    29. pom = readMavenPom file: "./${moduleName}/pom.xml"
    30. // 设置 appName 和 appVersion 两个全局参数
    31. appName = "${pom.artifactId}"
    32. def timeStamp = Calendar.getInstance().getTime().format('YYMMddHHmmss',TimeZone.getTimeZone('Asia/Shanghai'))
    33. appVersion = "${pom.version}"+"-"+timeStamp
    34. echo "服务名:${appName}"
    35. echo "版本号:${appVersion}"
    36. }
    37. stage('maven-package'){
    38. container('maven') {
    39. sh "cd ${moduleName}"
    40. sh "mvn -pl ${moduleName} -am -B clean install package -U -DskipTests=true"
    41. }
    42. }
    43. stage('docker-build') {
    44. sh """
    45. cd ${moduleName}
    46. docker build -t 192.168.1.207:6543/com.newpwr/${appName}:${appVersion} .
    47. """
    48. }
    49. stage('docker-push') {
    50. withCredentials([usernamePassword(credentialsId: 'localDocker', passwordVariable: 'localDockerPassword', usernameVariable: 'localDockerUser')]) {
    51. sh "docker login 192.168.1.207:6543 -u ${localDockerUser} -p ${localDockerPassword}"
    52. sh "docker push 192.168.1.207:6543/com.newpwr/${appName}:${appVersion}"
    53. }
    54. }
    55. stage('deploy') {
    56. if(CAN_DEPLOY == "YES"){
    57. sh """
    58. curl -X PATCH \
    59. -H "content-type: application/strategic-merge-patch+json" \
    60. -H "Authorization:Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6IkY5NmtWZjVTbWNLdjZ5TWpzZUpPSlJaQjJ4bTBjV1FPY3RFTFNOZDBzczAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJvYXJkLXVzZXItdG9rZW4tcGc1enAiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoia3Vib2FyZC11c2VyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiYjA0ZmM5NDMtYmE3NS00NWY2LTlmZmYtYmM2ZmQyMjhlN2RjIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Omt1YmUtc3lzdGVtOmt1Ym9hcmQtdXNlciJ9.AEmK0-j_xi_Go4xVPLmtZa587C2WUEWcib1UjXgWO4ZoRB_4cRPBiDzmU8kcxKl8-39OY8mg-DQ5yuygB7biDO9RNlPwHGCNcYe63bhaFjp5W3zUq51TDx57r7VKkOAwor9nFwxNEYBauTmA_0_jnloCUMLmKRLlMsttQ_QumPdDFuHruC56yioAQfHv7h9EDQioiGVOO8ng4xW0goutsUOpQbRkQ4TbLck3_oGX7zX7QOazBDHxo-x73TJ6VgsrNuCnSTgKYQNiWAhNYnySLZ5THuT2agol1mkSgAp6B9gIZOL_2zYiOER_an3B9-GSRHIxuYj_ouxBL19T-m1SvQ" \
    61. -d '{"spec":{"template":{"spec":{"containers":[{"name":"genoany-crud-service","image":"192.168.1.207:6543/com.newpwr/${appName}:${appVersion}"}]}}}}' \
    62. "http://192.168.1.18:32567/k8s-api/apis/apps/v1/namespaces/genoany-dev/deployments/${appName}"
    63. """
    64. }
    65. }
    66. } catch (Exception ex) {
    67. IS_SUCCESS= false
    68. throw ex
    69. } finally {
    70. if(IS_SUCCESS){
    71. echo "success"
    72. } else {
    73. echo "not success"
    74. }
    75. }
    76. }
    77. }