Jenkinsfile 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. def SERIAL
  2. def CONTAINER_ID_CLIENT
  3. def ENAMES = [ 'prod', 'dev' ]
  4. def CLUSTERS = ['prod': 'iru-swarm1-open.infoclinica.lan', 'dev': 'dev-iru-swarm.infoclinica.lan']
  5. def REGISTRIES = ['prod': 'registry.infoclinica.ru:5000', 'dev': 'dev-registry.infoclinica.ru:5000']
  6. pipeline {
  7. agent {
  8. label "swarm"
  9. }
  10. environment {
  11. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  12. DOCKER_IMAGE='ovpn-rsa'
  13. SERVICE_NAME='ovpn-rsa_server'
  14. SERVICE_IMAGE='container_run'
  15. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  16. SWARM_GIT_NAME='stack-deploy'
  17. PKI_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/openvpn-pki.git'
  18. PKI_GIT_NAME='openvpn-pki'
  19. OVPN_GIT_DIR='openvpn'
  20. JENKINS_MAIL='jenkins@sdsys.ru'
  21. CLUSTER_NAME='dev-iru-swarm.infoclinica.lan'
  22. DOCKER_CERT_PATH='/run/secrets/swarm'
  23. }
  24. parameters {
  25. string(
  26. name: "branch",
  27. defaultValue: "97009",
  28. description: "Which branch to use"
  29. )
  30. string(
  31. name: "mailto",
  32. defaultValue: "tomishinets.v@sdsys.ru",
  33. description: "Email which has to be notified."
  34. )
  35. }
  36. stages {
  37. stage ("Discover SERIAL") {
  38. steps {
  39. script {
  40. SERIAL = sh script: "echo -n `date +%y%m%d``printf %03d $BUILD_NUMBER`", returnStdout: true
  41. }
  42. }
  43. }
  44. stage("Pull PKI repo") {
  45. steps {
  46. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  47. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  48. git clone ${PKI_GIT_URL} && cd ${WORKSPACE}/${PKI_GIT_NAME} && git checkout ${branch} && cd ${WORKSPACE}
  49. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  50. git clone ${SWARM_GIT_URL}
  51. '''
  52. }
  53. sh '''cp ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/ca.crt \
  54. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/server.crt \
  55. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/server.key \
  56. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/sds-test.crt \
  57. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/sds-test.key \
  58. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/ta.key \
  59. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/stonevpn.crl \
  60. ${WORKSPACE}/openvpn-pki/open/easy-rsa/keys/dh2048.pem \
  61. ${WORKSPACE}/openvpn/keys
  62. ls -al ${WORKSPACE}/openvpn/keys/
  63. '''
  64. }
  65. }
  66. stage("Build") {
  67. steps {
  68. echo "Building ${DOCKER_IMAGE}:${SERIAL}."
  69. sh """docker build --no-cache -t ${DOCKER_IMAGE}:${SERIAL} .
  70. if [ \$? != 0 ]; then echo 'The container was not built'; exit 1; fi
  71. """
  72. }
  73. }
  74. stage ("Push to registry") {
  75. steps {
  76. script {
  77. ENAMES.each { item ->
  78. echo "Pushing to: ${item}, CLUSTER ${CLUSTERS.get((item))}"
  79. sh """docker tag ${DOCKER_IMAGE}:${SERIAL} ${REGISTRIES.get((item))}/iru/${DOCKER_IMAGE}:${SERIAL}
  80. docker push ${REGISTRIES.get((item))}/iru/${DOCKER_IMAGE}:${SERIAL}
  81. """
  82. }
  83. }
  84. }
  85. }
  86. stage("Run in Prod-like") {
  87. steps {
  88. script {
  89. echo "Check Prod-like cluster status"
  90. sh "echo -n \${SERIAL} > \${SWARM_GIT_NAME}/tags/\${DOCKER_IMAGE}.version"
  91. sh """cd \${SWARM_GIT_NAME}/\${DOCKER_IMAGE}
  92. DOCKER_HOST=tcp://${CLUSTERS.get((ENAMES[1]))}:2376 DOCKER_TLS_VERIFY=1 ./ovpn-rsa-open-staging.sh
  93. """
  94. def NODE = sh (script: "DOCKER_HOST=tcp://${CLUSTERS.get((ENAMES[1]))}:2376 DOCKER_TLS_VERIFY=1 docker service ps \${SERVICE_NAME} --format '{{.Node}}' --filter desired-state=Running" , returnStdout: true).trim()
  95. echo "${NODE}"
  96. CONTAINER_ID_CLIENT = sh (script: "docker run -e mode=client -e server=${NODE} --privileged -d --rm ${DOCKER_IMAGE}:${SERIAL}" , returnStdout: true).trim()
  97. sh """docker exec -t ${CONTAINER_ID_CLIENT} ping -c 3 -q 10.10.20.1
  98. if [ \$? != 0 ]; then exit 1; else echo 'OVPN_RSA is working!!!'; fi
  99. """
  100. }
  101. }
  102. }
  103. stage ("Tagging") {
  104. steps {
  105. script {
  106. ENAMES.each { item ->
  107. echo "Setting latest tag for $item"
  108. sh """docker tag ${DOCKER_IMAGE}:${SERIAL} ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:latest
  109. docker push ${REGISTRIES.get((item))}/${DOCKER_IMAGE}:latest
  110. """
  111. }
  112. }
  113. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  114. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  115. sh """cd ${SWARM_GIT_NAME}
  116. echo -n ${SERIAL} > tags/${DOCKER_IMAGE}.version
  117. git add -A
  118. git config --global user.email "${JENKINS_MAIL}"
  119. git config --global user.name "Jenkins"
  120. git commit -m 'Version update'
  121. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  122. git push origin master
  123. """
  124. }
  125. }
  126. }
  127. }
  128. post {
  129. always {
  130. sh "docker stop ${CONTAINER_ID_CLIENT}"
  131. sh "DOCKER_HOST=tcp://${CLUSTERS.get((ENAMES[1]))}:2376 DOCKER_TLS_VERIFY=1 docker stack rm ${DOCKER_IMAGE}"
  132. echo "CleaningUp work directory"
  133. deleteDir()
  134. }
  135. failure {
  136. mail charset: 'UTF-8',
  137. subject: "Jenkins build ERROR",
  138. mimeType: 'text/html',
  139. to: "${mailto}",
  140. 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}"
  141. }
  142. success {
  143. mail charset: 'UTF-8',
  144. subject: "Jenkins build SUSCCESS",
  145. mimeType: 'text/html',
  146. to: "${mailto}",
  147. 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}"
  148. }
  149. }
  150. }