Jenkinsfile 4.5 KB

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