Jenkinsfile 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. def ENAMES = [ 'prod', 'dev' ]
  2. def CLUSTERS = ['prod': 'iru-swarm1-open.infoclinica.lan', 'dev': 'dev-iru-swarm.infoclinica.lan']
  3. def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000', 'dev': 'dev-registry.infoclinica.ru:5000']
  4. pipeline {
  5. agent {
  6. label "swarm"
  7. }
  8. environment {
  9. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  10. DOCKER_IMAGE='ovpn-rsa'
  11. SERVICE_NAME='ovpn_server'
  12. SERVICE_IMAGE='container_run'
  13. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  14. SWARM_GIT_NAME='stack-deploy'
  15. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn-pki.git'
  16. PKI_GIT_NAME='openvpn-pki'
  17. OVPN_GIT_DIR='openvpn'
  18. JENKINS_MAIL='jenkins@sdsys.ru'
  19. CLUSTER_NAME='dev-iru-swarm.infoclinica.lan'
  20. }
  21. parameters {
  22. string(
  23. name: "branch",
  24. defaultValue: "97009",
  25. description: "Which branch to use"
  26. )
  27. string(
  28. name: "mailto",
  29. defaultValue: "tomishinets.v@sdsys.ru",
  30. description: "Email which has to be notified."
  31. )
  32. }
  33. stages {
  34. stage ("Discover SERIAL") {
  35. steps {
  36. script {
  37. SERIAL = sh script: "echo -n `date +%y%m%d``printf %03d $BUILD_NUMBER`", returnStdout: true
  38. }
  39. }
  40. }
  41. stage("Pull PKI repo") {
  42. steps {
  43. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  44. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  45. git clone ${PKI_GIT_URL} && cd ${WORKSPACE}/${PKI_GIT_NAME} && git checkout ${branch} && cd ${WORKSPACE}
  46. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  47. git clone ${SWARM_GIT_URL}
  48. '''
  49. }
  50. sh '''cp ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/ca.crt \
  51. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/server.crt \
  52. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/server.key \
  53. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/sds-test.crt \
  54. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/sds-test.key \
  55. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/ta.key \
  56. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/stonevpn.crl \
  57. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/dh2048.pem \
  58. ${WORKSPACE}/openvpn/keys
  59. ls -al ${WORKSPACE}/openvpn/keys/
  60. '''
  61. }
  62. }
  63. stage("Build") {
  64. steps {
  65. echo "Building ${DOCKER_IMAGE}:${SERIAL}."
  66. sh """docker build --no-cache -t ${DOCKER_IMAGE}:${SERIAL} .
  67. if [ \$? != 0 ]; then echo 'The container was not built'; exit 1; fi
  68. """
  69. }
  70. }
  71. stage ("Push to registry") {
  72. steps {
  73. script {
  74. ENAMES.each { item ->
  75. echo "Pushing to: ${item}, CLUSTER ${CLUSTERS.get((item))}"
  76. sh """docker tag ${DOCKER_IMAGE}:${SERIAL} ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:${SERIAL}
  77. docker push ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:${SERIAL}
  78. """
  79. }
  80. }
  81. }
  82. }
  83. /* stage("Publish") {
  84. steps {
  85. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}."
  86. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
  87. }
  88. }
  89. stage("Prod-like") {
  90. steps {
  91. echo "Check Prod-like cluster status"
  92. sh '''ping -c 2 ${CLUSTER_NAME}
  93. if [ $? -eq 0 ]; then
  94. export DOCKER_CERT_PATH=/run/secrets/swarm
  95. export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1
  96. docker node ls --format "{{.Hostname}} {{.TLSStatus}}" | while read host status
  97. do
  98. if [ $status != Ready ]; then echo "Cluster ${CLUSTER_NAME} state is inconsistent"; exit 1
  99. else echo "HOST: $host STATUS: $status"
  100. fi
  101. done
  102. else echo "Host not Found"; exit 1
  103. fi
  104. '''
  105. echo "Run containers in Prod-like"
  106. sh '''export DOCKER_CERT_PATH=/run/secrets/swarm
  107. export DOCKER_HOST=tcp://${CLUSTER_NAME}:2376 DOCKER_TLS_VERIFY=1
  108. export DOCKER_HOST=tcp://$(docker info -f '{{.Name}}'):2376 DOCKER_TLS_VERIFY=1
  109. if [ -z $(docker service ps -q ${DOCKER_IMAGE}) ];then
  110. docker service create --replicas 1 \
  111. --mount type=bind,source=/var/run/docker.sock,destination=/var/run/docker.sock \
  112. --name ${SERVICE_NAME} ${DOCKER_REGISTRY}/${SERVICE_IMAGE}:2 -p 1194:1194 \
  113. --privileged --security-opt seccomp=unconfined \
  114. --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
  115. -e "mode=server" ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}
  116. else
  117. docker service update \
  118. --args "-p 1194:1194 --privileged --security-opt seccomp=unconfined \
  119. --tmpfs /run --tmpfs /run/lock -v /sys/fs/cgroup:/sys/fs/cgroup:ro \
  120. -e "mode=server" ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}" \
  121. ${SERVICE_NAME}
  122. if [ $? != 0 ]; then docker service rollback ${SERVICE_NAME}; fi
  123. fi
  124. '''
  125. }
  126. }
  127. stage("Tagging") {
  128. steps {
  129. echo "Tagging ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} to ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest"
  130. sh '''docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} \
  131. ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  132. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  133. '''
  134. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  135. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  136. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  137. git clone ${SWARM_GIT_URL}
  138. cd ${SWARM_GIT_NAME}
  139. echo -n ${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
  140. git add -A
  141. git config --global user.email "${JENKINS_MAIL}"
  142. git config --global user.name "Jenkins"
  143. git commit -m 'Version update'
  144. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  145. git push origin master
  146. '''
  147. }
  148. }
  149. }
  150. */ }
  151. post {
  152. always {
  153. echo "CleaningUp work directory"
  154. deleteDir()
  155. }
  156. failure {
  157. mail charset: 'UTF-8',
  158. subject: "Jenkins build ERROR",
  159. mimeType: 'text/html',
  160. to: "${mailto}",
  161. 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}"
  162. }
  163. success {
  164. mail charset: 'UTF-8',
  165. subject: "Jenkins build SUSCCESS",
  166. mimeType: 'text/html',
  167. to: "${mailto}",
  168. body: "<b>Congradulations!!!</b> <b><br> Jenkins job succefully finished.\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}"
  169. }
  170. }
  171. }