Jenkinsfile 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  7. DOCKER_IMAGE='node'
  8. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/iru/stack-deploy.git'
  9. SWARM_GIT_NAME='stack-deploy'
  10. JENKINS_MAIL='jenkins@sdsys.ru'
  11. }
  12. parameters {
  13. string(
  14. name: "repo",
  15. defaultValue: "prod",
  16. description: "Repository to build and/or deploy from."
  17. )
  18. string(
  19. name: "service_update",
  20. defaultValue: "info_node info_node-api",
  21. description: "Services to update - i.e. info_node or/and info_node-api."
  22. )
  23. string(
  24. name: "mailto",
  25. defaultValue: "admin@sdsys.ru",
  26. description: "Email which has to be notified."
  27. )
  28. }
  29. stages {
  30. stage("Build") {
  31. when { expression { service_update == "" } } // tmp
  32. steps {
  33. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  34. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  35. }
  36. }
  37. stage("Publish") {
  38. when { expression { service_update == "" } } // tmp
  39. steps {
  40. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  41. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  42. }
  43. }
  44. stage("Update") {
  45. // when { expression { service_update != "" } }
  46. when { expression { service_update == "" } } // tmp
  47. steps {
  48. script {
  49. for (String item : service_update.split()) {
  50. try{
  51. echo "Updating $item"
  52. sh "docker service update $item --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  53. }
  54. catch(err){
  55. echo "Recovering service $item"
  56. sh "docker service rollback $item"
  57. throw err
  58. }
  59. }
  60. }
  61. }
  62. }
  63. stage("Tagging"){
  64. steps{
  65. echo "Setting latest tag"
  66. // sh '''docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
  67. // docker pull ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest'''
  68. echo "Updating tag info in iru/stack-deploy repository"
  69. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  70. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  71. git clone ${SWARM_GIT_URL}
  72. ls
  73. cd ${SWARM_GIT_NAME}
  74. ls
  75. echo ${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
  76. git add -A
  77. git config --global user.email "${JENKINS_MAIL}"
  78. git config --global user.name "Jenkins"
  79. git commit -am 'Version update'
  80. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  81. git push origin master
  82. ls'''
  83. }
  84. }
  85. }
  86. }
  87. post {
  88. always {
  89. echo "CleaningUp work diretory"
  90. deleteDir()
  91. }
  92. failure {
  93. mail charset: 'UTF-8',
  94. subject: "Jenkins build error",
  95. mimeType: 'text/html',
  96. to: "${mailto}",
  97. 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}"
  98. }
  99. success {
  100. mail charset: 'UTF-8',
  101. subject: "Jenkins build SUSCCESS",
  102. mimeType: 'text/html',
  103. to: "${mailto}",
  104. 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}"
  105. }
  106. }
  107. }