Jenkinsfile 6.2 KB

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