Jenkinsfile 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  7. DOCKER_IMAGE='promo'
  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_promo",
  21. description: "Services to update."
  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. steps {
  32. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  33. sh """set +x
  34. cat ${GIT_SSH_KEY} > ${WORKSPACE}/id_rsa
  35. chmod 600 ${WORKSPACE}/id_rsa
  36. """
  37. }
  38. echo "Building ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  39. sh "docker build --build-arg repo=${repo} --no-cache -t ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  40. }
  41. }
  42. stage("Publish") {
  43. steps {
  44. echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  45. sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  46. }
  47. }
  48. stage("Update") {
  49. when { expression { service_update != "" } }
  50. steps {
  51. script {
  52. for (String item : service_update.split()) {
  53. try{
  54. echo "Updating $item"
  55. sh "docker service update $item --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}"
  56. }
  57. catch(err){
  58. echo "Recovering service $item"
  59. sh "docker service rollback $item"
  60. throw err
  61. }
  62. }
  63. }
  64. }
  65. }
  66. stage("Tagging"){
  67. steps{
  68. echo "Setting latest tag"
  69. sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  70. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest'''
  71. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  72. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  73. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  74. git clone ${SWARM_GIT_URL}
  75. cd ${SWARM_GIT_NAME}
  76. echo -n ${repo}-${BUILD_NUMBER} > tags/${DOCKER_IMAGE}.version
  77. git add -A
  78. git config --global user.email "${JENKINS_MAIL}"
  79. git config --global user.name "Jenkins"
  80. git commit -m 'Version update'
  81. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  82. git push origin master
  83. ls'''
  84. }
  85. }
  86. }
  87. }
  88. post {
  89. always {
  90. echo "CleaningUp work diretory"
  91. deleteDir()
  92. }
  93. failure {
  94. mail charset: 'UTF-8',
  95. subject: "Jenkins build ERROR",
  96. mimeType: 'text/html',
  97. to: "${mailto}",
  98. 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}"
  99. }
  100. success {
  101. mail charset: 'UTF-8',
  102. subject: "Jenkins build SUSCCESS",
  103. mimeType: 'text/html',
  104. to: "${mailto}",
  105. 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}"
  106. }
  107. }
  108. }