Jenkinsfile 4.6 KB

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