.gitlab-ci.yml 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. variables:
  2. SONAR_URL: http://sonar2.cloud.prolog.org/
  3. SONAR_LOGIN: c6b851ca3eb86f571a6900eb17dfb64e5283e918
  4. GIT_CLONE_PATH: $CI_BUILDS_DIR/repo/$CI_CONCURRENT_ID/$CI_PROJECT_NAME/$CI_COMMIT_REF_NAME
  5. PROJECT_NAME: ${CI_PROJECT_NAME}
  6. K8S_FILE: "deploy.yml"
  7. GIT_STRATEGY: none
  8. #MAVEN_OPTS: "-Dmaven.repo.local=/builds/repo/m2"
  9. MAVEN_OPTS: "-Dorg.slf4j.simpleLogger.defaultLogLevel=WARN"
  10. # CI_DEBUG_TRACE: "true"
  11. stages:
  12. - compile
  13. - sonar-check
  14. - publish-sonar-result
  15. - set-code-check-baseline
  16. - deploy-jar
  17. - build-img
  18. - k8s-deploy
  19. image: harbor.cloud.prolog.org/base-solution/maven:3-openjdk-8
  20. mvn-compile:
  21. stage: compile
  22. variables:
  23. GIT_STRATEGY: clone
  24. script:
  25. - mvn -B clean install package
  26. sonar-check:
  27. stage: sonar-check
  28. variables:
  29. GIT_STRATEGY: none
  30. script:
  31. - >
  32. mvn -B verify sonar:sonar
  33. -Dsonar.host.url=$SONAR_URL
  34. -Dsonar.login=$SONAR_LOGIN
  35. -Dsonar.analysis.mode=preview
  36. -Dsonar.gitlab.max_major_issues_gate=0
  37. -Dsonar.gitlab.max_minor_issues_gate=0
  38. -Dsonar.gitlab.max_info_issues_gate=0
  39. -Dsonar.gitlab.commit_sha=$CI_COMMIT_SHA
  40. -Dsonar.gitlab.ref_name=$CI_COMMIT_REF_NAME
  41. -Dsonar.gitlab.project_id=$CI_PROJECT_ID
  42. #
  43. publish-sonar-result:
  44. stage: publish-sonar-result
  45. script:
  46. - >
  47. mvn -B verify sonar:sonar
  48. -Dsonar.host.url=$SONAR_URL
  49. -Dsonar.projectVersion=$CI_COMMIT_SHA
  50. -Dsonar.login=$SONAR_LOGIN
  51. only:
  52. refs:
  53. - master
  54. set-code-check-baseline:
  55. stage: set-code-check-baseline
  56. variables:
  57. GIT_STRATEGY: clone
  58. script:
  59. - >
  60. mvn -B clean package verify sonar:sonar
  61. -Dsonar.host.url=$SONAR_URL
  62. -Dsonar.projectVersion=$CI_COMMIT_SHA
  63. -Dsonar.login=$SONAR_LOGIN
  64. when: manual
  65. deploy-jar:
  66. stage: deploy-jar
  67. variables:
  68. GIT_STRATEGY: clone
  69. script:
  70. - pwd
  71. - ls
  72. - mvn -B clean deploy
  73. when: manual
  74. #开始构建镜像并上传至harbor
  75. build-img:
  76. stage: build-img
  77. image: harbor.cloud.prolog.org/base-solution/drone-kaniko:v2
  78. variables:
  79. PLUGIN_REPO: "test/${CI_PROJECT_NAME}" #此处为上传harbor时候的容器名称
  80. PLUGIN_REGISTRY: "harbor.cloud.prolog.org"
  81. PLUGIN_USERNAME: "$DOCKER_USERNAME" #项目设置里面cicd 配置变量
  82. PLUGIN_PASSWORD: "$DOCKER_PASSWORD" #项目设置里面cicd 配置变量
  83. PLUGIN_SKIP_TLS_VERIFY: "true"
  84. PLUGIN_SKIP_TLS_VERIFY_PULL: "true"
  85. PLUGIN_CACHE: "true"
  86. script:
  87. - PLUGIN_TAGS="${CI_COMMIT_SHA:0:5}"
  88. - pwd
  89. - ls -l
  90. - /kaniko/plugin.sh
  91. only:
  92. refs:
  93. - dev #对应分支代码分支
  94. when: manual #手动执行,注释后会自动在每次提交变更后执行
  95. #发布流程
  96. k8s-deploy:
  97. stage: k8s-deploy
  98. variables:
  99. PROJECT_PORT: 9816 #配置文件里面的端口需要和deployment容器端口一致 次端口只能在k8s内部访问
  100. PROJECT_NODEPORT: 39816 #nodeport 为该服务在k8s集群外访问端口
  101. # PROJECT_NAME: plg-cs-eureka # 服务名称,不宜过长
  102. PROJECT_NAME: ${CI_PROJECT_NAME} #启用变量配。
  103. PROJECT_NAMESPACE: test # k8s集群内部的命名空间namespaces
  104. PROJECT_ACTIVE: k8s # 项目对应的环境配置文件
  105. # PROJECT_IMAGE: harbor.cloud.prolog.org/test/plg-cs-eureka:latest
  106. PROJECT_IMAGE: harbor.cloud.prolog.org/test/${CI_PROJECT_NAME}:latest #变量配置镜像
  107. image: harbor.cloud.prolog.org/base-solution/kubectl:v1
  108. script:
  109. - kubectl get nodes
  110. - kubectl version
  111. - sed -i "s#{PROJECT_NAME}#$PROJECT_NAME#g;
  112. s#{PROJECT_NODEPORT}#$PROJECT_NODEPORT#g;
  113. s#{PROJECT_PORT}#$PROJECT_PORT#g;
  114. s#{PROJECT_NAMESPACE}#$PROJECT_NAMESPACE#g;
  115. s#{PROJECT_IMAGE}#$PROJECT_IMAGE#g;
  116. s#{PROJECT_ACTIVE}#$PROJECT_ACTIVE#g" ./deploy.yml
  117. - cat ./deploy.yml
  118. - kubectl delete -f ./deploy.yml
  119. - kubectl apply -f ./deploy.yml
  120. only:
  121. refs:
  122. - dev #对应分支代码分支
  123. when: manual #手动执行,注释后会自动在每次提交变更后执行