Jenkinsfile 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. options {
  6. ansiColor('xterm')
  7. }
  8. /*AnsiColor
  9. “\u001B[31m” = RED
  10. “\u001B[30m” = BLACK
  11. “\u001B[32m” = GREEN
  12. “\u001B[33m” = YELLOW
  13. “\u001B[34m” = BLUE
  14. “\u001B[35m” = PURPLE
  15. “\u001B[36m” = CYAN
  16. “\u001B[37m” = WHITE
  17. “\u001B[0m” is for RESET
  18. */
  19. /*Unicode icons
  20. “\u2776” = ❶
  21. “\u27A1” = ➡
  22. “\u2756” = ❖
  23. “\u273F” = ✿
  24. “\u2795” = ➕
  25. “\u2713” = ✓
  26. “\u2705” = ✅
  27. “\u274E” = ❎
  28. “\u2717” = ✗
  29. “\u274C” = ❌
  30. “\u2600” = ☀
  31. “\u2601” = ☁
  32. “\u2622” = ☢
  33. “\u2623” = ☣
  34. “\u2639” = ☹
  35. “\u263A” = ☺
  36. */
  37. environment {
  38. CLUSTER_NAME='dev-iru-swarm.infoclinica.lan'
  39. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  40. DOCKER_IMAGE='bind'
  41. SERVICE_NAME='bind'
  42. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  43. SWARM_GIT_NAME='stack-deploy'
  44. OVPN_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn.git'
  45. OVPN_GIT_NAME='openvpn'
  46. BIND_GIT_URL='ssh://git@git.sdsys.ru:8022/sdsys/bind.git'
  47. JENKINS_MAIL='jenkins.dev@sdsys.ru'
  48. SMTP_SERVER='mail.sdsys.ru'
  49. MAIL_TO='admins@sdsys.ru'
  50. }
  51. stages {
  52. stage ("PULL REPOs") {
  53. steps {
  54. echo "\u001B[32m \u2600 \u001B[32m Pull repo ${SWARM_GIT_NAME} \u001B[0m"
  55. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  56. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  57. git clone ${SWARM_GIT_URL}'''
  58. }
  59. echo "\u001B[32m \u2600 Pull repo ${OVPN_GIT_NAME} \u001B[0m"
  60. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  61. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  62. git clone ${OVPN_GIT_URL}'''
  63. }
  64. }
  65. }
  66. stage("Generate zone-files") {
  67. steps {
  68. echo "\u001B[32m \u2600 Generate ptr-zones \u001B[0m"
  69. sh '''set +x
  70. serial=$(cat ${WORKSPACE}/${SWARM_GIT_NAME}/tags/bind.version)
  71. serial=$((serial+1))
  72. ${WORKSPACE}/generate_ptr.sh 201 ${serial}
  73. ${WORKSPACE}/generate_ptr.sh 204 ${serial}
  74. ${WORKSPACE}/generate_ptr.sh 205 ${serial}
  75. '''
  76. echo "\u001B[32m \u2600 Generate infoclinica-zone \u001B[0m"
  77. sh '''set +x
  78. serial=$(cat ${WORKSPACE}/${SWARM_GIT_NAME}/tags/bind.version)
  79. serial=$((serial+1))
  80. ${WORKSPACE}/generate_zone.sh ${serial}
  81. '''
  82. }
  83. }
  84. stage("Create BIND Docker Image") {
  85. steps {
  86. echo "\u001B[32m \u2600 Create image \u001B[35m ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \u001B[0m"
  87. sh '''docker build --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} .
  88. if [ $? != 0 ]; then echo "\u274C \u001B[31m The container was not built \u001B[0m"; exit 1; fi
  89. '''
  90. }
  91. }
  92. stage("Staging") {
  93. steps {
  94. echo "\u001B[32m \u2600 Testing Bind \u001B[0m"
  95. sh '''set +x
  96. container_id_server=`docker run -d --rm ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}`
  97. docker exec -t ${container_id_server} nslookup iru-swarm.infoclinica.lan
  98. if [ $? != 0 ]; then exit 1; else echo "\u001B[32m \u2600 DNS is working!!! \u001B[0m"; fi
  99. docker stop ${container_id_server}
  100. '''
  101. }
  102. }
  103. stage("Publishing") {
  104. steps {
  105. echo "\u001B[32m \u2600 Publishing \u001B[35m ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}. \u001B[0m"
  106. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
  107. }
  108. }
  109. stage("Prod-like") {
  110. steps {
  111. echo "\u001B[32m \u2600 Check Prod-like cluster status \u001B[0m"
  112. sh '''set +x
  113. ping -c 2 ${CLUSTER_NAME}
  114. if [ $? -eq 0 ]; then
  115. export DOCKER_CERT_PATH=/run/secrets/swarm
  116. export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1
  117. docker node ls --format "{{.Hostname}} {{.TLSStatus}}" | while read host status
  118. do
  119. if [ $status != Ready ]; then echo "\u274C \u001B[31m Cluster ${CLUSTER_NAME} state is inconsistent \u001B[0m"; exit 1
  120. else echo "\u2705 \u001B[34m HOST: $host STATUS: $status \u001B[0m"
  121. fi
  122. done
  123. else echo "\u274C \u001B[31m Host not Found \u001B[0m"; exit 1
  124. fi
  125. '''
  126. echo "\u001B[32m \u2600 Run containers in Prod-like \u001B[0m"
  127. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  128. export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1
  129. export DOCKER_HOST=tcp://$(docker info -f '{{.Name}}'):2376 DOCKER_TLS_VERIFY=1
  130. if [ -z $(docker service ps -q ${DOCKER_IMAGE}) ];then
  131. docker service create --replicas 1 \
  132. --name ${SERVICE_NAME} -p 53:53/tcp -p 53:53/udp \
  133. ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}
  134. else
  135. docker service update ${SERVICE_NAME} \
  136. --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}
  137. if [ $? != 0 ]; then docker service rollback ${SERVICE_NAME}; fi
  138. fi
  139. rm -rf ${WORKSPACE}/${SWARM_GIT_NAME}
  140. '''
  141. }
  142. }
  143. stage("Tagging") {
  144. steps {
  145. echo "\u001B[32m \u2600 Tagging \u001B[35m ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \u001B[32m to \u001B[35m ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest \u001B[0m"
  146. sh '''docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \
  147. ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  148. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  149. '''
  150. echo "\u001B[32m \u2600 Updating tag info in ${SWARM_GIT_NAME} repository \u001B[0m"
  151. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  152. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  153. git clone ${SWARM_GIT_URL}
  154. cd ${SWARM_GIT_NAME}
  155. echo -n ${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
  156. git add -A
  157. git config --global user.email "${JENKINS_MAIL}"
  158. git config --global user.name "Jenkins"
  159. git commit -m 'Version update'
  160. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  161. git push origin master
  162. '''
  163. }
  164. }
  165. }
  166. }
  167. post {
  168. always {
  169. echo "\u001B[32m \u2600 CleaningUp work directory \u001B[0m"
  170. deleteDir()
  171. }
  172. failure {
  173. mail charset: 'UTF-8',
  174. subject: "Jenkins build ERROR",
  175. mimeType: 'text/html',
  176. to: "${MAIL_TO}",
  177. body: "<b>ATTENTION!!!</b> <b><br> Jenkins job failed.\n\n <b><br>Project Name:</b> ${env.JOB_NAME} <b><br>\nBuild Number:</b> ${env.BUILD_NUMBER} <b><br>\nURL Build:</b> ${RUN_DISPLAY_URL}"
  178. }
  179. }
  180. }