Jenkinsfile-prod 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_IMAGE='lab/admin-lab'
  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/labportal/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: "lab_admin-lab",
  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. }