Jenkinsfile 4.8 KB

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