Jenkinsfile 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. pipeline {
  2. agent {
  3. label "swarm"
  4. }
  5. environment {
  6. DOCKER_REGISTRY='dev-registry.infoclinica.ru:5000'
  7. BASE_REGISTRY='images.sdsys.ru'
  8. DOCKER_IMAGE='lab/admin-lab'
  9. SWARM_GIT_URL='ssh://git@git.sdsys.ru:8022/labportal/stack-deploy.git'
  10. SWARM_GIT_NAME='stack-deploy'
  11. PACKAGE_NAME='web-laboratory-admin'
  12. JENKINS_MAIL='jenkins@sdsys.ru'
  13. DOCKER_CERT_PATH='/run/secrets/swarm'
  14. DHOST='tcp://dev-iru-swarm.infoclinica.lan:2376 DOCKER_TLS_VERIFY=1'
  15. }
  16. parameters {
  17. string(
  18. name: "repo",
  19. defaultValue: "prod",
  20. description: "Repository to build and/or deploy from."
  21. )
  22. string(
  23. name: "version",
  24. defaultValue: "16.1",
  25. description: "Witch version to build and/or deploy."
  26. )
  27. string(
  28. name: "service_update",
  29. defaultValue: "lab_admin-lab",
  30. description: "Services to update."
  31. )
  32. string(
  33. name: "mailto",
  34. defaultValue: "admin@sdsys.ru",
  35. description: "Email which has to be notified."
  36. )
  37. }
  38. stages {
  39. stage("Build") {
  40. steps {
  41. script {
  42. withCredentials([usernamePassword(credentialsId: 'registry', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
  43. echo "Building ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}."
  44. sh "docker login -u ${USERNAME} -p ${PASSWORD} ${BASE_REGISTRY}"
  45. sh "docker build --build-arg repo=${repo} --build-arg version=${version} --no-cache -t ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ."
  46. }
  47. NEWTAG = sh script: "docker run --rm --entrypoint=\"\" ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} rpm -q --qf %{VERSION}-%{RELEASE} ${PACKAGE_NAME} 2> /dev/null", returnStdout: true
  48. NEWTAG = repo + "-" + NEWTAG + "_" + BUILD_NUMBER
  49. echo "NEWTAG is $NEWTAG"
  50. }
  51. sh "docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  52. }
  53. }
  54. stage("Publish") {
  55. steps {
  56. echo "Publishing ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  57. sh "docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  58. }
  59. }
  60. stage("Update") {
  61. when { expression { service_update != "" } }
  62. steps {
  63. script {
  64. for (String item : service_update.split()) {
  65. echo "Updating ${item}"
  66. try{
  67. sh "DOCKER_HOST=${DHOST} docker service update ${item} --image ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG}"
  68. }
  69. catch(err){
  70. echo "Recovering service $item"
  71. sh "DOCKER_HOST=${DHOST} docker service rollback ${item}"
  72. throw err
  73. }
  74. }
  75. }
  76. }
  77. }
  78. stage("Tagging") {
  79. steps{
  80. echo "Setting latest tag"
  81. sh """ docker tag ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${NEWTAG} ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest
  82. docker push ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:latest """
  83. echo "Updating tag info in ${SWARM_GIT_NAME} repository"
  84. withCredentials([sshUserPrivateKey(credentialsId: 'provision', keyFileVariable: 'GIT_SSH_KEY', passphraseVariable: '', usernameVariable: 'GIT_SSH_USERNAME')]) {
  85. sh """GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  86. git clone ${SWARM_GIT_URL}
  87. cd ${SWARM_GIT_NAME}
  88. echo -n ${NEWTAG} > tags/${DOCKER_IMAGE}.dev-version
  89. git add -A
  90. git config --global user.email "${JENKINS_MAIL}"
  91. git config --global user.name "Jenkins"
  92. git commit -m 'Version update'
  93. GIT_SSH_COMMAND='ssh -i ${GIT_SSH_KEY} -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
  94. git push origin master
  95. ls"""
  96. }
  97. }
  98. }
  99. }
  100. post {
  101. always {
  102. echo "CleaningUp work diretory"
  103. deleteDir()
  104. sh "docker image rm -f `docker image ls -q ${DOCKER_REGISTRY}/${DOCKER_IMAGE}:${repo}-${BUILD_NUMBER}`"
  105. }
  106. failure {
  107. mail charset: 'UTF-8',
  108. subject: "Jenkins build ERROR",
  109. mimeType: 'text/html',
  110. to: "${mailto}",
  111. 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}"
  112. }
  113. success {
  114. mail charset: 'UTF-8',
  115. subject: "Jenkins build SUSCCESS",
  116. mimeType: 'text/html',
  117. to: "${mailto}",
  118. 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}"
  119. }
  120. }
  121. }