Jenkinsfile-prod 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_IMAGE='iru/node'
  7. DOCKER_CERT_PATH='/run/secrets/swarm'
  8. DHOST='tcp://iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
  9. DEV_REGISTRY='dev-registry.infoclinica.ru:5000'
  10. PROD_REGISTRY='registry.infoclinica.ru:5000'
  11. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  12. SWARM_GIT_NAME='stack-deploy'
  13. JENKINS_MAIL='jenkins@sdsys.ru'
  14. NEWTAG=''
  15. }
  16. parameters {
  17. string(
  18. name: "service_update",
  19. defaultValue: "info_node info_node-api",
  20. description: "Service name to update."
  21. )
  22. string(
  23. name: "mailto",
  24. defaultValue: "admin@sdsys.ru",
  25. description: "Email which has to be notified."
  26. )
  27. }
  28. stages {
  29. stage("Tagging in Git"){
  30. steps{
  31. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  32. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  33. git clone ${SWARM_GIT_URL}
  34. cd ${SWARM_GIT_NAME}
  35. cp -f tags/${DOCKER_IMAGE}.dev-version tags/${DOCKER_IMAGE}.version
  36. git add -A
  37. git config --global user.email "${JENKINS_MAIL}"
  38. git config --global user.name "Jenkins"
  39. git commit -m 'Version update'| grep '' # Shield ourself from non-zero exit code from 'git commit' command
  40. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  41. git push origin master
  42. ls"""
  43. }
  44. }
  45. }
  46. stage("Tagging and pushing to prod registry"){
  47. steps{
  48. script {
  49. NEWTAG=sh (script: "cat ${SWARM_GIT_NAME}/tags/${DOCKER_IMAGE}.version", returnStdout: true).trim()
  50. sh """docker pull ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  51. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  52. docker tag ${DEV_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
  53. docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}
  54. docker push ${PROD_REGISTRY}/${DOCKER_IMAGE}:latest
  55. """
  56. }
  57. }
  58. }
  59. stage("Updating service"){
  60. steps{
  61. script {
  62. for (String item : service_update.split()) {
  63. echo "Updating ${item}"
  64. try{
  65. sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  66. }
  67. catch(err){
  68. echo "Recovering service $item"
  69. sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
  70. throw err
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. post {
  78. always {
  79. echo "CleaningUp work diretory"
  80. deleteDir()
  81. sh "docker image rm -f `docker image ls -q ${PROD_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}`"
  82. }
  83. failure {
  84. mail charset: 'UTF-8',
  85. subject: "Jenkins build ERROR",
  86. mimeType: 'text/html',
  87. to: "${mailto}",
  88. 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}"
  89. }
  90. success {
  91. mail charset: 'UTF-8',
  92. subject: "Jenkins build SUSCCESS",
  93. mimeType: 'text/html',
  94. to: "${mailto}",
  95. 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}"
  96. }
  97. }
  98. }