Jenkinsfile 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. NEWTAG=''
  12. }
  13. parameters {
  14. string(
  15. name: "GitBranch",
  16. defaultValue: "newPromoSpring",
  17. description: "Which branch to use"
  18. )
  19. string(
  20. name: "service_update",
  21. defaultValue: "info_promo",
  22. description: "Services to update."
  23. )
  24. string(
  25. name: "mailto",
  26. defaultValue: "admin@sdsys.ru",
  27. description: "Email which has to be notified."
  28. )
  29. }
  30. stages {
  31. stage("Build") {
  32. steps {
  33. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  34. sh """set +x
  35. cat ${GIT_SSH_KEY} > ${WORKSPACE}/id_rsa
  36. chmod 600 ${WORKSPACE}/id_rsa
  37. """
  38. }
  39. echo "Building ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  40. sh "docker build --build-arg branch=${GitBranch} --no-cache -t ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${BUILD_NUMBER} ."
  41. script {
  42. NEWTAG = sh script: "docker run --rm ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} cat /tmp/version 2> /dev/null", returnStdout: true
  43. NEWTAG = NEWTAG + "_" + BUILD_NUMBER
  44. echo "NEWTAG is $NEWTAG"
  45. }
  46. sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  47. }
  48. }
  49. stage("Publish") {
  50. steps {
  51. echo "Publishing ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
  52. sh "docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
  53. }
  54. }
  55. stage("Update") {
  56. when { expression { service_update != "" } }
  57. steps {
  58. script {
  59. for (String item : service_update.split()) {
  60. try{
  61. echo "Updating $item"
  62. sh "docker service update $item --image ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${NEWTAG}"
  63. }
  64. catch(err){
  65. echo "Recovering service $item"
  66. sh "docker service rollback $item"
  67. throw err
  68. }
  69. }
  70. }
  71. }
  72. }
  73. stage("Tagging"){
  74. steps{
  75. echo "Setting latest tag"
  76. sh '''docker tag ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest
  77. docker push ${DOCKER_REGISTRY}/iru/${DOCKER_IMAGE}:latest'''
  78. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  79. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  80. sh '''GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  81. git clone ${SWARM_GIT_URL}
  82. cd ${SWARM_GIT_NAME}
  83. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.version
  84. git add -A
  85. git config --global user.email "${JENKINS_MAIL}"
  86. git config --global user.name "Jenkins"
  87. git commit -m 'Version update'
  88. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  89. git push origin master
  90. ls'''
  91. }
  92. }
  93. }
  94. }
  95. post {
  96. always {
  97. echo "CleaningUp work diretory"
  98. deleteDir()
  99. }
  100. failure {
  101. mail charset: 'UTF-8',
  102. subject: "Jenkins build ERROR",
  103. mimeType: 'text/html',
  104. to: "${mailto}",
  105. 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}"
  106. }
  107. success {
  108. mail charset: 'UTF-8',
  109. subject: "Jenkins build SUSCCESS",
  110. mimeType: 'text/html',
  111. to: "${mailto}",
  112. 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}"
  113. }
  114. }
  115. }